Skip to content

Commit ae5757c

Browse files
committed
update demo
1 parent 1e6ca43 commit ae5757c

10 files changed

Lines changed: 251 additions & 41 deletions

.DS_Store

2 KB
Binary file not shown.

index.html

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<head>
55
<meta charset="utf-8">
6-
<meta name="apple-mobile-web-app-capable" content="yes" />
6+
<meta name="mobile-web-app-capable" content="yes">
77
<meta name="viewport"
88
content="width=device-width, initial-scale=1.0, shrink-to-fit=no, maximum-scale=1.0, user-scalable=no">
99
<link rel="manifest" href="./manifest.json">
@@ -61,6 +61,7 @@
6161
importShim.addImportMap(importMap);
6262
</script>
6363

64+
<link rel="stylesheet" href="./node_modules/monaco-editor/min/vs/editor/editor.main.css">
6465
<link rel="stylesheet" href="./node_modules/dock-spawn-ts/lib/css/dock-manager-context-menu.css">
6566

6667
<!-- Drag/Drop support on touch devices -->
@@ -85,8 +86,16 @@
8586
-->
8687

8788
<script type="module">
89+
window.MonacoEnvironment = {
90+
getWorker: function (moduleId, label) {
91+
if (label === 'editorWorkerService')
92+
return new Worker('/node_modules/monaco-editor/esm/vs/editor/editor.worker.js', { type: 'module' });
93+
return new Worker(`/node_modules/monaco-editor/esm/vs/editor/${label}.worker.js`, { type: 'module' });
94+
}
95+
};
96+
8897
let { CodeViewMonaco } = await importShim('@node-projects/web-component-designer-codeview-monaco');
89-
await CodeViewMonaco.loadMonacoEditorViaImport();
98+
await CodeViewMonaco.getMonacoLib();
9099

91100
await importShim('dock-spawn-ts');
92101
await importShim('@node-projects/web-component-designer');
@@ -231,7 +240,9 @@
231240
src="./node_modules/@node-projects/web-component-designer/assets/icons/redo.svg"></button>
232241
<button style="margin-left: 10px;" data-command="screenshot"><img
233242
src="./node_modules/@node-projects/web-component-designer/assets/icons/screenshot.svg"></button>
234-
<button style="margin-left: 10px; color: inherit; font-family: monospace; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase;" data-command="collaboration" title="collaboration">collab</button>
243+
<button
244+
style="margin-left: 10px; color: inherit; font-family: monospace; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase;"
245+
data-command="collaboration" title="collaboration">collab</button>
235246
<button style="margin-left: 10px;" data-command="github"><img
236247
src="./node_modules/@node-projects/web-component-designer/assets/icons/github.svg"></button>
237248
</nav>

package-lock.json

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
"@node-projects/acad-ts": "^2.3.0",
3333
"@node-projects/base-custom-webcomponent": "0.46.0",
3434
"@node-projects/css-parser": "^5.2.0",
35-
"@node-projects/layout2vector": "^5.14.0",
35+
"@node-projects/layout2vector": "^5.16.0",
3636
"@node-projects/lean-he-esm": "^3.4.1",
3737
"@node-projects/node-html-parser-esm": "^6.4.1",
3838
"@node-projects/pickr-webcomponent": "^1.2.0",
39-
"@node-projects/web-component-designer": "^0.2.3",
39+
"@node-projects/web-component-designer": "^0.2.7",
4040
"@node-projects/web-component-designer-codeview-monaco": "^0.1.33",
41-
"@node-projects/web-component-designer-collaboration-service": "^0.1.5",
41+
"@node-projects/web-component-designer-collaboration-service": "^0.2.0",
4242
"@node-projects/web-component-designer-htmlparserservice-nodehtmlparser": "^0.1.12",
4343
"@node-projects/web-component-designer-stylesheetservice-css-parser": "^0.1.4",
4444
"@node-projects/web-component-designer-widgets-wunderbaum": "^0.2.1",

src/appShell.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { CssParserStylesheetService } from '@node-projects/web-component-designe
77

88
import '@node-projects/web-component-designer-widgets-wunderbaum';
99
import { PaletteTreeView, BindableObjectsBrowser, TreeViewExtended, ExpandCollapseContextMenu } from '@node-projects/web-component-designer-widgets-wunderbaum';
10-
import { DemoEditorTypesService } from './services/DemoEditorTypesService.js';
10+
import { DemoPropertyEditorTypesService } from './services/DemoPropertyEditorTypesService.js';
11+
import { DemoEditorTypeService } from './services/DemoEditorTypeService.js';
1112

