Repository navigation
Expand file tree
/
Copy pathexamples-basic.html
More file actions
263 lines (228 loc) · 12.7 KB
/
Copy pathexamples-basic.html
File metadata and controls
263 lines (228 loc) · 12.7 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
<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.">
<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>
<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>
<!-- 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');
});
// Persist the switches across reloads (restores + re-applies via our listeners).
attachControlsPersistence('basic');
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', wire);
else wire();
</script>