diff --git a/client/components/codeEditor/customKeyMaps.js b/client/components/codeEditor/customKeyMaps.js index 86b108ac3..7da06b119 100644 --- a/client/components/codeEditor/customKeyMaps.js +++ b/client/components/codeEditor/customKeyMaps.js @@ -20,72 +20,142 @@ const indentLess = (view)=>{ const makeBold = (view)=>{ const { from, to } = view.state.selection.main; const selected = view.state.doc.sliceString(from, to); - const text = selected.startsWith('**') && selected.endsWith('**') - ? selected.slice(2, -2) - : `**${selected}**`; + + let text, cursor; + + if(from === to) { + text = '****'; + cursor = from + 2; + } else if(selected.startsWith('**') && selected.endsWith('**')) { + text = selected.slice(2, -2); + cursor = from + text.length; + } else { + text = `**${selected}**`; + cursor = from + text.length; + } + view.dispatch({ changes : { from, to, insert: text }, - selection : { anchor: from + text.length }, + selection : { anchor: cursor }, }); + return true; }; const makeItalic = (view)=>{ const { from, to } = view.state.selection.main; const selected = view.state.doc.sliceString(from, to); - const text = selected.startsWith('*') && selected.endsWith('*') - ? selected.slice(1, -1) - : `*${selected}*`; + + let text, cursor; + + if(from === to) { + text = '**'; + cursor = from + 1; + } else if(selected.startsWith('*') && selected.endsWith('*')) { + text = selected.slice(2, -2); + cursor = from + text.length; + } else { + text = `*${selected}*`; + cursor = from + text.length; + } + view.dispatch({ changes : { from, to, insert: text }, - selection : { anchor: from + text.length }, + selection : { anchor: cursor }, }); + return true; }; const makeUnderline = (view)=>{ const { from, to } = view.state.selection.main; const selected = view.state.doc.sliceString(from, to); - const text = selected.startsWith('') && selected.endsWith('') - ? selected.slice(3, -4) - : `${selected}`; + + let text, cursor; + + if(from === to) { + text = ''; + cursor = from + 3; + } else if(selected.startsWith('') && selected.endsWith('')) { + text = selected.slice(3, -4); + cursor = from + text.length; + } else { + text = `${selected}`; + cursor = from + text.length; + } + view.dispatch({ changes : { from, to, insert: text }, - selection : { anchor: from + text.length }, + selection : { anchor: cursor }, }); + return true; }; - const makeSuper = (view)=>{ const { from, to } = view.state.selection.main; const selected = view.state.doc.sliceString(from, to); - const text = selected.startsWith('^') && selected.endsWith('^') - ? selected.slice(1, -1) - : `^${selected}^`; + + let text, cursor; + + if(from === to) { + text = '^^'; + cursor = from + 1; + } else if(selected.startsWith('^') && selected.endsWith('^')) { + text = selected.slice(1, -1); + cursor = from + text.length; + } else { + text = `^${selected}^`; + cursor = from + text.length; + } + view.dispatch({ changes : { from, to, insert: text }, - selection : { anchor: from + text.length }, + selection : { anchor: cursor }, }); + return true; }; const makeSub = (view)=>{ const { from, to } = view.state.selection.main; const selected = view.state.doc.sliceString(from, to); - const text = selected.startsWith('^^') && selected.endsWith('^^') - ? selected.slice(2, -2) - : `^^${selected}^^`; + + let text, cursor; + + if(from === to) { + text = '^^^^'; + cursor = from + 2; + } else if(selected.startsWith('^^') && selected.endsWith('^^')) { + text = selected.slice(2, -2); + cursor = from + text.length; + } else { + text = `^^${selected}^^`; + cursor = from + text.length; + } + view.dispatch({ changes : { from, to, insert: text }, - selection : { anchor: from + text.length }, + selection : { anchor: cursor }, }); + return true; }; -const makeNbsp = (view)=>{ - const { from, to } = view.state.selection.main; - view.dispatch({ changes: { from, to, insert: ' ' } }); - return true; +const makeNbsp = (view) => { + const { from } = view.state.selection.main; + + const prev2 = from >= 2 + ? view.state.doc.sliceString(from - 2, from) + : ''; + + const insert = (prev2 === ':>' || prev2 === '>>') ? '>' : ':>'; + + view.dispatch({ + changes: { from, to: from, insert }, + selection: { anchor: from + insert.length }, + }); + + return true; }; const makeSpace = (view)=>{ @@ -193,7 +263,7 @@ export const generalKeymap = Prec.high(keymap.of([ { key: 'Mod-z', run: undo }, //i think it may be unnecessary { key: 'Mod-Shift-z', run: redo }, { key: 'Mod-y', run: redo }, - { key: 'Mod-d', run: deleteLine}, + { key: 'Mod-d', run: deleteLine }, ])); export const markdownKeymap = Prec.highest(keymap.of([