markdown shortcuts
This commit is contained in:
@@ -622,9 +622,13 @@ class TextFieldView extends BaseFieldView {
|
||||
* @param {KeyboardEvent} event
|
||||
*/
|
||||
onKeyDownMarkdown(event) {
|
||||
if (Espo.Utils.getKeyFromKeyEvent(event) !== 'Enter') {
|
||||
const key = Espo.Utils.getKeyFromKeyEvent(event);
|
||||
|
||||
if (key !== 'Enter') {
|
||||
this._lastEnteredKeyIsEnter = false;
|
||||
|
||||
this.handleKeyDownMarkdown(event, key);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -686,6 +690,70 @@ class TextFieldView extends BaseFieldView {
|
||||
|
||||
this.controlTextareaHeight();
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {KeyboardEvent} event
|
||||
* @param {string} key
|
||||
*/
|
||||
handleKeyDownMarkdown(event, key) {
|
||||
const target = event.target;
|
||||
|
||||
if (!(target instanceof HTMLTextAreaElement)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} wrapper
|
||||
*/
|
||||
const toggleWrap = (wrapper) => {
|
||||
const {selectionStart, selectionEnd} = target;
|
||||
|
||||
let text = target.value.substring(selectionStart, selectionEnd);
|
||||
|
||||
if (text === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
let textPrevious = text;
|
||||
|
||||
text = text.trimStart();
|
||||
|
||||
const startPart = textPrevious.substring(0, textPrevious.length - text.length);
|
||||
|
||||
textPrevious = text;
|
||||
|
||||
text = text.trimEnd();
|
||||
|
||||
const endPart = textPrevious.substring(text.length);
|
||||
|
||||
if (text.startsWith(wrapper) && text.endsWith(wrapper)) {
|
||||
text = text.slice(wrapper.length, - wrapper.length);
|
||||
} else {
|
||||
text = wrapper + text + wrapper;
|
||||
}
|
||||
|
||||
const newText = startPart + text + endPart;
|
||||
|
||||
// noinspection JSDeprecatedSymbols
|
||||
document.execCommand('insertText', false, newText);
|
||||
|
||||
const newStart = selectionStart + startPart.length;
|
||||
|
||||
target.setSelectionRange(newStart, newStart + text.length);
|
||||
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
};
|
||||
|
||||
if (key === 'Control+KeyB') {
|
||||
toggleWrap('**');
|
||||
}
|
||||
|
||||
if (key === 'Control+KeyI') {
|
||||
toggleWrap('_');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default TextFieldView;
|
||||
|
||||
Reference in New Issue
Block a user