Skip to content

Commit

Permalink
feat(editor): add script editor mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ala-n committed Jan 23, 2024
1 parent 1c0aac8 commit d35fdc5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/core/base/root.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ uip-root {
'header header'
'preview sidebar'
'settings settings'
'editor editor';
'editor editor'
'editor-js editor-js';

min-width: 100%;
height: auto;
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/editor/editor.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
.uip-editor {
grid-area: editor;

&[script] {
grid-area: editor-js;
}

&-inner {
display: flex;
padding: 1em;
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/editor/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export class UIPEditor extends UIPPluginPanel {
/** Highlight method declaration */
public static highlight = (editor: HTMLElement): void => Prism.highlightElement(editor, false);

/** Marker of JS Editor */
@boolAttr() public script: boolean;

/** Marker to display copy widget */
@boolAttr({name: 'copy'}) public showCopy: boolean;

Expand Down Expand Up @@ -61,7 +64,8 @@ export class UIPEditor extends UIPPluginPanel {
@memoize()
protected get $code(): HTMLElement {
const type = this.constructor as typeof UIPEditor;
return (<pre class={type.is + '-code language-html'}><code/></pre>) as HTMLElement;
const lang = this.script ? 'javascript' : 'html';
return (<pre class={type.is + '-code language-' + lang}><code/></pre>) as HTMLElement;
}

/** Wrapped [CodeJar](https://medv.io/codejar/) editor instance */
Expand Down Expand Up @@ -110,15 +114,16 @@ export class UIPEditor extends UIPPluginPanel {
}

/** Callback to call on an editor's content changes */
@decorate(debounce, 1000)
@decorate(debounce, 2000)
protected _onChange(): void {
this.model!.setHtml(this.value, this);
if (this.script) this.model!.setJS(this.value, this);
else this.model!.setHtml(this.value, this);
}

/** Change editor's markup from markup state changes */
@listen({event: 'uip:change', target: ($this: UIPEditor) => $this.$root})
protected _onRootStateChange(): void {
if (this.model!.lastModifier === this) return;
this.value = this.model!.html;
this.value = this.script ? this.model!.js : this.model!.html;
}
}

0 comments on commit d35fdc5

Please sign in to comment.