Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade ember-modifier #74

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 20 additions & 37 deletions frontend/app/modifiers/code-mirror.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { action } from '@ember/object';
import { bind } from '@ember/runloop';
import codemirror from 'codemirror';
import Modifier from 'ember-modifier';

Expand All @@ -8,41 +6,26 @@ import 'codemirror/mode/markdown/markdown';
import 'codemirror/mode/sparql/sparql';

export default class CodeMirrorModifier extends Modifier {
didInstall() {
this._setup();
}

didUpdateArguments() {
if (this._editor.getValue() !== this.args.named.content) {
this._editor.setValue(this.args.named.content);
didSetup = false;

modify(element, _, { content, onUpdate }) {
if (!this.didSetup) {
const editor = codemirror(element, {
lineNumbers: true,
matchBrackets: true,
mode: 'markdown',
readOnly: false,
styleActiveLine: true,
theme: 'base16-light',
value: content || '',
viewportMargin: Infinity,
});

editor.on('change', () => {
onUpdate(editor.getValue());
});

this.didSetup = true;
}

this._editor.setOption('readOnly', this.args.named.readOnly);
}

@action
_onChange(editor) {
this.args.named.onUpdate(editor.getValue());
}

_setup() {
if (!this.element) {
throw new Error('CodeMirror modifier has no element');
}

const editor = codemirror(this.element, {
lineNumbers: true,
matchBrackets: true,
mode: 'markdown',
readOnly: this.args.named.readOnly,
styleActiveLine: true,
theme: 'base16-light',
value: this.args.named.content || '',
viewportMargin: Infinity,
});

editor.on('change', bind(this, this._onChange));

this._editor = editor;
}
}
23 changes: 13 additions & 10 deletions frontend/app/modifiers/evaluate-scripts.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { modifier } from 'ember-modifier';

export default modifier(function evaluateScripts(element) {
element.querySelectorAll('script').forEach((orig) => {
const el = document.createElement('script');
export default modifier(
function evaluateScripts(element) {
element.querySelectorAll('script').forEach((orig) => {
const el = document.createElement('script');

el.textContent = orig.textContent;
el.textContent = orig.textContent;

Array.from(orig.attributes).forEach((attr) => {
el.setAttribute(attr.nodeName, attr.textContent);
});
Array.from(orig.attributes).forEach((attr) => {
el.setAttribute(attr.nodeName, attr.textContent);
});

orig.replaceWith(el);
});
});
orig.replaceWith(el);
});
},
{ eager: true },
);
Loading