1213
let serviceContainer = createDefaultServiceContainer();
1314
import { defaultWebRtcTabCollaborationSignalingChannels, setupCollaborationService, WebRtcTabCollaborationSignalingChannelKind, WebRtcTabCollaborationTransport } from '@node-projects/web-component-designer-collaboration-service';
@@ -22,7 +23,8 @@ serviceContainer.register("bindableObjectsService", new CustomBindableObjectsSer
2223
serviceContainer.registerLast("propertyService", new UnkownElementsPropertiesService());
2324
serviceContainer.register("refactorService", new BindingsRefactorService());
2425
serviceContainer.register("refactorService", new TextRefactorService());
25-
serviceContainer.register("editorTypesService", new DemoEditorTypesService());
26+
serviceContainer.register("propertyEditorTypesService", new DemoPropertyEditorTypesService());
27+
serviceContainer.register("editorTypeService", new DemoEditorTypeService());
2628

2729
serviceContainer.designerExtensions.set(ExtensionType.PrimarySelection, [
2830
...serviceContainer.designerExtensions.get(ExtensionType.PrimarySelection) ?? [],
@@ -641,7 +643,7 @@ export class AppShell extends BaseCustomWebComponentConstructorAppend {
641643
this._propertyGrid.serviceContainer = serviceContainer;
642644
}
643645

644-
public newDocument(fixedWidth: boolean, code?: string, style?: string) {
646+
public async newDocument(fixedWidth: boolean, code?: string, style?: string) {
645647
this._documentNumber++;
646648
let sampleDocument = new DocumentContainer(serviceContainer);
647649
sampleDocument.setAttribute('dock-spawn-panel-type', 'document');
@@ -654,7 +656,7 @@ export class AppShell extends BaseCustomWebComponentConstructorAppend {
654656
}` : style
655657
}
656658
];
657-
const model = this._styleEditor.createModel(sampleDocument.additionalStylesheets[0].content);
659+
const model = await this._styleEditor.createModel(sampleDocument.additionalStylesheets[0].content);
658660
sampleDocument.additionalData = { model: model };
659661

660662
let timer;

src/services/DemoColorEditor.ts

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { showPopup, ValueType, w3color } from "@node-projects/web-component-designer";
2+
import "@node-projects/pickr-webcomponent";
3+
import { BaseCustomWebComponentConstructorAppend } from "@node-projects/base-custom-webcomponent";
4+
5+
type PickrRgba = [number, number, number, number] & {
6+
toString: (precision?: number) => string;
7+
};
8+
9+
type PickrColor = {
10+
toRGBA(): PickrRgba;
11+
};
12+
13+
type PickrSaveEvent = CustomEvent<{
14+
value: PickrColor | null;
15+
}>;
16+
17+
type PickrElement = HTMLElement & {
18+
config: {
19+
default?: string;
20+
swatches?: string[];
21+
components?: {
22+
preview?: boolean;
23+
opacity?: boolean;
24+
hue?: boolean;
25+
interaction?: {
26+
hex?: boolean;
27+
rgba?: boolean;
28+
hsla?: boolean;
29+
hsva?: boolean;
30+
input?: boolean;
31+
cancel?: boolean;
32+
save?: boolean;
33+
};
34+
};
35+
};
36+
setColor(value: string | null, silent?: boolean): boolean;
37+
};
38+
39+
export class DemoColorEditor extends BaseCustomWebComponentConstructorAppend{
40+
private static readonly _defaultColor = '#000000';
41+
42+
private _colorValue: string | null = null;
43+
private _valueLabel: HTMLSpanElement;
44+
private _swatchFill: HTMLSpanElement;
45+
private static _closePopup?: () => void;
46+
47+
constructor(private changedCallback: (newValue: any) => void) {
48+
super();
49+
50+
const element = document.createElement('button');
51+
element.type = 'button';
52+
element.style.display = 'grid';
53+
element.style.gridTemplateColumns = '18px minmax(0, 1fr)';
54+
element.style.alignItems = 'center';
55+
element.style.gap = '8px';
56+
element.style.width = '100%';
57+
element.style.minHeight = '24px';
58+
element.style.padding = '2px 6px';
59+
element.style.border = '1px solid var(--input-border-color, #596c7a)';
60+
element.style.background = 'transparent';
61+
element.style.color = 'inherit';
62+
//element.style.cursor = property.readonly ? 'default' : 'pointer';
63+
element.style.textAlign = 'left';
64+
65+
const swatch = document.createElement('span');
66+
swatch.style.position = 'relative';
67+
swatch.style.display = 'block';
68+
swatch.style.width = '16px';
69+
swatch.style.height = '16px';
70+
swatch.style.borderRadius = '3px';
71+
swatch.style.overflow = 'hidden';
72+
swatch.style.border = '1px solid gray';
73+
74+
this._swatchFill = document.createElement('span');
75+
this._swatchFill.style.position = 'absolute';
76+
this._swatchFill.style.inset = '0';
77+
swatch.appendChild(this._swatchFill);
78+
79+
this._valueLabel = document.createElement('span');
80+
this._valueLabel.style.overflow = 'hidden';
81+
this._valueLabel.style.textOverflow = 'ellipsis';
82+
this._valueLabel.style.whiteSpace = 'nowrap';
83+
84+
element.append(swatch, this._valueLabel);
85+
86+
//if (property.readonly)
87+
// element.disabled = true;
88+
//else
89+
element.onclick = () => { void this._openPopup(); };
90+
91+
this.shadowRoot.appendChild(element);
92+
this._applyDisplayValue(null);
93+
}
94+
95+
refreshValue(valueType: ValueType, value: any) {
96+
this._colorValue = value ? String(value) : null;
97+
this._applyDisplayValue(this._colorValue);
98+
}
99+
100+
private async _openPopup() {
101+
const pickrElement = this.ownerDocument.createElement('pickr-classic') as PickrElement;
102+
pickrElement.config = {
103+
default: this._colorValue ?? DemoColorEditor._defaultColor,
104+
components: {
105+
preview: true,
106+
opacity: true,
107+
hue: true,
108+
interaction: {
109+
hex: true,
110+
rgba: true,
111+
hsla: true,
112+
hsva: true,
113+
input: true,
114+
cancel: true,
115+
save: true
116+
}
117+
}
118+
};
119+
120+
//pickrElement.setColor(this._colorValue ?? TsColorPropertyEditor._defaultColor, true);
121+
pickrElement.addEventListener('save', (event) => void this._handlePickrSave(event as PickrSaveEvent));
122+
pickrElement.addEventListener('cancel', () => { void this._closePopup(true); });
123+
124+
DemoColorEditor._closePopup = showPopup(pickrElement, this, () => { void this._closePopup(true); });
125+
}
126+
127+
private async _handlePickrSave(event: PickrSaveEvent) {
128+
let nextValue
129+
if (event.detail.value !== null) {
130+
nextValue = this._formatColor(event.detail.value);
131+
}
132+
this._colorValue = <any>nextValue;
133+
this._applyDisplayValue(nextValue!);
134+
this.changedCallback(nextValue);
135+
await this._closePopup(false);
136+
}
137+
138+
public get value() {
139+
return this._colorValue;
140+
}
141+
public set value(v: string | null) {
142+
this._colorValue = v;
143+
this._applyDisplayValue(v);
144+
}
145+
146+
private async _closePopup(removePreview: boolean) {
147+
DemoColorEditor._closePopup?.();
148+
DemoColorEditor._closePopup = undefined;
149+
}
150+
151+
152+
private _applyDisplayValue(value: string | null) {
153+
const displayValue = value ?? DemoColorEditor._defaultColor;
154+
this._valueLabel.textContent = value ?? DemoColorEditor._defaultColor;
155+
this._valueLabel.title = value ?? DemoColorEditor._defaultColor;
156+
this._swatchFill.style.background = displayValue;
157+
}
158+
159+
private _formatColor(color: PickrColor) {
160+
const rgba = color.toRGBA();
161+
if (rgba[3] < 0.999)
162+
return rgba.toString(3);
163+
try {
164+
return w3color.toColorObject(rgba.toString()).toNameOrHexString();
165+
} catch {
166+
return rgba.toString();
167+
}
168+
}
169+
}
170+
customElements.define('demo-color-editor', DemoColorEditor);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { IEditorTypeService } from "@node-projects/web-component-designer";
2+
import { DemoColorEditor } from "./DemoColorEditor.js";
3+
4+
export class DemoEditorTypeService implements IEditorTypeService {
5+
getEditor(type: string, additional: { changedCallback: (newValue: any) => void; }): { element: HTMLElement; getValue: () => any; setValue: (value: any) => void; } {
6+
if (type === 'color') {
7+
const editor = new DemoColorEditor(additional.changedCallback);
8+
return {
9+
element: editor,
10+
getValue: () => editor.value,
11+
setValue: (value: any) => { editor.value = value; }
12+
};
13+
}
14+
return null;
15+
}
16+
}

src/services/DemoEditorTypesService.ts renamed to src/services/DemoPropertyEditorTypesService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { IEditorTypesService, IProperty, IPropertyEditor } from "@node-projects/web-component-designer";
1+
import { IPropertyEditorTypesService, IProperty, IPropertyEditor } from "@node-projects/web-component-designer";
22
import { DemoColorPropertyEditor } from "./DemoColorPropertyEditor.js";
33

4-
export class DemoEditorTypesService implements IEditorTypesService {
4+
export class DemoPropertyEditorTypesService implements IPropertyEditorTypesService {
55
getEditorForProperty(property: IProperty): IPropertyEditor | null {
66
if (property.createEditor)
77
return property.createEditor(property);

0 commit comments

Comments
 (0)