@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
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { EditorStyleTargetKind } from '@astl/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The event of a request for a style form or the rules (`detail.target`: the target at the cursor, or `rules`). It
|
|
5
|
+
* bubbles, is composed and can be cancelled: a host that shows them itself calls `preventDefault`, and the element that
|
|
6
|
+
* was asked shows nothing. Otherwise that element shows its default: the toolbar a popup, the inspector a form in place.
|
|
7
|
+
*/
|
|
8
|
+
export const STYLE_OPEN = 'astl-style-open';
|
|
9
|
+
|
|
10
|
+
/** What `astl-style-open` asks for. */
|
|
11
|
+
export interface StyleOpen {
|
|
12
|
+
readonly target: EditorStyleTargetKind | 'rules';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let registry: CustomElementRegistry | null = null;
|
|
16
|
+
let loading: Promise<void> | null = null;
|
|
17
|
+
|
|
18
|
+
/** Where the kit is defined: the style elements go there too. */
|
|
19
|
+
export function styleElementsIn(where: CustomElementRegistry): void {
|
|
20
|
+
registry = where;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Defines `<astl-style-form>` and `<astl-style-rules>` where the kit is defined, and resolves once they are. A module of
|
|
25
|
+
* their own, outside the editor's first load: the toolbar and the inspector call this on first use, a host that places
|
|
26
|
+
* either element itself calls it first.
|
|
27
|
+
*/
|
|
28
|
+
export function loadStyleElements(): Promise<void> {
|
|
29
|
+
loading ??= Promise.all([import('./style-form.js'), import('./style-rules.js')]).then(
|
|
30
|
+
([{ AstlStyleForm }, { AstlStyleRules }]) => {
|
|
31
|
+
const target = registry ?? customElements;
|
|
32
|
+
if (target.get('astl-style-form') === undefined) target.define('astl-style-form', AstlStyleForm);
|
|
33
|
+
if (target.get('astl-style-rules') === undefined) target.define('astl-style-rules', AstlStyleRules);
|
|
34
|
+
},
|
|
35
|
+
(error: unknown) => {
|
|
36
|
+
// A failed chunk load is not cached by every browser: the next use tries again.
|
|
37
|
+
loading = null;
|
|
38
|
+
throw error;
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
return loading;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let helping: Promise<void> | null = null;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Defines `<astl-help-form>` (the inspector's help for students) where the kit is defined, and resolves once it is:
|
|
48
|
+
* a module of its own, outside the editor's first load, like the style elements. The inspector calls this when it shows
|
|
49
|
+
* a graded element, the insert menu before it adds help.
|
|
50
|
+
*/
|
|
51
|
+
export function loadHelpForm(): Promise<typeof import('./help-form.js')> {
|
|
52
|
+
const module = import('./help-form.js');
|
|
53
|
+
helping ??= module.then(
|
|
54
|
+
({ AstlHelpForm }) => {
|
|
55
|
+
const target = registry ?? customElements;
|
|
56
|
+
if (target.get('astl-help-form') === undefined) target.define('astl-help-form', AstlHelpForm);
|
|
57
|
+
},
|
|
58
|
+
(error: unknown) => {
|
|
59
|
+
helping = null;
|
|
60
|
+
throw error;
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
return helping.then(() => module);
|
|
64
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The toolbar's popups of the style controls: the menus of values, the colours, and the panel with the form or the
|
|
3
|
+
* rules. Outside the editor's first load: the toolbar loads this module when one of them first opens, and draws them
|
|
4
|
+
* in its own root, as its other popups.
|
|
5
|
+
*/
|
|
6
|
+
import type { EditorAdapter, EditorStyleChanges, EditorStyleState } from '@astl/core';
|
|
7
|
+
import { type CSSResult, css, html, nothing, type TemplateResult } from 'lit';
|
|
8
|
+
import type { Authoring } from '../authoring.js';
|
|
9
|
+
import { type EditorFont, styleMapOf } from '../style-model.js';
|
|
10
|
+
import { colorControl, colorStyles } from './color-control.js';
|
|
11
|
+
import type { StyleOpen } from './style-load.js';
|
|
12
|
+
|
|
13
|
+
/** The popups of the style controls: menus of values, and the colours. */
|
|
14
|
+
export type StylePopup = 'font' | 'size' | 'color' | 'highlight' | 'align' | 'spacing';
|
|
15
|
+
|
|
16
|
+
/** The font sizes of the menu, in px. */
|
|
17
|
+
const FONT_SIZES: readonly number[] = [10, 11, 12, 14, 16, 18, 20, 24, 28, 32, 36, 48, 72];
|
|
18
|
+
const ALIGNS = ['start', 'center', 'end', 'justify'] as const;
|
|
19
|
+
const LINE_HEIGHTS: readonly string[] = ['1', '1.15', '1.5', '2'];
|
|
20
|
+
const BLOCK_SPACES: readonly (readonly [string, string])[] = [
|
|
21
|
+
['0px', 'none'],
|
|
22
|
+
['8px', 'small'],
|
|
23
|
+
['16px', 'medium'],
|
|
24
|
+
['24px', 'large'],
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
/** One value of a menu of values: what it writes, and its label. */
|
|
28
|
+
interface Choice {
|
|
29
|
+
readonly value: string | null;
|
|
30
|
+
readonly label: string;
|
|
31
|
+
/** A stack to show the label in, in a font menu. */
|
|
32
|
+
readonly font?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const stylePopupStyles: CSSResult = css`
|
|
36
|
+
${colorStyles}
|
|
37
|
+
.colors {
|
|
38
|
+
inline-size: min(16rem, calc(100vw - 2 * var(--astl-space-4)));
|
|
39
|
+
}
|
|
40
|
+
.panel {
|
|
41
|
+
inline-size: min(26rem, calc(100vw - 2 * var(--astl-space-4)));
|
|
42
|
+
max-block-size: min(40rem, calc(100vh - 2 * var(--astl-space-4)));
|
|
43
|
+
}
|
|
44
|
+
.panel astl-style-rules::part(root) {
|
|
45
|
+
padding: 0;
|
|
46
|
+
border: 0;
|
|
47
|
+
}
|
|
48
|
+
.panel astl-style-rules::part(title) {
|
|
49
|
+
display: none;
|
|
50
|
+
}
|
|
51
|
+
.panel-head {
|
|
52
|
+
display: flex;
|
|
53
|
+
align-items: center;
|
|
54
|
+
justify-content: space-between;
|
|
55
|
+
gap: var(--astl-space-2);
|
|
56
|
+
margin-block-end: var(--astl-space-2);
|
|
57
|
+
font-weight: 600;
|
|
58
|
+
}
|
|
59
|
+
`;
|
|
60
|
+
|
|
61
|
+
/** What a popup needs from its toolbar. */
|
|
62
|
+
export interface StylePopupContext {
|
|
63
|
+
/** Which one is open: `panel` shows the form of a target, or the rules. */
|
|
64
|
+
readonly popup: StylePopup | { readonly panel: StyleOpen['target'] };
|
|
65
|
+
readonly styles: EditorStyleState;
|
|
66
|
+
readonly fonts: readonly EditorFont[];
|
|
67
|
+
/** A typed colour was refused. */
|
|
68
|
+
readonly colorError: boolean;
|
|
69
|
+
readonly authoring: Authoring | null;
|
|
70
|
+
readonly adapter: EditorAdapter | null;
|
|
71
|
+
readonly t: (key: string, params?: Record<string, string | number>) => string;
|
|
72
|
+
/** On the selected text, then closes the popup. */
|
|
73
|
+
readonly setText: (changes: EditorStyleChanges) => void;
|
|
74
|
+
/** On each text block of the selection, then closes the popup. */
|
|
75
|
+
readonly setBlock: (changes: EditorStyleChanges) => void;
|
|
76
|
+
readonly refuseColor: () => void;
|
|
77
|
+
/** Closes the popup; the focus goes back to its button with `back`. */
|
|
78
|
+
readonly close: (back: boolean) => void;
|
|
79
|
+
/** The arrows, Home and End along a menu's buttons, as the toolbar's other menus. */
|
|
80
|
+
readonly menuKeydown: (event: KeyboardEvent) => void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A menu of values: one checked at a time, each writing what it says. */
|
|
84
|
+
function choiceMenu(
|
|
85
|
+
context: StylePopupContext,
|
|
86
|
+
popup: StylePopup,
|
|
87
|
+
groups: readonly {
|
|
88
|
+
readonly title?: string;
|
|
89
|
+
readonly choices: readonly Choice[];
|
|
90
|
+
readonly now: string | null;
|
|
91
|
+
}[],
|
|
92
|
+
choose: (choice: Choice, group: number) => void,
|
|
93
|
+
label: string,
|
|
94
|
+
): TemplateResult {
|
|
95
|
+
return html`<div
|
|
96
|
+
id=${popup}
|
|
97
|
+
class="menu"
|
|
98
|
+
popover="manual"
|
|
99
|
+
role="menu"
|
|
100
|
+
aria-label=${label}
|
|
101
|
+
part="style-menu"
|
|
102
|
+
@keydown=${context.menuKeydown}
|
|
103
|
+
>
|
|
104
|
+
${groups.map(
|
|
105
|
+
(group, at) => html`<div class="menu-section" role="group" aria-label=${group.title ?? nothing}>
|
|
106
|
+
${group.title === undefined ? nothing : html`<div class="menu-group" aria-hidden="true">${group.title}</div>`}
|
|
107
|
+
${group.choices.map(
|
|
108
|
+
(choice) => html`<button
|
|
109
|
+
type="button"
|
|
110
|
+
role="menuitemradio"
|
|
111
|
+
tabindex="-1"
|
|
112
|
+
data-value=${choice.value ?? ''}
|
|
113
|
+
aria-checked=${String(choice.value === group.now)}
|
|
114
|
+
data-font=${choice.font ?? nothing}
|
|
115
|
+
@click=${() => choose(choice, at)}
|
|
116
|
+
>
|
|
117
|
+
${choice.label}
|
|
118
|
+
</button>`,
|
|
119
|
+
)}
|
|
120
|
+
</div>`,
|
|
121
|
+
)}
|
|
122
|
+
</div>`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function colors(context: StylePopupContext, popup: 'color' | 'highlight'): TemplateResult {
|
|
126
|
+
const property = popup === 'color' ? 'color' : 'background-color';
|
|
127
|
+
return html`<div
|
|
128
|
+
id=${popup}
|
|
129
|
+
class="colors"
|
|
130
|
+
popover="manual"
|
|
131
|
+
role="dialog"
|
|
132
|
+
aria-label=${context.t(`toolbar.${popup}`)}
|
|
133
|
+
part="style-colors"
|
|
134
|
+
@keydown=${(event: KeyboardEvent) => {
|
|
135
|
+
if (event.key === 'Escape') {
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
context.close(true);
|
|
138
|
+
}
|
|
139
|
+
}}
|
|
140
|
+
@focusout=${(event: FocusEvent) => {
|
|
141
|
+
const next = event.relatedTarget as Node | null;
|
|
142
|
+
if (next !== null && !(event.currentTarget as HTMLElement).contains(next)) context.close(false);
|
|
143
|
+
}}
|
|
144
|
+
>
|
|
145
|
+
${colorControl({
|
|
146
|
+
id: popup,
|
|
147
|
+
value: context.styles.text[property] ?? null,
|
|
148
|
+
t: context.t,
|
|
149
|
+
invalid: context.colorError,
|
|
150
|
+
ground: popup === 'highlight',
|
|
151
|
+
pick: (value) => context.setText({ [property]: value }),
|
|
152
|
+
refuse: context.refuseColor,
|
|
153
|
+
})}
|
|
154
|
+
</div>`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The default of `astl-style-open`: the form or the rules in a popup. */
|
|
158
|
+
function panel(context: StylePopupContext, target: StyleOpen['target']): TemplateResult {
|
|
159
|
+
const { t, authoring, adapter } = context;
|
|
160
|
+
const title = t(target === 'rules' ? 'style.rules.title' : `style.target.${target}`);
|
|
161
|
+
return html`<div
|
|
162
|
+
id="panel"
|
|
163
|
+
class="panel"
|
|
164
|
+
popover="manual"
|
|
165
|
+
role="dialog"
|
|
166
|
+
tabindex="-1"
|
|
167
|
+
aria-label=${title}
|
|
168
|
+
part="style-panel"
|
|
169
|
+
>
|
|
170
|
+
<div class="panel-head">
|
|
171
|
+
<span>${title}</span>
|
|
172
|
+
<button type="button" data-close @click=${() => context.close(true)}>${t('style.close')}</button>
|
|
173
|
+
</div>
|
|
174
|
+
${
|
|
175
|
+
target === 'rules'
|
|
176
|
+
? html`<astl-style-rules .authoring=${authoring} .adapter=${adapter}></astl-style-rules>`
|
|
177
|
+
: html`<astl-style-form .authoring=${authoring} .adapter=${adapter} .target=${target}></astl-style-form>`
|
|
178
|
+
}
|
|
179
|
+
</div>`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The popup that is open. */
|
|
183
|
+
export function stylePopup(context: StylePopupContext): TemplateResult {
|
|
184
|
+
const { popup, styles, t, fonts, setText, setBlock } = context;
|
|
185
|
+
if (typeof popup === 'object') return panel(context, popup.panel);
|
|
186
|
+
if (popup === 'color' || popup === 'highlight') return colors(context, popup);
|
|
187
|
+
const { text } = styles;
|
|
188
|
+
const none = { value: null, label: t('toolbar.default') };
|
|
189
|
+
const block = styleMapOf(styles.targets.find((target) => target.kind === 'block')?.style ?? '');
|
|
190
|
+
const number = (value: string | undefined) =>
|
|
191
|
+
value === undefined ? null : String(Number.parseFloat(value));
|
|
192
|
+
if (popup === 'font')
|
|
193
|
+
return choiceMenu(
|
|
194
|
+
context,
|
|
195
|
+
'font',
|
|
196
|
+
[
|
|
197
|
+
{
|
|
198
|
+
choices: [
|
|
199
|
+
none,
|
|
200
|
+
...fonts.map((font) => ({ value: font.stack, label: font.name, font: font.stack })),
|
|
201
|
+
],
|
|
202
|
+
now: text['font-family'] ?? null,
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
(choice) => setText({ 'font-family': choice.value }),
|
|
206
|
+
t('toolbar.font.name'),
|
|
207
|
+
);
|
|
208
|
+
if (popup === 'size')
|
|
209
|
+
return choiceMenu(
|
|
210
|
+
context,
|
|
211
|
+
'size',
|
|
212
|
+
[
|
|
213
|
+
{
|
|
214
|
+
choices: [none, ...FONT_SIZES.map((n) => ({ value: `${n}px`, label: String(n) }))],
|
|
215
|
+
now: text['font-size'] ?? null,
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
(choice) => setText({ 'font-size': choice.value }),
|
|
219
|
+
t('toolbar.size.name'),
|
|
220
|
+
);
|
|
221
|
+
if (popup === 'align') {
|
|
222
|
+
const now = block.get('text-align');
|
|
223
|
+
return choiceMenu(
|
|
224
|
+
context,
|
|
225
|
+
'align',
|
|
226
|
+
[
|
|
227
|
+
{
|
|
228
|
+
choices: ALIGNS.map((value) => ({ value, label: t(`toolbar.align.${value}`) })),
|
|
229
|
+
now: now === 'left' ? 'start' : now === 'right' ? 'end' : (now ?? 'start'),
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
(choice) => setBlock({ 'text-align': choice.value === 'start' ? null : choice.value }),
|
|
233
|
+
t('toolbar.align'),
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
return choiceMenu(
|
|
237
|
+
context,
|
|
238
|
+
'spacing',
|
|
239
|
+
[
|
|
240
|
+
{
|
|
241
|
+
title: t('toolbar.spacing.line'),
|
|
242
|
+
choices: [none, ...LINE_HEIGHTS.map((value) => ({ value, label: value }))],
|
|
243
|
+
now: number(block.get('line-height')),
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
title: t('toolbar.spacing.after'),
|
|
247
|
+
choices: [
|
|
248
|
+
none,
|
|
249
|
+
...BLOCK_SPACES.map(([value, name]) => ({ value, label: t(`toolbar.spacing.${name}`) })),
|
|
250
|
+
],
|
|
251
|
+
now: block.has('margin-block-end') ? `${number(block.get('margin-block-end'))}px` : null,
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
(choice, group) => setBlock({ [group === 0 ? 'line-height' : 'margin-block-end']: choice.value }),
|
|
255
|
+
t('toolbar.spacing'),
|
|
256
|
+
);
|
|
257
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import type { EditorStyleRule } from '@astl/core';
|
|
2
|
+
import { MAX_STYLE_RULES } from '@astl/core/declarations';
|
|
3
|
+
import { type CSSResultGroup, css, html, nothing, type PropertyValues, type TemplateResult } from 'lit';
|
|
4
|
+
import { styleMessages } from '../style-messages.js';
|
|
5
|
+
import { styleNameFrom } from '../style-placing.js';
|
|
6
|
+
import { AuthoringElement } from './base.js';
|
|
7
|
+
import { inheritTokens, kitStyles } from './styles.js';
|
|
8
|
+
|
|
9
|
+
/** The tags of "Text styles": what authors call them, and the element each rule reaches. */
|
|
10
|
+
const TAGS: readonly (readonly [key: string, tag: string])[] = [
|
|
11
|
+
['paragraph', 'p'],
|
|
12
|
+
['heading', 'h3'],
|
|
13
|
+
['subheading', 'h4'],
|
|
14
|
+
['quote', 'blockquote'],
|
|
15
|
+
['list', 'li'],
|
|
16
|
+
['caption', 'figcaption'],
|
|
17
|
+
['picture', 'img'],
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `<astl-style-rules>`: the document's rules, each opening the form that edits it. "Text styles" holds a rule per tag
|
|
22
|
+
* (every paragraph, every heading of the exercise), "My styles" the named ones: add, rename, edit, delete. A deleted
|
|
23
|
+
* style leaves no class behind. Everything is one undo step, undone here or in the editor. A host places it (a view of
|
|
24
|
+
* its own, a card), with `authoring` and `adapter`, after `loadStyleElements`; the toolbar's style menu opens it in a
|
|
25
|
+
* popup otherwise.
|
|
26
|
+
*/
|
|
27
|
+
export class AstlStyleRules extends AuthoringElement {
|
|
28
|
+
static override styles: CSSResultGroup = [
|
|
29
|
+
kitStyles,
|
|
30
|
+
inheritTokens('astl-style-form'),
|
|
31
|
+
css`
|
|
32
|
+
h3 {
|
|
33
|
+
margin: 0;
|
|
34
|
+
font-size: 1rem;
|
|
35
|
+
}
|
|
36
|
+
li.row {
|
|
37
|
+
display: flex;
|
|
38
|
+
flex-direction: column;
|
|
39
|
+
gap: var(--astl-space-2);
|
|
40
|
+
}
|
|
41
|
+
.line {
|
|
42
|
+
display: flex;
|
|
43
|
+
flex-wrap: wrap;
|
|
44
|
+
align-items: center;
|
|
45
|
+
gap: var(--astl-space-1);
|
|
46
|
+
}
|
|
47
|
+
.line > .name {
|
|
48
|
+
flex: 1 1 8rem;
|
|
49
|
+
justify-content: flex-start;
|
|
50
|
+
}
|
|
51
|
+
.line > .name::after {
|
|
52
|
+
content: '';
|
|
53
|
+
margin-inline-start: auto;
|
|
54
|
+
border-block: 0.3em solid transparent;
|
|
55
|
+
border-inline-start: 0.45em solid currentColor;
|
|
56
|
+
}
|
|
57
|
+
.line > .name[aria-expanded='true']::after {
|
|
58
|
+
rotate: 90deg;
|
|
59
|
+
}
|
|
60
|
+
.line > input {
|
|
61
|
+
flex: 1 1 8rem;
|
|
62
|
+
inline-size: auto;
|
|
63
|
+
}
|
|
64
|
+
form {
|
|
65
|
+
display: grid;
|
|
66
|
+
grid-template-columns: minmax(0, 1fr) auto;
|
|
67
|
+
gap: var(--astl-space-2);
|
|
68
|
+
align-items: end;
|
|
69
|
+
}
|
|
70
|
+
form label {
|
|
71
|
+
grid-column: 1 / -1;
|
|
72
|
+
font-weight: 600;
|
|
73
|
+
}
|
|
74
|
+
.undo {
|
|
75
|
+
align-self: flex-start;
|
|
76
|
+
}
|
|
77
|
+
.summary {
|
|
78
|
+
color: var(--astl-ink-muted);
|
|
79
|
+
font-size: var(--astl-size-small);
|
|
80
|
+
}
|
|
81
|
+
`,
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
/** The rule whose form is open: `for:<tag>` or `name:<name>`. */
|
|
85
|
+
#open: string | null = null;
|
|
86
|
+
/** The named style being renamed. */
|
|
87
|
+
#renaming: string | null = null;
|
|
88
|
+
#newLabel = '';
|
|
89
|
+
|
|
90
|
+
protected override willUpdate(changed: PropertyValues<this>): void {
|
|
91
|
+
super.willUpdate(changed);
|
|
92
|
+
if (changed.has('authoring')) this.authoring?.addMessages(styleMessages);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#styles() {
|
|
96
|
+
return this.adapter?.style ?? null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#toggle(key: string): void {
|
|
100
|
+
this.#open = this.#open === key ? null : key;
|
|
101
|
+
this.requestUpdate();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
#form(rule: Pick<EditorStyleRule, 'name' | 'for'>): TemplateResult {
|
|
105
|
+
return html`<astl-style-form
|
|
106
|
+
.authoring=${this.authoring}
|
|
107
|
+
.adapter=${this.adapter}
|
|
108
|
+
.rule=${rule}
|
|
109
|
+
></astl-style-form>`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#add(): void {
|
|
113
|
+
const styles = this.#styles();
|
|
114
|
+
const label = this.#newLabel.trim();
|
|
115
|
+
if (styles === null || label === '') {
|
|
116
|
+
this.renderRoot.querySelector<HTMLInputElement>('#new-style')?.focus();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const taken = new Set(styles.rules().flatMap((rule) => rule.name ?? []));
|
|
120
|
+
const name = styleNameFrom(label, taken);
|
|
121
|
+
if (styles.setRule({ name, label, style: '' })) {
|
|
122
|
+
this.#newLabel = '';
|
|
123
|
+
this.#open = `name:${name}`;
|
|
124
|
+
}
|
|
125
|
+
this.requestUpdate();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#rename(rule: EditorStyleRule, text: string): void {
|
|
129
|
+
// Removing the field after Enter or Escape fires its change: that one is not a rename.
|
|
130
|
+
if (this.#renaming !== rule.name) return;
|
|
131
|
+
const label = text.trim();
|
|
132
|
+
if (label !== '') this.#styles()?.setRule({ ...rule, label });
|
|
133
|
+
this.#renaming = null;
|
|
134
|
+
this.requestUpdate();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
#delete(rule: EditorStyleRule): void {
|
|
138
|
+
this.#styles()?.removeRule({ ...(rule.name === undefined ? {} : { name: rule.name }) });
|
|
139
|
+
if (this.#open === `name:${rule.name}`) this.#open = null;
|
|
140
|
+
// The row and its button are gone: the focus goes to what is next to it for sure.
|
|
141
|
+
this.requestUpdate();
|
|
142
|
+
void this.updateComplete.then(() => this.renderRoot.querySelector<HTMLElement>('#new-style')?.focus());
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#namedRow(rule: EditorStyleRule): TemplateResult {
|
|
146
|
+
const key = `name:${rule.name}`;
|
|
147
|
+
const label = rule.label ?? rule.name ?? '';
|
|
148
|
+
const open = this.#open === key;
|
|
149
|
+
return html`<li class="row" data-style=${rule.name ?? ''}>
|
|
150
|
+
<div class="line">
|
|
151
|
+
${
|
|
152
|
+
this.#renaming === rule.name
|
|
153
|
+
? html`<input
|
|
154
|
+
type="text"
|
|
155
|
+
aria-label=${this.t('style.rules.renameField', { label })}
|
|
156
|
+
.value=${label}
|
|
157
|
+
@change=${(event: Event) => this.#rename(rule, (event.target as HTMLInputElement).value)}
|
|
158
|
+
@keydown=${(event: KeyboardEvent) => {
|
|
159
|
+
if (event.key === 'Enter') {
|
|
160
|
+
event.preventDefault();
|
|
161
|
+
this.#rename(rule, (event.target as HTMLInputElement).value);
|
|
162
|
+
} else if (event.key === 'Escape') {
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
this.#renaming = null;
|
|
165
|
+
this.requestUpdate();
|
|
166
|
+
}
|
|
167
|
+
}}
|
|
168
|
+
/>`
|
|
169
|
+
: html`<button
|
|
170
|
+
type="button"
|
|
171
|
+
class="name"
|
|
172
|
+
data-edit
|
|
173
|
+
aria-expanded=${String(open)}
|
|
174
|
+
aria-label=${this.t('style.rules.edit', { label })}
|
|
175
|
+
@click=${() => this.#toggle(key)}
|
|
176
|
+
>
|
|
177
|
+
${label}
|
|
178
|
+
</button>`
|
|
179
|
+
}
|
|
180
|
+
<button
|
|
181
|
+
type="button"
|
|
182
|
+
data-rename
|
|
183
|
+
aria-label=${this.t('style.rules.rename', { label })}
|
|
184
|
+
@click=${() => {
|
|
185
|
+
this.#renaming = rule.name ?? null;
|
|
186
|
+
this.requestUpdate();
|
|
187
|
+
void this.updateComplete.then(() =>
|
|
188
|
+
this.renderRoot.querySelector<HTMLInputElement>(`[data-style="${rule.name}"] input`)?.select(),
|
|
189
|
+
);
|
|
190
|
+
}}
|
|
191
|
+
>
|
|
192
|
+
${this.t('style.rules.renameShort')}
|
|
193
|
+
</button>
|
|
194
|
+
<button
|
|
195
|
+
type="button"
|
|
196
|
+
data-delete
|
|
197
|
+
aria-label=${this.t('style.rules.delete', { label })}
|
|
198
|
+
@click=${() => this.#delete(rule)}
|
|
199
|
+
>
|
|
200
|
+
${this.t('style.rules.deleteShort')}
|
|
201
|
+
</button>
|
|
202
|
+
</div>
|
|
203
|
+
${open ? this.#form({ ...(rule.name === undefined ? {} : { name: rule.name }) }) : nothing}
|
|
204
|
+
</li>`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
protected override render(): TemplateResult | typeof nothing {
|
|
208
|
+
if (this.authoring === null) return nothing;
|
|
209
|
+
const { lang, dir } = this.ui;
|
|
210
|
+
const styles = this.#styles();
|
|
211
|
+
if (styles === null)
|
|
212
|
+
return html`<p class="note" lang=${lang} dir=${dir}>${this.t('style.form.none')}</p>`;
|
|
213
|
+
const rules = styles.rules();
|
|
214
|
+
const named = rules.filter((rule) => rule.name !== undefined);
|
|
215
|
+
const full = rules.length >= MAX_STYLE_RULES;
|
|
216
|
+
return html`<section class="kit" part="root" lang=${lang} dir=${dir} aria-labelledby="rules-title">
|
|
217
|
+
<h2 class="title" id="rules-title" part="title">${this.t('style.rules.title')}</h2>
|
|
218
|
+
<h3 id="tags-title">${this.t('style.rules.tags')}</h3>
|
|
219
|
+
<ul role="list" aria-labelledby="tags-title">
|
|
220
|
+
${TAGS.map(([key, tag]) => {
|
|
221
|
+
const id = `for:${tag}`;
|
|
222
|
+
const open = this.#open === id;
|
|
223
|
+
const label = this.t(`style.tag.${key}`);
|
|
224
|
+
const own = rules.find((rule) => rule.for === tag)?.style;
|
|
225
|
+
return html`<li class="row" data-for=${tag}>
|
|
226
|
+
<div class="line">
|
|
227
|
+
<button
|
|
228
|
+
type="button"
|
|
229
|
+
class="name"
|
|
230
|
+
data-edit
|
|
231
|
+
aria-expanded=${String(open)}
|
|
232
|
+
aria-label=${this.t('style.rules.edit', { label })}
|
|
233
|
+
@click=${() => this.#toggle(id)}
|
|
234
|
+
>
|
|
235
|
+
${label}
|
|
236
|
+
</button>
|
|
237
|
+
</div>
|
|
238
|
+
${own === undefined ? nothing : html`<span class="summary">${own}</span>`}
|
|
239
|
+
${open ? this.#form({ for: tag }) : nothing}
|
|
240
|
+
</li>`;
|
|
241
|
+
})}
|
|
242
|
+
</ul>
|
|
243
|
+
<h3 id="named-title">${this.t('toolbar.myStyles')}</h3>
|
|
244
|
+
${
|
|
245
|
+
named.length === 0
|
|
246
|
+
? html`<p class="note">${this.t('style.rules.empty')}</p>`
|
|
247
|
+
: html`<ul role="list" aria-labelledby="named-title">${named.map((rule) => this.#namedRow(rule))}</ul>`
|
|
248
|
+
}
|
|
249
|
+
<form
|
|
250
|
+
@submit=${(event: Event) => {
|
|
251
|
+
event.preventDefault();
|
|
252
|
+
this.#add();
|
|
253
|
+
}}
|
|
254
|
+
>
|
|
255
|
+
<label for="new-style">${this.t('style.rules.newLabel')}</label>
|
|
256
|
+
<input
|
|
257
|
+
id="new-style"
|
|
258
|
+
type="text"
|
|
259
|
+
autocomplete="off"
|
|
260
|
+
.value=${this.#newLabel}
|
|
261
|
+
@input=${(event: Event) => {
|
|
262
|
+
this.#newLabel = (event.target as HTMLInputElement).value;
|
|
263
|
+
}}
|
|
264
|
+
/>
|
|
265
|
+
<button type="submit" data-add ?disabled=${full}>${this.t('style.rules.add')}</button>
|
|
266
|
+
</form>
|
|
267
|
+
${
|
|
268
|
+
this.adapter?.undo === undefined
|
|
269
|
+
? nothing
|
|
270
|
+
: html`<button
|
|
271
|
+
type="button"
|
|
272
|
+
class="undo"
|
|
273
|
+
data-undo
|
|
274
|
+
@click=${(event: Event) => {
|
|
275
|
+
const button = event.currentTarget as HTMLElement;
|
|
276
|
+
this.adapter?.undo?.();
|
|
277
|
+
// Undoing focuses the text a frame later; the author is still here, and in a host's view the text is
|
|
278
|
+
// not on show.
|
|
279
|
+
requestAnimationFrame(() => button.focus());
|
|
280
|
+
this.requestUpdate();
|
|
281
|
+
}}
|
|
282
|
+
>
|
|
283
|
+
${this.t('toolbar.undo')}
|
|
284
|
+
</button>`
|
|
285
|
+
}
|
|
286
|
+
</section>`;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
declare global {
|
|
291
|
+
interface HTMLElementTagNameMap {
|
|
292
|
+
'astl-style-rules': AstlStyleRules;
|
|
293
|
+
}
|
|
294
|
+
}
|