Skip to content

Commit e26157f

Browse files
author
Daniel Hottinger
committed
Make :iabbr work in contenteditable text boxes
Used by e.g. Gmail compose window for formatted text.
1 parent 80ab95a commit e26157f

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

common/content/editor.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,21 @@ const Editor = Module("editor", {
417417
let textbox = Editor.getEditor();
418418
if (!textbox)
419419
return false;
420-
let text = textbox.value;
420+
let text = '';
421+
let currStart = 0;
422+
let currEnd = 0;
423+
if (textbox instanceof Window) {
424+
text = textbox.getSelection().getRangeAt(0).startContainer.data;
425+
currStart = textbox.getSelection().getRangeAt(0).startOffset;
426+
currEnd = textbox.getSelection().getRangeAt(0).endOffset;
427+
} else {
428+
text = textbox.value;
429+
currStart = textbox.selectionStart;
430+
currEnd = textbox.selectionEnd;
431+
}
421432
if (typeof text !== "string")
422433
return false;
423434

424-
let currStart = textbox.selectionStart;
425-
let currEnd = textbox.selectionEnd;
426435
let foundWord = text.substring(0, currStart).replace(/.*[\s\n]/gm, '').match(RegExp('(' + abbreviations._match + ')$'));
427436
if (!foundWord)
428437
return true;
@@ -432,10 +441,20 @@ const Editor = Module("editor", {
432441
if (abbrev) {
433442
let len = foundWord.length;
434443
let abbrText = abbrev.text;
435-
text = text.substring(0, currStart - len) + abbrText + text.substring(currStart);
436-
textbox.value = text;
437-
textbox.selectionStart = currStart - len + abbrText.length;
438-
textbox.selectionEnd = currEnd - len + abbrText.length;
444+
if (textbox instanceof Window) {
445+
let r = textbox.getSelection().getRangeAt(0);
446+
r.setStart(r.startContainer, currStart - len);
447+
r.deleteContents();
448+
let textNode = document.createTextNode(abbrText);
449+
r.insertNode(textNode);
450+
r.selectNodeContents(textNode);
451+
textbox.getSelection().collapseToEnd();
452+
} else {
453+
text = text.substring(0, currStart - len) + abbrText + text.substring(currStart);
454+
textbox.value = text;
455+
textbox.selectionStart = currStart - len + abbrText.length;
456+
textbox.selectionEnd = currEnd - len + abbrText.length;
457+
}
439458
}
440459

441460
return true;

0 commit comments

Comments
 (0)