Skip to content

Commit

Permalink
Add support for Monaco Editor (#239)
Browse files Browse the repository at this point in the history
Co-authored-by: Federico Brigante <[email protected]>
  • Loading branch information
ryan6416 and fregante authored Mar 11, 2023
1 parent 0f36b25 commit da7c48f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions source/ghost-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class AdvancedTextWrapper {
}

function wrapField(field) {
const monaco = field.closest('.monaco-editor');
if (monaco) {
const visualElement = monaco.querySelector('.monaco-editor-background');
return new AdvancedTextWrapper(monaco, visualElement);
}

if (field.classList.contains('ace_text-input')) {
const ace = field.parentNode;
const visualElement = ace.querySelector('.ace_scroller');
Expand Down
20 changes: 20 additions & 0 deletions source/unsafe-messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default function unsafeMessenger() {
function listener({target}) {
if (target.CodeMirror) {
codeMirror(target);
} else if (target.classList.contains('monaco-editor')) {
monacoEditor(target);
} else {
ace(target);
}
Expand Down Expand Up @@ -73,6 +75,24 @@ export default function unsafeMessenger() {
editor.blur();
});
}

function monacoEditor(target) {
const editor = globalThis.monaco.editor.getModel(target.dataset.uri);
sendBack(target, editor.getValue());

editor.onDidChangeContent(throttle(50, event => {
if (!event.isFlush) { // Flush === setValue
sendBack(target, editor.getValue());
}
}));

target.addEventListener('gt:transfer', () => {
editor.setValue(target.getAttribute('gt-value'));
});
target.addEventListener('gt:blur', () => {
target.ownerDocument.activeElement.blur();
});
}
}

// eslint-disable-next-line no-unused-expressions
Expand Down

0 comments on commit da7c48f

Please sign in to comment.