Commit 103579f2 authored by Michael Murtaugh's avatar Michael Murtaugh
Browse files

linted

parent e5b5d282
......@@ -13,7 +13,6 @@ while fragment never includes the hash, e.g. fragment is '', or 'foo'
import { parseFragment } from './url.js';
export default class CCManager {
// constructor() {}
frameHashchange(frame, baseurl, fragment) {
......@@ -29,7 +28,7 @@ export default class CCManager {
}
}
frameClick (frame, d) {
frameClick(frame, d) {
// console.log('cc doFrameClick', frame, d);
// intercept clicks and potentially route them
// returns true if handled
......@@ -46,15 +45,16 @@ export default class CCManager {
this.frame.src = d.href;
return true;
}
return false;
}
setActiveEditor (editor) {
setActiveEditor(editor) {
if (this.cclab) {
this.cclab.setActiveEditor(editor);
}
}
setActiveFrame (frame) {
setActiveFrame(frame) {
if (this.activeFrame !== frame) {
this.activeFrame = frame;
}
......@@ -62,5 +62,4 @@ export default class CCManager {
this.cclab.setActiveFrame(frame);
}
}
}
/* Fresh start 2021 */
import { each, find } from '@lumino/algorithm';
import { each } from '@lumino/algorithm';
import { CommandRegistry } from '@lumino/commands';
// import { Message } from '@lumino/messaging';
import {
......@@ -18,7 +18,6 @@ import './style/index.css';
import * as reflinks from './reflinks.js';
import { parseFragment } from './url.js';
// Examples ?!
// https://github.com/jupyterlab/jupyterlab/tree/011ff58a61689b734e29a2fe2c97c9aaa52efe25/examples
// Commands
......@@ -35,12 +34,11 @@ import { parseFragment } from './url.js';
// });
class CCLab {
constructor() {
// const dialog = document.getElementById('dialog');
this.version = 'dev2021-02-17-17:46';
let baseURL = new URL(window.location);
const baseURL = new URL(window.location);
baseURL.pathname = '';
const cc = new CCManager();
this.cc = cc;
......@@ -68,7 +66,6 @@ class CCLab {
// }
});
this.initContextMenu();
const frame = new FrameWidget(cc, baseURL.href);
......@@ -95,7 +92,7 @@ class CCLab {
Widget.attach(this.mainPanel, document.body);
}
initCommands () {
initCommands() {
const commands = new CommandRegistry();
this.commands = commands;
......@@ -104,7 +101,7 @@ class CCLab {
iconClass: 'fa fa-edit',
execute: () => {
const editor = new EditorWidget(this.cc);
dock.addWidget(editor);
this.dock.addWidget(editor);
},
});
......@@ -116,7 +113,7 @@ class CCLab {
// console.log('Import...');
// showOpenfile();
const editor = new EditorWidget(this.cc);
dock.addWidget(editor);
this.dock.addWidget(editor);
// https://github.com/jupyterlab/jupyterlab/blob/011ff58a61689b734e29a2fe2c97c9aaa52efe25/examples/filebrowser/src/index.ts
// execute: () => {
// each(fbWidget.selectedItems(), item => {
......@@ -132,27 +129,27 @@ class CCLab {
iconClass: 'fa fa-save',
execute: () => {
console.log('cclab save');
let editor = this.activeEditor;
const editor = this.activeEditor;
if (editor) {
fetch(editor.url, {
method: 'POST',
headers: {
// 'Content-Type': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
},
// body: {text: this.activeEditor.getValue()},
body: 'text=' + encodeURIComponent(editor.getValue()),
body: `text=${encodeURIComponent(editor.getValue())}`,
})
.then(resp => resp.json())
.then(data => {
console.log("save resp", data);
if (data.text === "ok") {
editor.doPostSave();
}
})
.catch(error => {
console.log("save error", error);
});
.then((resp) => resp.json())
.then((data) => {
console.log('save resp', data);
if (data.text === 'ok') {
editor.doPostSave();
}
})
.catch((error) => {
console.log('save error', error);
});
}
// showOpenfile();
// https://github.com/jupyterlab/jupyterlab/blob/011ff58a61689b734e29a2fe2c97c9aaa52efe25/examples/filebrowser/src/index.ts
......@@ -160,7 +157,6 @@ class CCLab {
// const context = docManager.contextForWidget(activeWidget);
// return context?.save();
// }
},
});
......@@ -258,8 +254,8 @@ class CCLab {
});
}
initCommandKeyBindings () {
const commands = this.commands;
initCommandKeyBindings() {
const { commands } = this;
commands.addKeyBinding({
keys: ['Accel E'],
......@@ -305,7 +301,7 @@ class CCLab {
}
initMenu() {
const commands = this.commands;
const { commands } = this;
const filemenu = new Menu({ commands });
this.filemenu = filemenu;
filemenu.addItem({ command: 'file:edit' });
......@@ -335,7 +331,7 @@ class CCLab {
}
initContextMenu() {
const commands = this.commands;
const { commands } = this;
const contextMenu = new ContextMenu({ commands });
this.contextMenu = contextMenu;
......@@ -358,38 +354,39 @@ class CCLab {
contextMenu.addItem({ command: 'link:openInEditor', selector: '.cclink' });
}
tabForWidget (widget) {
tabForWidget(widget) {
// shld be in lumino api, but I don't manage to find it..
// so some code to search the dock for the tab matching a certain
// widget
each(this.dock.tabBars(), tabbar => {
console.log("tabbar*", tabbar);
each(tabbar.titles, t => {
console.log("title", t);
})
each(this.dock.tabBars(), (tabbar) => {
console.log('tabbar*', tabbar);
each(tabbar.titles, (t) => {
console.log('title', t);
});
});
}
openInEditor (href) {
openInEditor(href) {
const editor = new EditorWidget(this.cc, href);
this.dock.addWidget(editor);
this.dock.addWidget(editor);
}
frameClick(frame, {href, target}) {
frameClick(frame, { href, target }) {
if (this.contextMenu.menu.isVisible) {
this.contextMenu.menu.close();
}
//if (this.menubar.isVisible) {
each(this.menubar.menus, menu=> {
// if (this.menubar.isVisible) {
each(this.menubar.menus, (menu) => {
if (menu.isVisible) {
menu.close();
}
});
//}
// }
if (href && href.match(/\.(md|py|css|js|json|txt)$/i)) {
this.openInEditor(href);
return true;
}
return false;
}
pasteLink() {
......
......@@ -15,33 +15,33 @@ import * as reflinks from './reflinks.js';
/* eslint no-unused-vars: ["error", { "args": "none" }] */
function extract_filename (path) {
let m = path.match(/[^/]*$/);
function extractFilename(path) {
const m = path.match(/[^/]*$/);
if (m !== null) {
return m[0];
}
return null;
}
function extract_ext (path) {
let m = path.match(/[^.]*$/);
function extractExtension(path) {
const m = path.match(/[^.]*$/);
if (m !== null) {
return m[0];
}
return '';
}
const MODES_BY_EXT ={}
MODES_BY_EXT['md'] = { name: 'markdown', fold: true };
MODES_BY_EXT['py'] = { name: 'python' };
MODES_BY_EXT['html'] = { name: 'htmlmixed' };
MODES_BY_EXT['css'] = { name: 'css' };
MODES_BY_EXT['js'] = { name: 'javascript' };
MODES_BY_EXT['json'] = { name: 'javascript' };
const MODES_BY_EXT = {};
MODES_BY_EXT.md = { name: 'markdown', fold: true };
MODES_BY_EXT.py = { name: 'python' };
MODES_BY_EXT.html = { name: 'htmlmixed' };
MODES_BY_EXT.css = { name: 'css' };
MODES_BY_EXT.js = { name: 'javascript' };
MODES_BY_EXT.json = { name: 'javascript' };
// MODES_BY_EXT['makefile'] = { name: 'makefile' };
const CM_OPTS = {
mode: MODES_BY_EXT['md'],
mode: MODES_BY_EXT.md,
// mode: {'name': 'lua'},
theme: 'default',
lineNumbers: true,
......@@ -102,11 +102,11 @@ export default class EditorWidget extends Widget {
this.title.closable = true;
if (url) {
const urlp = new URL(url, this.cc.baseurl);
const filename = extract_filename(urlp.pathname);
const filename = extractFilename(urlp.pathname);
this.title.label = filename;
this.ext = extract_ext(filename).toLowerCase();
this.ext = extractExtension(filename).toLowerCase();
this.url = urlp.href;
this.loadFromURL(this.url).then(text => {
this.loadFromURL(this.url).then((text) => {
this.initCodeMirror(text);
});
} else {
......@@ -116,15 +116,15 @@ export default class EditorWidget extends Widget {
// this.title.caption = this.title.label;
}
initCodeMirror (text) {
const cm_opts = Object.assign({}, CM_OPTS);
console.log("MODES_BY_EXT", this.ext, MODES_BY_EXT[this.ext])
cm_opts.mode = MODES_BY_EXT[this.ext] || MODES_BY_EXT['md'];
initCodeMirror(text) {
const cmopts = { ...CM_OPTS };
console.log('MODES_BY_EXT', this.ext, MODES_BY_EXT[this.ext]);
cmopts.mode = MODES_BY_EXT[this.ext] || MODES_BY_EXT.md;
if (text) {
cm_opts.value = text;
cmopts.value = text;
}
this.cm = CodeMirror(this.editorDiv, cm_opts);
this.cm.on("change", this.change.bind(this));
this.cm = CodeMirror(this.editorDiv, cmopts);
this.cm.on('change', this.change.bind(this));
const nop = function nop(cm) {};
// block shift-ctrl-up to prevent shift-up + cc keys
this.cm.setOption('extraKeys', {
......@@ -135,17 +135,24 @@ export default class EditorWidget extends Widget {
});
}
change () {
this.title.className = "unsaved";
change() {
this.title.className = 'unsaved';
}
async loadFromURL (url) {
let resp = await fetch(url);
return await resp.text();
async loadFromURL(url) {
this.loadedfromURL = url; // just to use this
const resp = await fetch(url);
// apparently calling await in the return
// of an async function is redundant
// (it's implicit ?!)
// return await resp.text();
return resp.text();
}
doPostSave () {
this.title.className = "";
doPostSave() {
this.title.className = '';
}
get editorDiv() {
return this.node.getElementsByTagName('div')[0];
}
......
......@@ -76,7 +76,6 @@ export default class FrameWidget extends Widget {
}
}
onAfterAttach() {
// console.log("editor.onAfterAttach", this, "hidden", this.isHidden);
if (!this.isHidden) {
......@@ -91,5 +90,4 @@ export default class FrameWidget extends Widget {
this.cc.setActiveFrame(this);
// this.cm.refresh();
}
}
// import * as timecode from './timecode.js';
import { parseFragment } from './url.js';
/*
/*
see this as a kind of "controller overlay"
patching link clicks to some kind of
......@@ -51,28 +51,27 @@ export default class IFrame {
this.frame.load(this, newURL);
this.iframe.contentWindow.addEventListener('hashchange', this.hashchange.bind(this));
this.iframe.contentDocument.addEventListener('click', this.clickhander.bind(this));
this.map_links();
this.mapLinks();
// console.log('iframe load', this.iframe.contentWindow.location);
}
map_links () {
mapLinks() {
const cdocument = this.iframe.contentDocument;
const links = cdocument.querySelectorAll("a");
for (let i=0; i<links.length; i ++) {
let link = links[i];
let link_href = link.href;
if (link_href.startsWith(this.url + '#')) {
link.classList.add("cc-internal-link");
const links = cdocument.querySelectorAll('a');
for (let i = 0; i < links.length; i += 1) {
const link = links[i];
if (link.href.startsWith(`${this.url}#`)) {
link.classList.add('cc-internal-link');
} else {
link.classList.add("cclink");
link.classList.add('cclink');
}
}
if (this.frame.cc.contextMenu){
if (this.frame.cc.contextMenu) {
cdocument.addEventListener('contextmenu', (event) => {
if (this.frame.cc.contextMenu.open(event)) {
event.preventDefault();
}
});
});
cdocument.addEventListener('mousedown', (event) => {
if (this.frame.cc.contextMenu.menu.isVisible) {
this.frame.cc.contextMenu.menu.close();
......@@ -81,7 +80,10 @@ export default class IFrame {
}
cdocument.addEventListener('click', (event) => {
// console.log("click", event);
if (this.frame.cc.frameClick(this, {href: event.target.href, target: event.target.target})) {
if (this.frame.cc.frameClick(this, {
href: event.target.href,
target: event.target.target,
})) {
event.preventDefault();
}
});
......
......@@ -8,6 +8,7 @@
"license": "MIT",
"private": true,
"dependencies": {
"@lumino/algorithm": "^1.3.3",
"@lumino/commands": "^1.12.0",
"@lumino/default-theme": "^0.9.0",
"@lumino/dragdrop": "^1.7.1",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment