feat: improve toggle selection under cursor in markdown editor

pull/6775/head
NGPixel 2 years ago
parent 51c4044509
commit 281ffd23f7
No known key found for this signature in database
GPG Key ID: B755FB6870B30F63

@ -428,9 +428,14 @@ async function toggleMarkup ({ start, end }) {
for (const selection of editor.getSelections()) { for (const selection of editor.getSelections()) {
const selectedText = editor.getModel().getValueInRange(selection) const selectedText = editor.getModel().getValueInRange(selection)
if (!selectedText) { if (!selectedText) {
const word = editor.getModel().getWordAtPosition(selection.getPosition()) const wordObj = editor.getModel().getWordAtPosition(selection.getPosition())
} const wordRange = new Range(selection.startLineNumber, wordObj.startColumn, selection.endLineNumber, wordObj.endColumn)
if (selectedText.startsWith(start) && selectedText.endsWith(end)) { if (wordObj.word.startsWith(start) && wordObj.word.endsWith(end)) {
edits.push({ range: wordRange, text: wordObj.word.substring(start.length, wordObj.word.length - end.length) })
} else {
edits.push({ range: wordRange, text: `${start}${wordObj.word}${end}` })
}
} else if (selectedText.startsWith(start) && selectedText.endsWith(end)) {
edits.push({ range: selection, text: selectedText.substring(start.length, selectedText.length - end.length) }) edits.push({ range: selection, text: selectedText.substring(start.length, selectedText.length - end.length) })
} else { } else {
edits.push({ range: selection, text: `${start}${selectedText}${end}` }) edits.push({ range: selection, text: `${start}${selectedText}${end}` })
@ -467,21 +472,26 @@ onMounted(async () => {
} }
}) })
// Allow `*` in word pattern for quick styling (toggle bold/italic without selection)
// original https://github.com/microsoft/vscode/blob/3e5c7e2c570a729e664253baceaf443b69e82da6/extensions/markdown-basics/language-configuration.json#L55
monaco.languages.setLanguageConfiguration('markdown', {
wordPattern: /([*_]{1,2}|~~|`+)?[\p{Alphabetic}\p{Number}\p{Nonspacing_Mark}]+(_+[\p{Alphabetic}\p{Number}\p{Nonspacing_Mark}]+)*\1/gu
})
// -> Initialize Monaco Editor // -> Initialize Monaco Editor
editor = monaco.editor.create(monacoRef.value, { editor = monaco.editor.create(monacoRef.value, {
value: pageStore.content,
language: 'markdown',
theme: 'wikijs',
automaticLayout: true, automaticLayout: true,
scrollBeyondLastLine: false, cursorBlinking: 'blink',
cursorSmoothCaretAnimation: true,
fontSize: 16, fontSize: 16,
formatOnType: true, formatOnType: true,
language: 'markdown',
lineNumbersMinChars: 3, lineNumbersMinChars: 3,
padding: { top: 10, bottom: 10 },
scrollBeyondLastLine: false,
tabSize: 2, tabSize: 2,
padding: { theme: 'wikijs',
top: 10, value: pageStore.content,
bottom: 10
},
wordWrap: 'on' wordWrap: 'on'
}) })
@ -494,7 +504,7 @@ onMounted(async () => {
id: 'markdown.extension.editing.toggleBold', id: 'markdown.extension.editing.toggleBold',
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyB], keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyB],
label: 'Toggle bold', label: 'Toggle bold',
precondition: 'editorHasSelection', precondition: '',
run (ed) { run (ed) {
toggleMarkup({ start: '**' }) toggleMarkup({ start: '**' })
} }
@ -506,7 +516,7 @@ onMounted(async () => {
id: 'markdown.extension.editing.toggleItalic', id: 'markdown.extension.editing.toggleItalic',
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyI], keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyI],
label: 'Toggle italic', label: 'Toggle italic',
precondition: 'editorHasSelection', precondition: '',
run (ed) { run (ed) {
toggleMarkup({ start: '*' }) toggleMarkup({ start: '*' })
} }

Loading…
Cancel
Save