@astl/editor 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +96 -0
- package/LICENSE +202 -0
- package/README.md +43 -0
- package/dist/authoring.d.ts +284 -0
- package/dist/authoring.d.ts.map +1 -0
- package/dist/authoring.js +566 -0
- package/dist/authoring.js.map +1 -0
- package/dist/define.d.ts +8 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +35 -0
- package/dist/define.js.map +1 -0
- package/dist/edit.d.ts +45 -0
- package/dist/edit.d.ts.map +1 -0
- package/dist/edit.js +150 -0
- package/dist/edit.js.map +1 -0
- package/dist/elements/base.d.ts +31 -0
- package/dist/elements/base.d.ts.map +1 -0
- package/dist/elements/base.js +75 -0
- package/dist/elements/base.js.map +1 -0
- package/dist/elements/color-control.d.ts +25 -0
- package/dist/elements/color-control.d.ts.map +1 -0
- package/dist/elements/color-control.js +139 -0
- package/dist/elements/color-control.js.map +1 -0
- package/dist/elements/help-form.d.ts +31 -0
- package/dist/elements/help-form.d.ts.map +1 -0
- package/dist/elements/help-form.js +371 -0
- package/dist/elements/help-form.js.map +1 -0
- package/dist/elements/insert-menu.d.ts +79 -0
- package/dist/elements/insert-menu.d.ts.map +1 -0
- package/dist/elements/insert-menu.js +362 -0
- package/dist/elements/insert-menu.js.map +1 -0
- package/dist/elements/inspector.d.ts +24 -0
- package/dist/elements/inspector.d.ts.map +1 -0
- package/dist/elements/inspector.js +846 -0
- package/dist/elements/inspector.js.map +1 -0
- package/dist/elements/issue-list.d.ts +18 -0
- package/dist/elements/issue-list.d.ts.map +1 -0
- package/dist/elements/issue-list.js +85 -0
- package/dist/elements/issue-list.js.map +1 -0
- package/dist/elements/issues.d.ts +24 -0
- package/dist/elements/issues.d.ts.map +1 -0
- package/dist/elements/issues.js +81 -0
- package/dist/elements/issues.js.map +1 -0
- package/dist/elements/picture-dialog.d.ts +40 -0
- package/dist/elements/picture-dialog.d.ts.map +1 -0
- package/dist/elements/picture-dialog.js +433 -0
- package/dist/elements/picture-dialog.js.map +1 -0
- package/dist/elements/picture-field.d.ts +33 -0
- package/dist/elements/picture-field.d.ts.map +1 -0
- package/dist/elements/picture-field.js +692 -0
- package/dist/elements/picture-field.js.map +1 -0
- package/dist/elements/preview.d.ts +34 -0
- package/dist/elements/preview.d.ts.map +1 -0
- package/dist/elements/preview.js +165 -0
- package/dist/elements/preview.js.map +1 -0
- package/dist/elements/style-form.d.ts +30 -0
- package/dist/elements/style-form.d.ts.map +1 -0
- package/dist/elements/style-form.js +640 -0
- package/dist/elements/style-form.js.map +1 -0
- package/dist/elements/style-load.d.ts +26 -0
- package/dist/elements/style-load.d.ts.map +1 -0
- package/dist/elements/style-load.js +50 -0
- package/dist/elements/style-load.js.map +1 -0
- package/dist/elements/style-popups.d.ts +39 -0
- package/dist/elements/style-popups.d.ts.map +1 -0
- package/dist/elements/style-popups.js +173 -0
- package/dist/elements/style-popups.js.map +1 -0
- package/dist/elements/style-rules.d.ts +21 -0
- package/dist/elements/style-rules.d.ts.map +1 -0
- package/dist/elements/style-rules.js +274 -0
- package/dist/elements/style-rules.js.map +1 -0
- package/dist/elements/styles.d.ts +9 -0
- package/dist/elements/styles.d.ts.map +1 -0
- package/dist/elements/styles.js +161 -0
- package/dist/elements/styles.js.map +1 -0
- package/dist/elements/toolbar.d.ts +65 -0
- package/dist/elements/toolbar.d.ts.map +1 -0
- package/dist/elements/toolbar.js +1270 -0
- package/dist/elements/toolbar.js.map +1 -0
- package/dist/help.d.ts +33 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +111 -0
- package/dist/help.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/inspect.d.ts +251 -0
- package/dist/inspect.d.ts.map +1 -0
- package/dist/inspect.js +428 -0
- package/dist/inspect.js.map +1 -0
- package/dist/messages/en.json +252 -0
- package/dist/messages/help/en.json +18 -0
- package/dist/messages/help/pl.json +18 -0
- package/dist/messages/pl.json +252 -0
- package/dist/messages/style/en.json +67 -0
- package/dist/messages/style/pl.json +67 -0
- package/dist/messages.d.ts +5 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +9 -0
- package/dist/messages.js.map +1 -0
- package/dist/pages.d.ts +28 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +147 -0
- package/dist/pages.js.map +1 -0
- package/dist/scan.d.ts +82 -0
- package/dist/scan.d.ts.map +1 -0
- package/dist/scan.js +329 -0
- package/dist/scan.js.map +1 -0
- package/dist/style-messages.d.ts +7 -0
- package/dist/style-messages.d.ts.map +1 -0
- package/dist/style-messages.js +12 -0
- package/dist/style-messages.js.map +1 -0
- package/dist/style-model.d.ts +50 -0
- package/dist/style-model.d.ts.map +1 -0
- package/dist/style-model.js +76 -0
- package/dist/style-model.js.map +1 -0
- package/dist/style-placing.d.ts +38 -0
- package/dist/style-placing.d.ts.map +1 -0
- package/dist/style-placing.js +98 -0
- package/dist/style-placing.js.map +1 -0
- package/dist/textarea.d.ts +17 -0
- package/dist/textarea.d.ts.map +1 -0
- package/dist/textarea.js +175 -0
- package/dist/textarea.js.map +1 -0
- package/package.json +42 -4
- package/src/authoring.ts +896 -0
- package/src/define.ts +34 -0
- package/src/edit.ts +195 -0
- package/src/elements/base.ts +93 -0
- package/src/elements/color-control.ts +159 -0
- package/src/elements/help-form.ts +404 -0
- package/src/elements/insert-menu.ts +481 -0
- package/src/elements/inspector.ts +941 -0
- package/src/elements/issue-list.ts +103 -0
- package/src/elements/issues.ts +89 -0
- package/src/elements/picture-dialog.ts +461 -0
- package/src/elements/picture-field.ts +770 -0
- package/src/elements/preview.ts +186 -0
- package/src/elements/style-form.ts +751 -0
- package/src/elements/style-load.ts +64 -0
- package/src/elements/style-popups.ts +257 -0
- package/src/elements/style-rules.ts +294 -0
- package/src/elements/styles.ts +162 -0
- package/src/elements/toolbar.ts +1386 -0
- package/src/help.ts +156 -0
- package/src/index.ts +81 -0
- package/src/inspect.ts +774 -0
- package/src/messages/en.json +252 -0
- package/src/messages/help/en.json +18 -0
- package/src/messages/help/pl.json +18 -0
- package/src/messages/pl.json +252 -0
- package/src/messages/style/en.json +67 -0
- package/src/messages/style/pl.json +67 -0
- package/src/messages.ts +11 -0
- package/src/pages.ts +158 -0
- package/src/scan.ts +382 -0
- package/src/style-messages.ts +13 -0
- package/src/style-model.ts +125 -0
- package/src/style-placing.ts +117 -0
- package/src/textarea.ts +182 -0
package/src/define.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AstlInsertMenu } from './elements/insert-menu.js';
|
|
2
|
+
import { AstlInspector } from './elements/inspector.js';
|
|
3
|
+
import { AstlIssues } from './elements/issues.js';
|
|
4
|
+
import { AstlPictureDialog } from './elements/picture-dialog.js';
|
|
5
|
+
import { AstlPictureField } from './elements/picture-field.js';
|
|
6
|
+
import { AstlPreview } from './elements/preview.js';
|
|
7
|
+
import { styleElementsIn } from './elements/style-load.js';
|
|
8
|
+
import { AstlToolbar } from './elements/toolbar.js';
|
|
9
|
+
|
|
10
|
+
const ELEMENTS: readonly (readonly [string, CustomElementConstructor])[] = [
|
|
11
|
+
['astl-insert-menu', AstlInsertMenu],
|
|
12
|
+
['astl-inspector', AstlInspector],
|
|
13
|
+
['astl-issues', AstlIssues],
|
|
14
|
+
['astl-preview', AstlPreview],
|
|
15
|
+
['astl-toolbar', AstlToolbar],
|
|
16
|
+
['astl-picture-dialog', AstlPictureDialog],
|
|
17
|
+
['astl-picture-field', AstlPictureField],
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Defines `<astl-toolbar>`, `<astl-insert-menu>`, `<astl-inspector>`, `<astl-issues>` and `<astl-preview>`, and the
|
|
22
|
+
* two they use for pictures, `<astl-picture-dialog>` and `<astl-picture-field>` (nothing is defined on import;
|
|
23
|
+
* `<astl-style-form>` and `<astl-style-rules>` come with `loadStyleElements`, on first use). Safe to call again; a
|
|
24
|
+
* tag already defined by another class throws, as two copies of the kit would otherwise mix silently.
|
|
25
|
+
*/
|
|
26
|
+
export function defineAuthoringElements(registry: CustomElementRegistry = customElements): void {
|
|
27
|
+
styleElementsIn(registry);
|
|
28
|
+
for (const [tag, element] of ELEMENTS) {
|
|
29
|
+
const existing = registry.get(tag);
|
|
30
|
+
if (existing === element) continue;
|
|
31
|
+
if (existing !== undefined) throw new Error(`<${tag}> is already defined by another class`);
|
|
32
|
+
registry.define(tag, element);
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/edit.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal text edits on one element's source HTML: set or remove an attribute, replace repeated text children, toggle
|
|
3
|
+
* a flag or set a value on a child. Everything the change does not touch stays byte-identical (formatting, quotes,
|
|
4
|
+
* other children).
|
|
5
|
+
*/
|
|
6
|
+
import { isElement, textContent } from '@astl/core';
|
|
7
|
+
import { parseHtml } from '@astl/engine';
|
|
8
|
+
import {
|
|
9
|
+
applyEdits,
|
|
10
|
+
attributeValue,
|
|
11
|
+
type Edit,
|
|
12
|
+
type ElementRange,
|
|
13
|
+
elementTree,
|
|
14
|
+
escapeAttribute,
|
|
15
|
+
escapeText,
|
|
16
|
+
spaceBefore,
|
|
17
|
+
type TagToken,
|
|
18
|
+
} from './scan.js';
|
|
19
|
+
|
|
20
|
+
/** What an inspector control changes. Indexes count children with the same tag, from 0. */
|
|
21
|
+
export type InspectorChange =
|
|
22
|
+
| {
|
|
23
|
+
readonly kind: 'attribute';
|
|
24
|
+
readonly name: string;
|
|
25
|
+
/** A string sets the value, `true` writes a bare boolean attribute, `false` or null removes it. */
|
|
26
|
+
readonly value: string | boolean | null;
|
|
27
|
+
}
|
|
28
|
+
| { readonly kind: 'text-list'; readonly tag: string; readonly values: readonly string[] }
|
|
29
|
+
| {
|
|
30
|
+
readonly kind: 'child-flag';
|
|
31
|
+
readonly tag: string;
|
|
32
|
+
readonly attribute: string;
|
|
33
|
+
readonly index: number;
|
|
34
|
+
readonly value: boolean;
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
readonly kind: 'child-choice';
|
|
38
|
+
readonly tag: string;
|
|
39
|
+
readonly attribute: string;
|
|
40
|
+
readonly index: number;
|
|
41
|
+
/** The value to write; null removes the attribute. */
|
|
42
|
+
readonly value: string | null;
|
|
43
|
+
}
|
|
44
|
+
| {
|
|
45
|
+
/** Several attributes of one child (a zone's shape and coordinates): a string sets one, null removes it. */
|
|
46
|
+
readonly kind: 'child-attributes';
|
|
47
|
+
readonly tag: string;
|
|
48
|
+
readonly index: number;
|
|
49
|
+
readonly attributes: Readonly<Record<string, string | null>>;
|
|
50
|
+
}
|
|
51
|
+
/** A new empty child with these attributes, after the last child of its tag (or at the end). */
|
|
52
|
+
| {
|
|
53
|
+
readonly kind: 'child-add';
|
|
54
|
+
readonly tag: string;
|
|
55
|
+
readonly attributes: Readonly<Record<string, string>>;
|
|
56
|
+
}
|
|
57
|
+
/** Removes one child, its content included. */
|
|
58
|
+
| { readonly kind: 'child-remove'; readonly tag: string; readonly index: number };
|
|
59
|
+
|
|
60
|
+
const NAME = /^[a-z][a-z0-9-]*$/;
|
|
61
|
+
|
|
62
|
+
function attributeEdits(source: string, tag: TagToken, name: string, value: string | boolean | null): Edit[] {
|
|
63
|
+
if (!NAME.test(name)) throw new TypeError(`Invalid attribute name "${name}"`);
|
|
64
|
+
const written = tag.attributes.filter((a) => a.name === name);
|
|
65
|
+
const first = written[0];
|
|
66
|
+
if (value === null || value === false) {
|
|
67
|
+
return written.map((a) => ({ start: spaceBefore(source, a.start), end: a.end, text: '' }));
|
|
68
|
+
}
|
|
69
|
+
if (first !== undefined) {
|
|
70
|
+
const current = attributeValue(tag, name) ?? '';
|
|
71
|
+
const same = value === true ? current === '' || current.toLowerCase() === name : current === value;
|
|
72
|
+
if (same) return [];
|
|
73
|
+
}
|
|
74
|
+
const text = value === true ? name : `${name}="${escapeAttribute(value)}"`;
|
|
75
|
+
if (first !== undefined) return [{ start: first.start, end: first.end, text }];
|
|
76
|
+
const last = tag.attributes[tag.attributes.length - 1];
|
|
77
|
+
const at = last?.end ?? tag.start + 1 + tag.name.length;
|
|
78
|
+
return [{ start: at, end: at, text: ` ${text}` }];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The children with `tag` in document order, wherever they are in the element (as the inspector lists them). */
|
|
82
|
+
function childrenOf(root: ElementRange, tag: string): ElementRange[] {
|
|
83
|
+
return root.children.flatMap((c) => (c.name === tag ? [c] : childrenOf(c, tag)));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Text of an element as the HTML parser reads it. */
|
|
87
|
+
function textOf(source: string, range: ElementRange): string {
|
|
88
|
+
const [node] = parseHtml(source.slice(range.start, range.end));
|
|
89
|
+
return isElement(node) ? textContent(node) : '';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Where content ends: before the end tag, or where the element was implicitly closed. */
|
|
93
|
+
function contentEnd(range: ElementRange): number {
|
|
94
|
+
return range.endTag?.start ?? range.end;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Replaces the content of an element, turning `<x-y/>` into `<x-y>...</x-y>`. */
|
|
98
|
+
function contentEdit(source: string, range: ElementRange, html: string): Edit {
|
|
99
|
+
const tag = range.startTag;
|
|
100
|
+
if (tag.selfClosing && range.endTag === null) {
|
|
101
|
+
const open = source.slice(tag.start, spaceBefore(source, tag.end - 2));
|
|
102
|
+
return { start: range.start, end: range.end, text: `${open}>${html}</${range.name}>` };
|
|
103
|
+
}
|
|
104
|
+
return { start: tag.end, end: contentEnd(range), text: html };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Appends HTML at the end of an element's content. */
|
|
108
|
+
function appendEdit(source: string, range: ElementRange, html: string): Edit {
|
|
109
|
+
const tag = range.startTag;
|
|
110
|
+
if (tag.selfClosing && range.endTag === null) return contentEdit(source, range, html);
|
|
111
|
+
if (range.endTag === null) return { start: range.end, end: range.end, text: `${html}</${range.name}>` };
|
|
112
|
+
return { start: range.endTag.start, end: range.endTag.start, text: html };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Whitespace written between children, reused for new ones. */
|
|
116
|
+
function separator(source: string, root: ElementRange, children: readonly ElementRange[]): string {
|
|
117
|
+
const [a, b] = children;
|
|
118
|
+
if (a === undefined) return '';
|
|
119
|
+
const gap =
|
|
120
|
+
b === undefined
|
|
121
|
+
? source.slice(Math.max(root.startTag.end, spaceBefore(source, a.start)), a.start)
|
|
122
|
+
: source.slice(a.end, b.start);
|
|
123
|
+
return /^\s*$/.test(gap) ? gap : '';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function textListEdits(source: string, root: ElementRange, tag: string, values: readonly string[]): Edit[] {
|
|
127
|
+
const children = childrenOf(root, tag);
|
|
128
|
+
const edits: Edit[] = [];
|
|
129
|
+
children.forEach((child, i) => {
|
|
130
|
+
const value = values[i];
|
|
131
|
+
if (value === undefined) {
|
|
132
|
+
edits.push({ start: spaceBefore(source, child.start), end: child.end, text: '' });
|
|
133
|
+
} else if (textOf(source, child) !== value) {
|
|
134
|
+
edits.push(contentEdit(source, child, escapeText(value)));
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
if (values.length > children.length) {
|
|
138
|
+
const sep = separator(source, root, children);
|
|
139
|
+
const added = values.slice(children.length).map((v) => `<${tag}>${escapeText(v)}</${tag}>`);
|
|
140
|
+
const last = children[children.length - 1];
|
|
141
|
+
if (last === undefined) edits.push(appendEdit(source, root, added.join(sep)));
|
|
142
|
+
else edits.push({ start: last.end, end: last.end, text: added.map((a) => sep + a).join('') });
|
|
143
|
+
}
|
|
144
|
+
return edits;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Applies one inspector change to the source HTML of one element (the first element in `html`). */
|
|
148
|
+
export function updateElement(html: string, change: InspectorChange): string {
|
|
149
|
+
const root = elementTree(html)[0];
|
|
150
|
+
if (root === undefined) return html;
|
|
151
|
+
switch (change.kind) {
|
|
152
|
+
case 'attribute':
|
|
153
|
+
return applyEdits(html, attributeEdits(html, root.startTag, change.name, change.value));
|
|
154
|
+
case 'child-flag':
|
|
155
|
+
case 'child-choice': {
|
|
156
|
+
const child = childrenOf(root, change.tag)[change.index];
|
|
157
|
+
if (child === undefined) return html;
|
|
158
|
+
return applyEdits(html, attributeEdits(html, child.startTag, change.attribute, change.value));
|
|
159
|
+
}
|
|
160
|
+
case 'text-list':
|
|
161
|
+
return applyEdits(html, textListEdits(html, root, change.tag, change.values));
|
|
162
|
+
case 'child-attributes': {
|
|
163
|
+
let out = html;
|
|
164
|
+
for (const [attribute, value] of Object.entries(change.attributes)) {
|
|
165
|
+
out = updateElement(out, {
|
|
166
|
+
kind: 'child-choice',
|
|
167
|
+
tag: change.tag,
|
|
168
|
+
attribute,
|
|
169
|
+
index: change.index,
|
|
170
|
+
value,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
case 'child-add': {
|
|
176
|
+
if (!NAME.test(change.tag)) throw new TypeError(`Invalid tag name "${change.tag}"`);
|
|
177
|
+
const attributes = Object.entries(change.attributes).map(([name, value]) => {
|
|
178
|
+
if (!NAME.test(name)) throw new TypeError(`Invalid attribute name "${name}"`);
|
|
179
|
+
return ` ${name}="${escapeAttribute(value)}"`;
|
|
180
|
+
});
|
|
181
|
+
const added = `<${change.tag}${attributes.join('')}></${change.tag}>`;
|
|
182
|
+
const children = childrenOf(root, change.tag);
|
|
183
|
+
// After the last child of the tag, else after the last child: on its own line when the children are.
|
|
184
|
+
const last = children.at(-1) ?? root.children.at(-1);
|
|
185
|
+
if (last === undefined) return applyEdits(html, [appendEdit(html, root, added)]);
|
|
186
|
+
const sep = separator(html, root, children.length > 0 ? children : root.children);
|
|
187
|
+
return applyEdits(html, [{ start: last.end, end: last.end, text: sep + added }]);
|
|
188
|
+
}
|
|
189
|
+
case 'child-remove': {
|
|
190
|
+
const child = childrenOf(root, change.tag)[change.index];
|
|
191
|
+
if (child === undefined) return html;
|
|
192
|
+
return applyEdits(html, [{ start: spaceBefore(html, child.start), end: child.end, text: '' }]);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Direction,
|
|
3
|
+
directionOf,
|
|
4
|
+
type EditorAdapter,
|
|
5
|
+
type EditorSelection,
|
|
6
|
+
type MessageParams,
|
|
7
|
+
} from '@astl/core';
|
|
8
|
+
import { LitElement, type PropertyDeclarations, type PropertyValues } from 'lit';
|
|
9
|
+
import type { Authoring } from '../authoring.js';
|
|
10
|
+
import { EDITOR_NAMESPACE } from '../messages.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Base of the kit elements: `authoring` and `adapter` properties, a subscription to the adapter's events while
|
|
14
|
+
* connected (re-rendering by default) and UI strings in the authoring locale.
|
|
15
|
+
*/
|
|
16
|
+
export class AuthoringElement extends LitElement {
|
|
17
|
+
static override properties: PropertyDeclarations = {
|
|
18
|
+
authoring: { attribute: false },
|
|
19
|
+
adapter: { attribute: false },
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
declare authoring: Authoring | null;
|
|
23
|
+
declare adapter: EditorAdapter | null;
|
|
24
|
+
|
|
25
|
+
#subscription: { readonly adapter: EditorAdapter; readonly off: () => void } | null = null;
|
|
26
|
+
|
|
27
|
+
constructor() {
|
|
28
|
+
super();
|
|
29
|
+
this.authoring = null;
|
|
30
|
+
this.adapter = null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
override connectedCallback(): void {
|
|
34
|
+
super.connectedCallback();
|
|
35
|
+
this.#subscribe();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
override disconnectedCallback(): void {
|
|
39
|
+
super.disconnectedCallback();
|
|
40
|
+
this.#unsubscribe();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected override willUpdate(changed: PropertyValues<this>): void {
|
|
44
|
+
if (changed.has('adapter')) this.#subscribe();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#subscribe(): void {
|
|
48
|
+
const adapter = this.isConnected ? this.adapter : null;
|
|
49
|
+
if ((this.#subscription?.adapter ?? null) === adapter) return;
|
|
50
|
+
this.#unsubscribe();
|
|
51
|
+
if (adapter === null) return;
|
|
52
|
+
const offs = [
|
|
53
|
+
adapter.onChange(() => this.sourceChanged()),
|
|
54
|
+
adapter.onSelectionChange((selection) => this.selectionChanged(selection)),
|
|
55
|
+
adapter.onCursorChange?.(() => this.cursorChanged()) ?? (() => {}),
|
|
56
|
+
];
|
|
57
|
+
this.#subscription = {
|
|
58
|
+
adapter,
|
|
59
|
+
off: () => {
|
|
60
|
+
for (const off of offs) off();
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#unsubscribe(): void {
|
|
66
|
+
this.#subscription?.off();
|
|
67
|
+
this.#subscription = null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The document changed. */
|
|
71
|
+
protected sourceChanged(): void {
|
|
72
|
+
this.requestUpdate();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The cursor entered or left a component. */
|
|
76
|
+
protected selectionChanged(_selection: EditorSelection | null): void {
|
|
77
|
+
this.requestUpdate();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The cursor moved, or the document or its history changed. Most elements have nothing to do. */
|
|
81
|
+
protected cursorChanged(): void {}
|
|
82
|
+
|
|
83
|
+
/** A kit message (`astl.editor`) in the authoring locale. */
|
|
84
|
+
protected t(key: string, params?: MessageParams): string {
|
|
85
|
+
return this.authoring?.t(EDITOR_NAMESPACE, key, params) ?? key;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Language and direction of the UI strings. */
|
|
89
|
+
protected get ui(): { readonly lang: string; readonly dir: Direction } {
|
|
90
|
+
const lang = this.authoring?.locale ?? 'en';
|
|
91
|
+
return { lang, dir: directionOf(lang) };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { parseStyleColor } from '@astl/core/declarations';
|
|
2
|
+
import { type CSSResult, css, html, nothing, type TemplateResult, unsafeCSS } from 'lit';
|
|
3
|
+
import { PALETTE_SIZE } from '../style-model.js';
|
|
4
|
+
|
|
5
|
+
const PALETTE = Array.from({ length: PALETTE_SIZE }, (_, i) => i + 1);
|
|
6
|
+
|
|
7
|
+
/** The theme's colours first, each its own token: an author's colour that follows the theme by itself. */
|
|
8
|
+
export const colorStyles: CSSResult = css`
|
|
9
|
+
.swatches {
|
|
10
|
+
display: grid;
|
|
11
|
+
grid-template-columns: repeat(4, minmax(var(--astl-target-min), 1fr));
|
|
12
|
+
gap: var(--astl-space-1);
|
|
13
|
+
max-inline-size: 16rem;
|
|
14
|
+
}
|
|
15
|
+
button.swatch {
|
|
16
|
+
position: relative;
|
|
17
|
+
justify-content: center;
|
|
18
|
+
padding: 0;
|
|
19
|
+
border-color: var(--astl-outline);
|
|
20
|
+
forced-color-adjust: none;
|
|
21
|
+
}
|
|
22
|
+
button.swatch[aria-pressed='true'] {
|
|
23
|
+
outline: var(--astl-focus-width) solid var(--astl-ink);
|
|
24
|
+
outline-offset: calc(-1 * var(--astl-focus-width));
|
|
25
|
+
}
|
|
26
|
+
button.swatch[aria-pressed='true']::after {
|
|
27
|
+
content: '';
|
|
28
|
+
inline-size: 0.5rem;
|
|
29
|
+
block-size: 0.5rem;
|
|
30
|
+
border-radius: 50%;
|
|
31
|
+
border: var(--astl-outline-width) solid var(--astl-ink);
|
|
32
|
+
background: var(--astl-surface);
|
|
33
|
+
}
|
|
34
|
+
.custom {
|
|
35
|
+
display: grid;
|
|
36
|
+
grid-template-columns: var(--astl-target-min) minmax(0, 1fr);
|
|
37
|
+
gap: var(--astl-space-2);
|
|
38
|
+
align-items: end;
|
|
39
|
+
margin-block-start: var(--astl-space-2);
|
|
40
|
+
}
|
|
41
|
+
.custom label {
|
|
42
|
+
display: block;
|
|
43
|
+
grid-column: 1 / -1;
|
|
44
|
+
font-weight: 600;
|
|
45
|
+
}
|
|
46
|
+
.custom input[type='color'] {
|
|
47
|
+
padding: var(--astl-outline-width);
|
|
48
|
+
}
|
|
49
|
+
.color-none {
|
|
50
|
+
margin-block-start: var(--astl-space-2);
|
|
51
|
+
}
|
|
52
|
+
.color-error {
|
|
53
|
+
margin: var(--astl-space-2) 0 0;
|
|
54
|
+
padding: var(--astl-space-2);
|
|
55
|
+
border-inline-start: var(--astl-space-1) solid var(--astl-danger);
|
|
56
|
+
background: var(--astl-danger-soft);
|
|
57
|
+
}
|
|
58
|
+
${unsafeCSS(
|
|
59
|
+
PALETTE.flatMap((n) => [
|
|
60
|
+
`button.swatch[data-n='${n}'] { background: var(--astl-palette-${n}); }`,
|
|
61
|
+
`button.swatch[data-n='${n}'][data-soft] { background: var(--astl-palette-${n}-soft); }`,
|
|
62
|
+
]).join('\n'),
|
|
63
|
+
)}
|
|
64
|
+
`;
|
|
65
|
+
|
|
66
|
+
/** A colour as `#rrggbb`, or null for what has no such form (a theme's colour); alpha left out. */
|
|
67
|
+
function hexOf(value: string | null | undefined): string | null {
|
|
68
|
+
const parsed = value === null || value === undefined ? null : parseStyleColor(value);
|
|
69
|
+
if (parsed === null) return null;
|
|
70
|
+
const channel = (n: number) => Math.round(n).toString(16).padStart(2, '0');
|
|
71
|
+
return `#${channel(parsed[0])}${channel(parsed[1])}${channel(parsed[2])}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What a typed colour comes to: `#rrggbb` or `#rgb`, the `#` optional; null: not a colour. */
|
|
75
|
+
const typedColor = (text: string): string | null => {
|
|
76
|
+
const match = /^#?([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(text.trim());
|
|
77
|
+
return match === null ? null : `#${(match[1] as string).toLowerCase()}`;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export interface ColorControl {
|
|
81
|
+
/** Prefix of the control's ids, unique in its root. */
|
|
82
|
+
readonly id: string;
|
|
83
|
+
/** The colour now, as a style holds it, or null. */
|
|
84
|
+
readonly value: string | null;
|
|
85
|
+
/** A message of the kit. */
|
|
86
|
+
readonly t: (key: string, params?: Record<string, string | number>) => string;
|
|
87
|
+
/** Set when the last typed colour was refused. */
|
|
88
|
+
readonly invalid: boolean;
|
|
89
|
+
/** A colour text goes on (a background, a highlight): the palette's soft forms, which it stays legible on. */
|
|
90
|
+
readonly ground?: boolean;
|
|
91
|
+
/** A colour was picked (`value`), or removed (null). */
|
|
92
|
+
pick(value: string | null): void;
|
|
93
|
+
/** The typed colour was refused: say so on the next render. */
|
|
94
|
+
refuse(): void;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Picks a colour: the theme's palette, then a custom one with the native picker and a hex field, then none. Colours
|
|
99
|
+
* apply when committed (`change`): a picker's drag would otherwise be an undo step per move.
|
|
100
|
+
*/
|
|
101
|
+
export function colorControl(control: ColorControl): TemplateResult {
|
|
102
|
+
const { id, value, t, invalid, ground = false } = control;
|
|
103
|
+
const hex = hexOf(value);
|
|
104
|
+
const swatch = (n: number) => `var(--astl-palette-${n}${ground ? '-soft' : ''})`;
|
|
105
|
+
return html`<div class="swatches" role="group" aria-label=${t('style.color.theme')}>
|
|
106
|
+
${PALETTE.map(
|
|
107
|
+
(n) => html`<button
|
|
108
|
+
type="button"
|
|
109
|
+
class="swatch"
|
|
110
|
+
data-n=${n}
|
|
111
|
+
?data-soft=${ground}
|
|
112
|
+
data-color=${swatch(n)}
|
|
113
|
+
title=${t('style.color.swatch', { n })}
|
|
114
|
+
aria-label=${t('style.color.swatch', { n })}
|
|
115
|
+
aria-pressed=${String(value === swatch(n))}
|
|
116
|
+
@click=${() => control.pick(swatch(n))}
|
|
117
|
+
></button>`,
|
|
118
|
+
)}
|
|
119
|
+
</div>
|
|
120
|
+
<div class="custom">
|
|
121
|
+
<label for="${id}-hex">${t('style.color.custom')}</label>
|
|
122
|
+
<input
|
|
123
|
+
type="color"
|
|
124
|
+
aria-label=${t('style.color.picker')}
|
|
125
|
+
.value=${hex ?? '#000000'}
|
|
126
|
+
@change=${(event: Event) => control.pick((event.target as HTMLInputElement).value)}
|
|
127
|
+
/>
|
|
128
|
+
<input
|
|
129
|
+
id="${id}-hex"
|
|
130
|
+
type="text"
|
|
131
|
+
spellcheck="false"
|
|
132
|
+
autocomplete="off"
|
|
133
|
+
placeholder="#rrggbb"
|
|
134
|
+
.value=${hex ?? ''}
|
|
135
|
+
aria-invalid=${invalid ? 'true' : nothing}
|
|
136
|
+
aria-describedby=${invalid ? `${id}-error` : nothing}
|
|
137
|
+
@change=${(event: Event) => {
|
|
138
|
+
const text = (event.target as HTMLInputElement).value;
|
|
139
|
+
const typed = typedColor(text);
|
|
140
|
+
if (typed !== null) control.pick(typed);
|
|
141
|
+
else if (text.trim() === '') control.pick(null);
|
|
142
|
+
else control.refuse();
|
|
143
|
+
}}
|
|
144
|
+
@keydown=${(event: KeyboardEvent) => {
|
|
145
|
+
// In a popup, Enter must commit like a change, not leave a half-typed colour behind.
|
|
146
|
+
if (event.key === 'Enter') {
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
(event.target as HTMLInputElement).dispatchEvent(new Event('change', { bubbles: true }));
|
|
149
|
+
}
|
|
150
|
+
}}
|
|
151
|
+
/>
|
|
152
|
+
</div>
|
|
153
|
+
<div role="alert">${invalid ? html`<p class="color-error" id="${id}-error">${t('style.color.invalid')}</p>` : nothing}</div>
|
|
154
|
+
<button type="button" class="color-none" aria-disabled=${value === null ? 'true' : nothing} @click=${() => {
|
|
155
|
+
if (value !== null) control.pick(null);
|
|
156
|
+
}}>
|
|
157
|
+
${t('style.color.none')}
|
|
158
|
+
</button>`;
|
|
159
|
+
}
|