Repository navigation
Expand file tree
/
Copy pathexamples-basic.html
More file actions
349 lines (309 loc) · 17.4 KB
/
Copy pathexamples-basic.html
File metadata and controls
349 lines (309 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<meta name="kmwc:title" content="Basic Usage">
<meta name="kmwc:subtitle" content="The shipped <code><km-editor></code> element — each input as ONE live editor you drive with control switches: view mode & read-only, the I/O format lens, toolbar & status-bar placement, theme, and a declarative no-JavaScript setup, and a required field in a form.">
<meta name="kmwc:body-class" content="dark-output">
<!-- ============================================================= -->
<!-- BU01 · View mode & editing -->
<!-- ============================================================= -->
<div class="card">
<h2>BU01 · View Mode & Editing</h2>
<p>
One document, four presentations. <code>view-mode</code> switches between
<code>edit</code>, <code>split</code> (editor + live output), <code>preview</code>
(read-only render), and <code>inline</code>. <code>readonly</code> blocks input;
<code>preview</code> forces it on. The mount is persistent, so the document and
selection survive a mode switch.
</p>
<div class="controls">
<span><code>view-mode</code>:</span>
<label><input type="radio" name="eb01-mode" value="edit" checked> edit</label>
<label><input type="radio" name="eb01-mode" value="split"> split</label>
<label><input type="radio" name="eb01-mode" value="preview"> preview</label>
<label><input type="radio" name="eb01-mode" value="inline"> inline</label>
</div>
<div class="controls">
<label><input type="checkbox" id="eb01-readonly"> <code>readonly</code></label>
</div>
<div class="demo-area">
<km-editor id="eb01" view-mode="edit" content-format="markdown" toolbar="top" value="# View modes
Edit me, then flip the **view-mode** switch above. Switch to *split* to see the live
output, or *preview* for the read-only render.
- One ProseMirror document
- Markdown in / out at the edge"></km-editor>
</div>
</div>
<!-- ============================================================= -->
<!-- BU02 · I/O format lens -->
<!-- ============================================================= -->
<div class="card">
<h2>BU02 · I/O Format Lens</h2>
<p>
<code>content-format</code> is only the serialization lens for content I/O — switching it
re-reports the SAME document as <code>markdown</code>, <code>html</code>, or
<code>json</code>; it never rebuilds the document. The readout below is the live
<code>change</code> event's <code>detail.value</code> in the chosen format.
</p>
<div class="controls">
<span><code>content-format</code>:</span>
<label><input type="radio" name="eb02-format" value="markdown" checked> markdown</label>
<label><input type="radio" name="eb02-format" value="html"> html</label>
<label><input type="radio" name="eb02-format" value="json"> json</label>
</div>
<div class="demo-area">
<km-editor id="eb02" view-mode="edit" content-format="markdown" toolbar="top" value="## Format lens
Type here and watch the **output** below change format without rebuilding the
document. Add a [link](https://keenmate.com) or some `code`."></km-editor>
<div class="output">
<div class="output-label">live <code>change</code> → <code>detail.value</code></div>
<pre id="eb02-out"></pre>
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- BU03 · Toolbar & status-bar placement -->
<!-- ============================================================= -->
<div class="card">
<h2>BU03 · Toolbar & Status Bar</h2>
<p>
The static toolbar docks to any edge (<code>toolbar</code>) flush against the
editor; <code>none</code> hides it (the selection toolbar and context menu stay
available while editing). The <code>statusbar</code> is a dedicated strip of live
indicators (word / char count) that hugs the editor body — <code>both</code> splits
items across two strips.
</p>
<p>
Keyboard: the toolbar is <strong>one</strong> Tab stop; the arrows along its edge (Left / Right on
top / bottom, Up / Down on start / end) and Home / End move between the buttons. Hover or focus a button
for its tooltip (the shortcut included).
</p>
<div class="controls">
<span><code>toolbar</code>:</span>
<label><input type="radio" name="eb03-toolbar" value="top" checked> top</label>
<label><input type="radio" name="eb03-toolbar" value="bottom"> bottom</label>
<label><input type="radio" name="eb03-toolbar" value="start"> start</label>
<label><input type="radio" name="eb03-toolbar" value="end"> end</label>
<label><input type="radio" name="eb03-toolbar" value="none"> none</label>
</div>
<div class="controls">
<span><code>statusbar</code>:</span>
<label><input type="radio" name="eb03-status" value="none" checked> none</label>
<label><input type="radio" name="eb03-status" value="bottom"> bottom</label>
<label><input type="radio" name="eb03-status" value="top"> top</label>
<label><input type="radio" name="eb03-status" value="both"> both</label>
</div>
<div class="demo-area">
<km-editor id="eb03" view-mode="edit" content-format="markdown" toolbar="top" statusbar="none" value="### Chrome placement
Move the **toolbar** to any edge, or hide it and use the selection toolbar.
Turn on the **status bar** to see the live Words / Chars counters."></km-editor>
</div>
</div>
<!-- ============================================================= -->
<!-- BU04 · Theme -->
<!-- ============================================================= -->
<div class="card">
<h2>BU04 · Theme</h2>
<p>
The editor follows the page: core detects <code>data-theme</code> / <code>data-bs-theme</code> /
<code>.dark</code> on <code><html></code> (try the page's theme switch) and mirrors it onto the
host as <code>data-context-theme</code>. <code>data-theme="dark|light"</code> on the element itself
overrides the page for that one editor. Every color is a <code>light-dark()</code> pair, so only
<code>color-scheme</code> flips — the document is untouched.
</p>
<div class="controls">
<span><code>data-theme</code>:</span>
<label><input type="radio" name="eb04-theme" value="" checked> auto (follow the page)</label>
<label><input type="radio" name="eb04-theme" value="light"> light</label>
<label><input type="radio" name="eb04-theme" value="dark"> dark</label>
</div>
<div class="demo-area">
<km-editor id="eb04" view-mode="edit" content-format="markdown" toolbar="top" statusbar="bottom" value="#### Theming
Switch `data-theme` above, or the page theme — with `inline code` and a [link](https://keenmate.com)."></km-editor>
</div>
</div>
<!-- ============================================================= -->
<!-- BU05 · Declarative (no JavaScript) -->
<!-- ============================================================= -->
<div class="card">
<h2>BU05 · Declarative Setup</h2>
<p>
No JavaScript — just the element with attributes. Everything below is configured in
markup: the initial <code>value</code>, the <code>content-format</code>, the
<code>toolbar</code> edge, and <code>statusbar="both"</code>.
</p>
<div class="demo-area">
<km-editor
view-mode="edit"
content-format="markdown"
toolbar="top"
statusbar="both"
value="# Declarative
This editor was set up entirely in **HTML** — no script wired it.
1. Attributes in, content out
2. Nothing to initialize"></km-editor>
</div>
</div>
<!-- ============================================================= -->
<!-- BU06 · Custom CSS (shadow root) -->
<!-- ============================================================= -->
<div class="card">
<h2>BU06 · Custom CSS</h2>
<p>
Page CSS can't reach into the shadow root, so CSS is injected (core
<code>createCustomStyles()</code>). The page-wide layer
<code>window.components['km-editor'].customStyles</code> styles every editor on the page;
an instance's own <code>custom-styles</code> attribute (or <code>customStyles</code> /
<code>getCustomStylesCallback</code>) comes after it and wins. Both editors below follow the
page layer; the right one also has its own CSS.
</p>
<div class="controls">
<label><input type="checkbox" id="eb06-page"> page-wide: serif body text</label>
<label><input type="checkbox" id="eb06-own"> right editor's own <code>custom-styles</code>: wide letter spacing</label>
</div>
<div class="grid-2">
<div class="demo-area">
<km-editor id="eb06a" toolbar="none" value="Left — page layer only."></km-editor>
</div>
<div class="demo-area">
<km-editor id="eb06b" toolbar="none" value="Right — page layer + its own CSS."></km-editor>
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- BU07 · Disabled / Readonly States -->
<!-- ============================================================= -->
<div class="card">
<h2>BU07 · Disabled / Readonly States</h2>
<div class="grid">
<div class="form-group">
<label>Disabled Editor</label>
<km-editor disabled toolbar="none" value="A **disabled** editor — no typing, no selection, no focus."></km-editor>
<small>No interaction, not focusable, not submitted with a form</small>
</div>
<div class="form-group">
<label>Readonly Editor</label>
<km-editor readonly toolbar="none" value="A **readonly** editor — select and copy this text."></km-editor>
<small>The value can't change; still focusable and copyable, submitted with a form</small>
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- BU08 · Labels & page-wide locale -->
<!-- ============================================================= -->
<div class="card">
<h2>BU08 · Labels & Page-wide Locale</h2>
<p>
Every UI string resolves per key: the editor's own <code>labels</code> → the page-wide
flat dictionary <code>window.components.locale.labels['km-editor.<key>']</code> → English. Keys are
<code><elementType>.<name></code> (<code>ariaLabels.bold</code>, <code>headers.linkUrl</code>). Switch the page to Czech: both editors
follow at once (button names, tooltips, the status counters, the link / image prompts); the second one
keeps its own <code>ariaLabels.bold</code> label.
</p>
<div class="controls">
<span>page labels:</span>
<label><input type="radio" name="eb08-locale" value="" checked> English (unset)</label>
<label><input type="radio" name="eb08-locale" value="cs"> Czech</label>
</div>
<div class="demo-area">
<km-editor id="eb08a" statusbar="bottom" value="Select a word and press **Ctrl+K**."></km-editor>
<km-editor id="eb08b" statusbar="bottom" labels='{"ariaLabels.bold":"Fett"}' value="This one has its own **bold** label."></km-editor>
</div>
</div>
<!-- ============================================================= -->
<!-- BU09 · Required + validation -->
<!-- ============================================================= -->
<div class="card">
<h2>BU09 · Required + Validation</h2>
<p>
<code>required</code> blocks the submit while the document has no content — no text after trimming
whitespace and no image, table or other non-text node (<code>validity.valueMissing</code>, message
<code>errors.required</code>). The host gets <code>aria-required</code> and <code>:state(required)</code>; a
read-only or disabled editor is never missing. Submit empty (or with only spaces), then type something:
the browser shows its bubble on the editing surface.
</p>
<kmwc-demo>
<form id="eb09-form" novalidate>
<km-editor id="eb09" name="body" required toolbar="none" labels='{"placeholders.document":"Write something…"}'></km-editor>
<div class="form-actions">
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</div>
</form>
<div class="output" id="eb09-status"></div>
<script type="module" data-kmwc-show>
const form = document.getElementById('eb09-form')
const editor = document.getElementById('eb09')
const status = document.getElementById('eb09-status')
const flags = ['valueMissing', 'customError']
const show = async () => {
await editor.whenSettled()
const set = flags.filter((f) => editor.validity[f])
status.textContent = `validity: ${set.join(' / ') || 'valid'} — ${editor.validationMessage || '(no message)'}`
}
// novalidate + reportValidity(): the native bubble, and the submit stays in the page.
form.addEventListener('submit', (e) => {
e.preventDefault()
if (form.reportValidity()) status.textContent = `submitted ${editor.value.length} character(s)`
})
form.addEventListener('reset', () => setTimeout(show))
editor.addEventListener('change', show)
editor.whenReady().then(show)
</script>
</kmwc-demo>
</div>
<!-- Page wiring: bind each card's control switches to its editor. -->
<script type="module">
import { attachControlsPersistence } from '@keenmate/web-components-devkit/examples';
// Toggle a boolean attribute on/off.
const setBool = (el, name, on) => on ? el.setAttribute(name, '') : el.removeAttribute(name);
// Checked radio value for a group.
const radio = (name) => document.querySelector(`input[name="${name}"]:checked`)?.value;
const onRadio = (name, fn) =>
document.querySelectorAll(`input[name="${name}"]`).forEach((r) =>
r.addEventListener('change', (e) => { if (e.target.checked) fn(e.target.value); }));
function wire() {
// BU01 — view-mode + readonly
const eb01 = document.getElementById('eb01');
onRadio('eb01-mode', (v) => eb01.setAttribute('view-mode', v));
document.getElementById('eb01-readonly').addEventListener('change', (e) =>
setBool(eb01, 'readonly', e.target.checked));
// BU02 — format lens + live output readout
const eb02 = document.getElementById('eb02');
const out = document.getElementById('eb02-out');
const refresh = () => { out.textContent = eb02.value ?? ''; };
onRadio('eb02-format', (v) => { eb02.setAttribute('content-format', v); refresh(); });
eb02.addEventListener('change', refresh);
if (eb02.isReady) refresh(); else eb02.addEventListener('ready', refresh);
// BU03 — toolbar + status-bar placement
const eb03 = document.getElementById('eb03');
onRadio('eb03-toolbar', (v) => eb03.setAttribute('toolbar', v));
onRadio('eb03-status', (v) => eb03.setAttribute('statusbar', v));
// BU04 — per-instance data-theme (auto = remove it, so the page decides)
const eb04 = document.getElementById('eb04');
onRadio('eb04-theme', (v) => v ? eb04.setAttribute('data-theme', v) : eb04.removeAttribute('data-theme'));
// BU06 — custom CSS: the page-wide layer (every editor) + one instance's own layer
const PAGE_CSS = '.ProseMirror { font-family: Georgia, serif; }';
const OWN_CSS = '.ProseMirror { letter-spacing: .12em; }';
document.getElementById('eb06-page').addEventListener('change', (e) => {
window.components['km-editor'].customStyles = e.target.checked ? PAGE_CSS : null;
});
document.getElementById('eb06-own').addEventListener('change', (e) => {
const el = document.getElementById('eb06b');
e.target.checked ? el.setAttribute('custom-styles', OWN_CSS) : el.removeAttribute('custom-styles');
});
// BU08 — page-wide labels for every <km-editor> (core /locale)
const CS = {
'km-editor.ariaLabels.bold': 'Tučně', 'km-editor.ariaLabels.italic': 'Kurzíva',
'km-editor.ariaLabels.insertLink': 'Vložit odkaz', 'km-editor.ariaLabels.insertImage': 'Vložit obrázek',
'km-editor.labels.words': 'Slova: {count}', 'km-editor.labels.chars': 'Znaky: {count}',
'km-editor.headers.linkUrl': 'Adresa odkazu', 'km-editor.headers.imageUrl': 'Adresa obrázku',
'km-editor.headers.imageAlt': 'Popis obrázku (nepovinný)', 'km-editor.buttons.ok': 'OK',
'km-editor.buttons.cancel': 'Zrušit', 'km-editor.ariaLabels.close': 'Zavřít',
};
onRadio('eb08-locale', (v) => {
window.components.locale = v ? { labels: CS } : null;
});
// Persist the switches across reloads (restores + re-applies via our listeners).
attachControlsPersistence('basic');
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', wire);
else wire();
</script>