-
-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddfb140
commit 5406472
Showing
6 changed files
with
567 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import {getCurrentLinePosition} from "../util/getCurrentLinePosition"; | ||
import {getMarkdown} from "../util/getMarkdown"; | ||
import {processKeymap} from "../util/processKeymap"; | ||
import {formatRender} from "./formatRender"; | ||
import {getSelectPosition} from "./getSelectPosition"; | ||
import {insertText} from "./insertText"; | ||
|
||
export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => { | ||
const editorElement = vditor.editor.element; | ||
const position = getSelectPosition(editorElement); | ||
const text = getMarkdown(vditor); | ||
// tab and shift + tab | ||
if (vditor.options.tab && event.key === "Tab") { | ||
const selectLinePosition = getCurrentLinePosition(position, text); | ||
const selectLineList = text.substring(selectLinePosition.start, selectLinePosition.end - 1).split("\n"); | ||
|
||
if (event.shiftKey) { | ||
let shiftCount = 0; | ||
let startIsShift = false; | ||
const selectionShiftResult = selectLineList.map((value, index) => { | ||
let shiftLineValue = value; | ||
if (value.indexOf(vditor.options.tab) === 0) { | ||
if (index === 0) { | ||
startIsShift = true; | ||
} | ||
shiftCount++; | ||
shiftLineValue = value.replace(vditor.options.tab, ""); | ||
} | ||
return shiftLineValue; | ||
}).join("\n"); | ||
|
||
formatRender(vditor, text.substring(0, selectLinePosition.start) + | ||
selectionShiftResult + text.substring(selectLinePosition.end - 1), | ||
{ | ||
end: position.end - shiftCount * vditor.options.tab.length, | ||
start: position.start - (startIsShift ? vditor.options.tab.length : 0), | ||
}); | ||
return true; | ||
} | ||
|
||
if (position.start === position.end) { | ||
insertText(vditor, vditor.options.tab, ""); | ||
return true; | ||
} | ||
const selectionResult = selectLineList.map((value) => { | ||
return vditor.options.tab + value; | ||
}).join("\n"); | ||
formatRender(vditor, text.substring(0, selectLinePosition.start) + selectionResult + | ||
text.substring(selectLinePosition.end - 1), | ||
{ | ||
end: position.end + selectLineList.length * vditor.options.tab.length, | ||
start: position.start + vditor.options.tab.length, | ||
}); | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
return true; | ||
} | ||
|
||
// delete | ||
if (!event.metaKey && !event.ctrlKey && !event.shiftKey && event.key === "Backspace") { | ||
if (position.start !== position.end) { | ||
insertText(vditor, "", "", true); | ||
} else { | ||
// delete emoji | ||
const emojiMatch = text.substring(0, position.start).match(/([\u{1F300}-\u{1F5FF}][\u{2000}-\u{206F}][\u{2700}-\u{27BF}]|([\u{1F900}-\u{1F9FF}]|[\u{1F300}-\u{1F5FF}]|[\u{1F680}-\u{1F6FF}]|[\u{1F600}-\u{1F64F}])[\u{2000}-\u{206F}][\u{2600}-\u{26FF}]|[\u{1F300}-\u{1F5FF}]|[\u{1F100}-\u{1F1FF}]|[\u{1F600}-\u{1F64F}]|[\u{1F680}-\u{1F6FF}]|[\u{1F200}-\u{1F2FF}]|[\u{1F900}-\u{1F9FF}]|[\u{1F000}-\u{1F02F}]|[\u{FE00}-\u{FE0F}]|[\u{1F0A0}-\u{1F0FF}]|[\u{0000}-\u{007F}][\u{20D0}-\u{20FF}]|[\u{0000}-\u{007F}][\u{FE00}-\u{FE0F}][\u{20D0}-\u{20FF}])$/u); | ||
const deleteChar = emojiMatch ? emojiMatch[0].length : 1; | ||
formatRender(vditor, | ||
text.substring(0, position.start - deleteChar) + text.substring(position.start), | ||
{ | ||
end: position.start - deleteChar, | ||
start: position.start - deleteChar, | ||
}); | ||
} | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
return true; | ||
} | ||
|
||
// hotkey command + delete | ||
if (vditor.options.keymap.deleteLine) { | ||
if (processKeymap(vditor.options.keymap.deleteLine, event, () => { | ||
const linePosition = getCurrentLinePosition(position, text); | ||
const deletedText = text.substring(0, linePosition.start) + text.substring(linePosition.end); | ||
const startIndex = Math.min(deletedText.length, position.start); | ||
formatRender(vditor, deletedText, { | ||
end: startIndex, | ||
start: startIndex, | ||
}); | ||
})) { | ||
return true; | ||
} | ||
} | ||
|
||
// hotkey command + d | ||
if (vditor.options.keymap.duplicate) { | ||
if (processKeymap(vditor.options.keymap.duplicate, event, () => { | ||
let lineText = text.substring(position.start, position.end); | ||
if (position.start === position.end) { | ||
const linePosition = getCurrentLinePosition(position, text); | ||
lineText = text.substring(linePosition.start, linePosition.end); | ||
formatRender(vditor, | ||
text.substring(0, linePosition.end) + lineText + text.substring(linePosition.end), | ||
{ | ||
end: position.end + lineText.length, | ||
start: position.start + lineText.length, | ||
}); | ||
} else { | ||
formatRender(vditor, | ||
text.substring(0, position.end) + lineText + text.substring(position.end), | ||
{ | ||
end: position.end + lineText.length, | ||
start: position.start + lineText.length, | ||
}); | ||
} | ||
})) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; |
Oops, something went wrong.