Skip to content

Commit

Permalink
Add Ctrl + Enter command
Browse files Browse the repository at this point in the history
  • Loading branch information
FIameCaster committed Jan 9, 2024
1 parent 617da31 commit 7ee34a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
39 changes: 24 additions & 15 deletions package/src/extensions/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,31 @@ export const defaultCommands = (
return scroll()
}

keyCommandMap.Enter = (_e, selection, value) => {
const [indentChar, tabSize] = getIndent(options),
context = languageMap[getLanguage(editor)],
autoIndent = context?.autoIndent,
indenationCount =
Math.floor(getLineBefore(value, selection[0]).search(/\S|$/) / tabSize) * tabSize,
extraIndent = autoIndent?.[0]?.call(editor, selection, value) ? tabSize : 0,
extraLine = autoIndent?.[1]?.call(editor, selection, value),
newText =
"\n" +
indentChar.repeat(indenationCount + extraIndent) +
(extraLine ? "\n" + indentChar.repeat(indenationCount) : "")
keyCommandMap.Enter = (e, selection, value) => {
const code = getModifierCode(e) & 7
if (!code || code == mod) {
if (code) selection = <any>Array(2).fill(getLines(value, selection[1], selection[1])[2])
const [indentChar, tabSize] = getIndent(options),
autoIndent = languageMap[getLanguage(editor)]?.autoIndent,
indenationCount =
Math.floor(getLineBefore(value, selection[0]).search(/\S|$/) / tabSize) * tabSize,
extraIndent = autoIndent?.[0]?.call(editor, selection, value) ? tabSize : 0,
extraLine = autoIndent?.[1]?.call(editor, selection, value),
newText =
"\n" +
indentChar.repeat(indenationCount + extraIndent) +
(extraLine ? "\n" + indentChar.repeat(indenationCount) : "")

if (newText[1] || selection[1] < value.length) {
insertText(editor, newText, null, null, selection[0] + indenationCount + extraIndent + 1)
return scroll()
if (newText[1] || selection[1] < value.length) {
insertText(
editor,
newText,
selection[0],
selection[1],
selection[0] + indenationCount + extraIndent + 1,
)
return scroll()
}
}
}

Expand Down
15 changes: 2 additions & 13 deletions package/src/testsite/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEditor, EditorOptions, isMac, PrismEditor } from ".."
import { createEditor, EditorOptions, PrismEditor } from ".."
import { defaultCommands } from "../extensions/commands"
import { copyButton } from "../extensions/copyButton"
import "../grammars/markup"
Expand All @@ -25,7 +25,7 @@ import { addFullEditor, addReadonlyEditor, PrismEditorElement } from "../webComp
import "./style.css"
import { matchTags } from "../extensions/matchTags"
import { addOverscroll } from "../tooltips"
import { getClosestToken, getModifierCode } from "../utils"
import { getClosestToken } from "../utils"

const runBtn = <HTMLButtonElement>document.getElementById("run"),
wrapper = document.querySelector<HTMLDivElement>(".editor-wrapper")!,
Expand Down Expand Up @@ -98,8 +98,6 @@ const editor = createEditorWrapper(wrapper, {
runBtn.setAttribute("aria-hidden", <any>(currentOptions == code))
},
}),
commands = editor.keyCommandMap,
oldEnter = commands.Enter,
theme = <HTMLLinkElement>document.getElementById("theme"),
themes = <HTMLSelectElement>document.getElementById("themes")

Expand Down Expand Up @@ -176,15 +174,6 @@ runBtn.onclick = () => {
newEditor.textarea.focus()
}

runBtn.title = isMac ? "(Cmd + Enter)" : "(Ctrl + Enter)"

commands.Enter = (e, selection, value) => {
if (getModifierCode(e) == (isMac ? 4 : 2) && value != currentOptions) {
runBtn.click()
return true
} else return oldEnter!(e, selection, value)
}

addFullEditor("prism-editor")

const webComponent = document.querySelector<PrismEditorElement>("prism-editor")!
Expand Down

0 comments on commit 7ee34a4

Please sign in to comment.