Skip to content

Commit

Permalink
custom context menu (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
e9gille authored Apr 1, 2019
1 parent b001e7b commit 0ca7334
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 23 deletions.
3 changes: 0 additions & 3 deletions src/cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,6 @@
}
return !0;
};
q.cwd.oncontextmenu = D.oncmenu;
q.args.oncontextmenu = D.oncmenu;
q.env.oncontextmenu = D.oncmenu;
q.fav_name.onkeyup = () => {
const u = sel.name;
const v = q.fav_name.value || '';
Expand Down
3 changes: 2 additions & 1 deletion src/ed.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
ed.$e = $(ed.dom);
ed.jumps = [];
ed.focusTS = 0;
// ed.dom.oncontextmenu = D.oncmenu;
D.createContextMenu(ed.dom, ed);
ed.oText = '';
ed.oStop = []; // remember original text and "stops" to avoid pointless saving on EP
ed.stop = new Set(); // remember original text and "stops" to avoid pointless saving on EP
Expand All @@ -34,6 +34,7 @@
autoClosingBrackets: !!D.prf.autoCloseBrackets(),
automaticLayout: true,
autoIndent: D.prf.indent() >= 0,
contextmenu: false,
cursorStyle: D.prf.blockCursor() ? 'block' : 'line',
cursorBlinking: D.prf.cursorBlinking(),
emptySelectionClipboard: false,
Expand Down
64 changes: 46 additions & 18 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,51 @@ const Console = console;
if (d && (e.ctrlKey || e.metaKey) && !e.shiftKey && !e.altKey) D.commands[d > 0 ? 'ZMI' : 'ZMO']();
};
document.body.className += ` zoom${D.prf.zoom()}`;

// context menu
const cmitems = ['Cut', 'Copy', 'Paste'].map(x => ({ label: x, role: x.toLowerCase() }))
.concat({ type: 'separator' })
.concat(['Undo', 'Redo'].map(x => ({
label: x,
click: () => {
if (!D.ide) return;
const u = D.ide.focusedWin;
const { me } = u;
if (u && me[x.toLowerCase()]) me[x.toLowerCase()]();
},
})));
const cmenu = D.el.Menu.buildFromTemplate(cmitems);
D.oncmenu = (e) => { e.preventDefault(); cmenu.popup(D.elw); };
}

D.createContextMenu = (el, win) => {
if (!D.el) return;
el.oncontextmenu = (e) => {
e.preventDefault();
e.stopPropagation();
const hasSelection = win
? !win.me.getSelection().isEmpty()
: el.getSelection().type === 'Range';
const isReadOnly = !!win && win.isReadOnly;
const tc = !!win && !!win.tc;
const fx = !!win && !tc && win.isCode && !win.isReadOnlyEntity;
const cmitems = [
{ label: 'Cut', role: 'cut', enabled: hasSelection && !isReadOnly },
{ label: 'Copy', role: 'copy', enabled: hasSelection },
{ label: 'Paste', role: 'paste', enabled: !isReadOnly },
{ type: 'separator' },
{ label: 'Undo', role: 'undo', enabled: !tc },
{ label: 'Redo', role: 'redo', enabled: !tc },
];
if (win && !win.session.get()) {
const { me } = win;
if (fx || tc) {
cmitems.unshift(...[
{
label: 'Fix',
click: () => { win.FX(me); },
visible: fx,
},
{
label: 'Skip to line',
click: () => { win.STL(me); },
visible: tc,
},
{ type: 'separator' },
]);
}
}
const cmenu = D.el.Menu.buildFromTemplate(cmitems);
cmenu.popup();
};
};
D.createContextMenu(window);

D.open = D.open || ((url, o) => {
const {
height, width, x, y,
Expand Down Expand Up @@ -160,9 +188,9 @@ const Console = console;
if (!e.altKey || e.ctrlKey || e.metaKey || e.which < 65 || e.which > 90) return undefined;
const c = String.fromCharCode(e.which).toLowerCase();
const C = c.toUpperCase();
const $ctx = $('.ui-widget-overlay').length ?
$('.ui-dialog:visible').last() :
$('body'); // modal dialogs take priority
const $ctx = $('.ui-widget-overlay').length
? $('.ui-dialog:visible').last()
: $('body'); // modal dialogs take priority

const $a = $('u:visible', $ctx).map((i, n) => {
const h = n.innerHTML;
Expand Down
1 change: 1 addition & 0 deletions src/prf_col.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@
autoClosingBrackets: true,
automaticLayout: true,
autoIndent: true,
contextmenu: false,
cursorStyle: D.prf.blockCursor() ? 'block' : 'line',
cursorBlinking: D.prf.cursorBlinking(),
folding: false,
Expand Down
1 change: 1 addition & 0 deletions src/prf_shc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
autoClosingBrackets: false,
automaticLayout: true,
autoIndent: false,
contextmenu: false,
cursorStyle: D.prf.blockCursor() ? 'block' : 'line',
cursorBlinking: D.prf.cursorBlinking(),
folding: false,
Expand Down
3 changes: 2 additions & 1 deletion src/se.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
se.dom = document.createElement('div');
se.dom.className = 'ride_win';
se.$e = $(se.dom);
// se.dom.oncontextmenu = D.oncmenu;
D.createContextMenu(se.dom, se);
const fs = D.zoom2fs[D.prf.zoom() + 10];
const me = monaco.editor.create(se.dom, {
acceptSuggestionOnCommitCharacter: true,
acceptSuggestionOnEnter: 'off',
autoClosingBrackets: !!D.prf.autoCloseBrackets(),
automaticLayout: false,
autoIndent: false,
contextmenu: false,
cursorStyle: D.prf.blockCursor() ? 'block' : 'line',
cursorBlinking: D.prf.cursorBlinking(),
emptySelectionClipboard: false,
Expand Down

0 comments on commit 0ca7334

Please sign in to comment.