Skip to content

Commit 8f212ae

Browse files
authored
Feature: Code Editor modal, adds pretty-print support (#18123)
* Reverts `elementName` constant * Adds `formatOnLoad` option * Code editor modal markup tidyup * Wires up `formatOnLoad` with Tiptap's view-source action
1 parent bc38817 commit 8f212ae

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/Umbraco.Web.UI.Client/src/packages/code-editor/code-editor-modal/code-editor-modal.element.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import type { UmbCodeEditorElement } from '../components/code-editor.element.js';
22
import type { UmbCodeEditorModalData, UmbCodeEditorModalValue } from './code-editor-modal.token.js';
3-
import { css, html, ifDefined, customElement, query } from '@umbraco-cms/backoffice/external/lit';
3+
import { css, customElement, html, ifDefined, query } from '@umbraco-cms/backoffice/external/lit';
44
import { UmbModalBaseElement } from '@umbraco-cms/backoffice/modal';
55

6-
const elementName = 'umb-code-editor-modal';
7-
8-
@customElement(elementName)
6+
@customElement('umb-code-editor-modal')
97
export class UmbCodeEditorModalElement extends UmbModalBaseElement<UmbCodeEditorModalData, UmbCodeEditorModalValue> {
108
@query('umb-code-editor')
119
_codeEditor?: UmbCodeEditorElement;
@@ -18,29 +16,39 @@ export class UmbCodeEditorModalElement extends UmbModalBaseElement<UmbCodeEditor
1816
#handleCancel() {
1917
this.modalContext?.reject();
2018
}
19+
20+
#onLoaded() {
21+
if (this.data?.formatOnLoad) {
22+
setTimeout(() => {
23+
this._codeEditor?.editor?.monacoEditor?.getAction('editor.action.formatDocument')?.run();
24+
}, 100);
25+
}
26+
}
27+
2128
override render() {
2229
return html`
2330
<umb-body-layout .headline=${this.data?.headline ?? 'Code Editor'}>
2431
<div id="editor-box">${this.#renderCodeEditor()}</div>
25-
<div slot="actions">
26-
<uui-button
27-
id="cancel"
28-
label=${this.localize.term('general_cancel')}
29-
@click=${this.#handleCancel}></uui-button>
30-
<uui-button
31-
id="confirm"
32-
color="${this.data?.color || 'positive'}"
33-
look="primary"
34-
label="${this.data?.confirmLabel || this.localize.term('general_submit')}"
35-
@click=${this.#handleConfirm}></uui-button>
36-
</div>
32+
<uui-button
33+
slot="actions"
34+
label=${this.localize.term('general_cancel')}
35+
@click=${this.#handleCancel}></uui-button>
36+
<uui-button
37+
slot="actions"
38+
color=${this.data?.color || 'positive'}
39+
look="primary"
40+
label=${this.data?.confirmLabel || this.localize.term('general_submit')}
41+
@click=${this.#handleConfirm}></uui-button>
3742
</umb-body-layout>
3843
`;
3944
}
4045

4146
#renderCodeEditor() {
4247
return html`
43-
<umb-code-editor language=${ifDefined(this.data?.language)} .code=${this.data?.content ?? ''}></umb-code-editor>
48+
<umb-code-editor
49+
language=${ifDefined(this.data?.language)}
50+
.code=${this.data?.content ?? ''}
51+
@loaded=${this.#onLoaded}></umb-code-editor>
4452
`;
4553
}
4654

@@ -63,6 +71,6 @@ export default UmbCodeEditorModalElement;
6371

6472
declare global {
6573
interface HTMLElementTagNameMap {
66-
[elementName]: UmbCodeEditorModalElement;
74+
'umb-code-editor-modal': UmbCodeEditorModalElement;
6775
}
6876
}

src/Umbraco.Web.UI.Client/src/packages/code-editor/code-editor-modal/code-editor-modal.token.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface UmbCodeEditorModalData {
99
language: 'razor' | 'typescript' | 'javascript' | 'css' | 'markdown' | 'json' | 'html';
1010
color?: 'positive' | 'danger';
1111
confirmLabel?: string;
12+
formatOnLoad?: boolean;
1213
}
1314

1415
export interface UmbCodeEditorModalValue {

src/Umbraco.Web.UI.Client/src/packages/tiptap/extensions/toolbar/source-editor.tiptap-toolbar-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default class UmbTiptapToolbarSourceEditorExtensionApi extends UmbTiptapT
1313
headline: 'Edit source code',
1414
content: editor?.getHTML() ?? '',
1515
language: 'html',
16+
formatOnLoad: true,
1617
},
1718
});
1819

0 commit comments

Comments
 (0)