@nanahoshi/mona-ui 0.36.1 → 0.37.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.
Files changed (58) hide show
  1. package/docs/api/editor.md +67 -0
  2. package/docs/api/file-upload.md +1 -1
  3. package/docs/api/i18n.md +16 -0
  4. package/docs/api/locales.md +14 -14
  5. package/docs/editor.md +238 -9
  6. package/fesm2022/nanahoshi-mona-ui-editor.mjs +1348 -190
  7. package/fesm2022/nanahoshi-mona-ui-file-upload.mjs +28 -94
  8. package/fesm2022/nanahoshi-mona-ui-i18n-icu.mjs +50 -1
  9. package/fesm2022/nanahoshi-mona-ui-internal.mjs +83 -2
  10. package/fesm2022/nanahoshi-mona-ui-locales-ar-sa.mjs +59 -1
  11. package/fesm2022/nanahoshi-mona-ui-locales-de-de.mjs +23 -1
  12. package/fesm2022/nanahoshi-mona-ui-locales-es-es.mjs +35 -1
  13. package/fesm2022/nanahoshi-mona-ui-locales-fr-fr.mjs +35 -1
  14. package/fesm2022/nanahoshi-mona-ui-locales-id-id.mjs +17 -1
  15. package/fesm2022/nanahoshi-mona-ui-locales-it-it.mjs +35 -1
  16. package/fesm2022/nanahoshi-mona-ui-locales-ja-jp.mjs +17 -1
  17. package/fesm2022/nanahoshi-mona-ui-locales-ko-kr.mjs +17 -1
  18. package/fesm2022/nanahoshi-mona-ui-locales-pl-pl.mjs +47 -1
  19. package/fesm2022/nanahoshi-mona-ui-locales-pt-br.mjs +35 -1
  20. package/fesm2022/nanahoshi-mona-ui-locales-ru-ru.mjs +47 -1
  21. package/fesm2022/nanahoshi-mona-ui-locales-tr-tr.mjs +17 -1
  22. package/fesm2022/nanahoshi-mona-ui-locales-zh-cn.mjs +17 -1
  23. package/fesm2022/nanahoshi-mona-ui-locales-zh-tw.mjs +17 -1
  24. package/i18n/catalogs/en-US.messages.json +17 -1
  25. package/llms.txt +1 -1
  26. package/locales/ar-sa/ar-sa.messages.json +17 -1
  27. package/locales/de-de/de-de.messages.json +17 -1
  28. package/locales/es-es/es-es.messages.json +17 -1
  29. package/locales/fr-fr/fr-fr.messages.json +17 -1
  30. package/locales/id-id/id-id.messages.json +17 -1
  31. package/locales/it-it/it-it.messages.json +17 -1
  32. package/locales/ja-jp/ja-jp.messages.json +17 -1
  33. package/locales/ko-kr/ko-kr.messages.json +17 -1
  34. package/locales/pl-pl/pl-pl.messages.json +17 -1
  35. package/locales/pt-br/pt-br.messages.json +17 -1
  36. package/locales/ru-ru/ru-ru.messages.json +17 -1
  37. package/locales/tr-tr/tr-tr.messages.json +17 -1
  38. package/locales/zh-cn/zh-cn.messages.json +17 -1
  39. package/locales/zh-tw/zh-tw.messages.json +17 -1
  40. package/package.json +1 -1
  41. package/types/nanahoshi-mona-ui-editor.d.ts +124 -2
  42. package/types/nanahoshi-mona-ui-file-upload.d.ts +1 -0
  43. package/types/nanahoshi-mona-ui-i18n.d.ts +32 -0
  44. package/types/nanahoshi-mona-ui-internal.d.ts +37 -0
  45. package/types/nanahoshi-mona-ui-locales-ar-sa.d.ts +16 -0
  46. package/types/nanahoshi-mona-ui-locales-de-de.d.ts +16 -0
  47. package/types/nanahoshi-mona-ui-locales-es-es.d.ts +16 -0
  48. package/types/nanahoshi-mona-ui-locales-fr-fr.d.ts +16 -0
  49. package/types/nanahoshi-mona-ui-locales-id-id.d.ts +16 -0
  50. package/types/nanahoshi-mona-ui-locales-it-it.d.ts +16 -0
  51. package/types/nanahoshi-mona-ui-locales-ja-jp.d.ts +16 -0
  52. package/types/nanahoshi-mona-ui-locales-ko-kr.d.ts +16 -0
  53. package/types/nanahoshi-mona-ui-locales-pl-pl.d.ts +16 -0
  54. package/types/nanahoshi-mona-ui-locales-pt-br.d.ts +16 -0
  55. package/types/nanahoshi-mona-ui-locales-ru-ru.d.ts +16 -0
  56. package/types/nanahoshi-mona-ui-locales-tr-tr.d.ts +16 -0
  57. package/types/nanahoshi-mona-ui-locales-zh-cn.d.ts +16 -0
  58. package/types/nanahoshi-mona-ui-locales-zh-tw.d.ts +16 -0
package/docs/editor.md CHANGED
@@ -16,13 +16,13 @@ Give it a name with a `<label>` and `aria-labelledby`, or with `aria-label`. A p
16
16
 
17
17
  Use Editor when people write formatted text: headings, lists, links, images and tables. For plain text, use `mona-text-area`.
18
18
 
19
- `@nanahoshi/mona-ui/editor` exports `EditorComponent`, the `EDITOR_DEFAULT_MESSAGES` catalog, and the `ContentChangeEvent` and `EditorSettings` types.
19
+ `@nanahoshi/mona-ui/editor` exports `EditorComponent`, the `EDITOR_DEFAULT_MESSAGES` catalog, the `countEditorText` function, and the `ContentChangeEvent`, `EditorMention`, `EditorMentionSearch`, `EditorSettings` and `EditorTextUnit` types.
20
20
 
21
21
  ## Import & Quick Start
22
22
 
23
23
  ```typescript
24
- import { EditorComponent } from "@nanahoshi/mona-ui/editor";
25
- import type { ContentChangeEvent, EditorSettings } from "@nanahoshi/mona-ui/editor";
24
+ import { countEditorText, EditorComponent } from "@nanahoshi/mona-ui/editor";
25
+ import type { ContentChangeEvent, EditorMention, EditorMentionSearch, EditorSettings } from "@nanahoshi/mona-ui/editor";
26
26
  ```
27
27
 
28
28
  ```html
@@ -159,6 +159,199 @@ export class EditorFormsExampleComponent {
159
159
  </div>
160
160
  ```
161
161
 
162
+ ### Image upload
163
+
164
+ `imageUpload` uploads an image file and returns its address, as a promise or an observable. It takes the same function as the file upload's `upload`. With it, images pasted or dropped into the content upload where they land, and the image tool's form takes a file as well as an address. `imageAccept` and `imageMaxFileSize` limit the files it takes.
165
+
166
+ `image-upload-example.component.ts`
167
+
168
+ ```typescript
169
+ import { Component, signal } from "@angular/core";
170
+ import { EditorComponent } from "@nanahoshi/mona-ui/editor";
171
+ import type { FileUploadHandler } from "@nanahoshi/mona-ui/file-upload";
172
+ import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
173
+
174
+ /**
175
+ * Stands in for a server: it takes about a second per image, and returns an address the page can show. A real server
176
+ * returns the address it stored the image at.
177
+ */
178
+ function simulatedImageServer(): FileUploadHandler {
179
+ return (file, { progress, signal }) =>
180
+ new Promise<string>((resolve, reject) => {
181
+ let step = 0;
182
+ const timer = setInterval(() => {
183
+ step++;
184
+ progress(step, 5);
185
+ if (step === 5) {
186
+ clearInterval(timer);
187
+ resolve(URL.createObjectURL(file));
188
+ }
189
+ }, 200);
190
+ signal.addEventListener("abort", () => {
191
+ clearInterval(timer);
192
+ reject(signal.reason);
193
+ });
194
+ });
195
+ }
196
+
197
+ @Component({
198
+ selector: "app-editor-image-upload-example",
199
+ imports: [EditorComponent, FormFieldComponent],
200
+ templateUrl: "./image-upload-example.component.html"
201
+ })
202
+ export class EditorImageUploadExampleComponent {
203
+ protected readonly maxFileSize = 2 * 1024 * 1024;
204
+ protected readonly notes = signal("<p>Paste a screenshot here.</p>");
205
+ protected readonly upload = simulatedImageServer();
206
+ }
207
+ ```
208
+
209
+ `image-upload-example.component.html`
210
+
211
+ ```html
212
+ <mona-form-field label="Release notes" hint="Paste or drop an image, or choose one in the image tool. Up to 2 MB.">
213
+ <mona-editor
214
+ class="h-64 w-full"
215
+ [imageMaxFileSize]="maxFileSize"
216
+ [imageUpload]="upload"
217
+ [settings]="{ table: false }"
218
+ [(value)]="notes" />
219
+ </mona-form-field>
220
+ ```
221
+
222
+ ### Counts and a character limit
223
+
224
+ `showWordCount` and `showCharacterCount` show counts under the content. `characterLimit` shows how many characters are left, then how many are too many. It doesn't stop typing or cut what's pasted, so in a form, check the limit with `countEditorText`, which counts the value as the editor does.
225
+
226
+ `character-limit-example.component.ts`
227
+
228
+ ```typescript
229
+ import { Component, signal } from "@angular/core";
230
+ import { form, FormField, submit, validate } from "@angular/forms/signals";
231
+ import { ButtonDirective } from "@nanahoshi/mona-ui/button";
232
+ import { countEditorText, EditorComponent } from "@nanahoshi/mona-ui/editor";
233
+ import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
234
+
235
+ const LIMIT = 140;
236
+
237
+ @Component({
238
+ selector: "app-editor-character-limit-example",
239
+ imports: [ButtonDirective, EditorComponent, FormField, FormFieldComponent],
240
+ templateUrl: "./character-limit-example.component.html"
241
+ })
242
+ export class EditorCharacterLimitExampleComponent {
243
+ readonly #model = signal({
244
+ post: "<p>The editor now uploads images, counts what you write, and offers mentions.</p>"
245
+ });
246
+ protected readonly limit = LIMIT;
247
+ protected readonly posted = signal(false);
248
+ protected readonly update = form(this.#model, path => {
249
+ // The editor shows the count and lets people type past it; the form keeps a long post from being sent.
250
+ validate(path.post, ({ value }) =>
251
+ countEditorText(value(), "characters") > LIMIT
252
+ ? { kind: "tooLong", message: `Keep the post to ${LIMIT} characters.` }
253
+ : undefined
254
+ );
255
+ });
256
+
257
+ protected onSubmit(event: Event): void {
258
+ event.preventDefault();
259
+ void submit(this.update, async () => {
260
+ this.posted.set(true);
261
+ });
262
+ }
263
+ }
264
+ ```
265
+
266
+ `character-limit-example.component.html`
267
+
268
+ ```html
269
+ <form class="flex flex-col gap-4" novalidate (submit)="onSubmit($event)">
270
+ <mona-form-field label="Status update">
271
+ <mona-editor
272
+ class="h-56 w-full"
273
+ showWordCount
274
+ [characterLimit]="limit"
275
+ [formField]="update.post"
276
+ [settings]="{ headings: false, image: false, table: false }" />
277
+ </mona-form-field>
278
+ <div class="flex items-center gap-3">
279
+ <button monaButton look="primary" type="submit">Post</button>
280
+ @if (posted()) {
281
+ <span class="text-sm" role="status">Posted.</span>
282
+ }
283
+ </div>
284
+ </form>
285
+ ```
286
+
287
+ ### Mentions
288
+
289
+ `mentionSearch` finds what to mention as `@` and a query are typed, such as people. It returns a list, a promise or an observable. A mention is kept in the HTML as `<span data-type="mention" data-id="…" data-label="…">@label</span>`, so your server can find who was mentioned by `data-id`.
290
+
291
+ `mentions-example.component.ts`
292
+
293
+ ```typescript
294
+ import { Component, computed, signal } from "@angular/core";
295
+ import { EditorComponent, type EditorMention, type EditorMentionSearch } from "@nanahoshi/mona-ui/editor";
296
+ import { FormFieldComponent } from "@nanahoshi/mona-ui/form-field";
297
+
298
+ const TEAM: readonly EditorMention[] = [
299
+ { id: "u1", label: "Ana Lima" },
300
+ { id: "u2", label: "Bo Chen" },
301
+ { id: "u3", label: "Chidi Okafor" },
302
+ { id: "u4", label: "Dana Weiss" },
303
+ { id: "u5", label: "Emre Kaya" },
304
+ { id: "u6", label: "Fatima Zahra" },
305
+ { id: "u7", label: "Giulia Rossi" },
306
+ { id: "u8", label: "Hana Sato" }
307
+ ];
308
+
309
+ @Component({
310
+ selector: "app-editor-mentions-example",
311
+ imports: [EditorComponent, FormFieldComponent],
312
+ templateUrl: "./mentions-example.component.html"
313
+ })
314
+ export class EditorMentionsExampleComponent {
315
+ protected readonly comment = signal(
316
+ '<p>Thanks <span data-type="mention" data-id="u2" data-label="Bo Chen">@Bo Chen</span> for the review.</p>'
317
+ );
318
+
319
+ /** Who the comment mentions, from the `data-id` of each mention in its HTML. */
320
+ protected readonly mentioned = computed(() => {
321
+ const ids = new Set(Array.from(this.comment().matchAll(/data-id="([^"]*)"/gu), match => match[1]));
322
+ return TEAM.filter(person => ids.has(person.id)).map(person => person.label);
323
+ });
324
+
325
+ /** A search can return a promise or an observable too, such as a request to the server. */
326
+ protected readonly search: EditorMentionSearch = query => {
327
+ const text = query.toLocaleLowerCase();
328
+ return TEAM.filter(person => person.label.toLocaleLowerCase().includes(text)).slice(0, 6);
329
+ };
330
+ }
331
+ ```
332
+
333
+ `mentions-example.component.html`
334
+
335
+ ```html
336
+ <div class="flex flex-col gap-3">
337
+ <mona-form-field label="Comment" hint="Type @ and a name to mention someone.">
338
+ <mona-editor
339
+ class="h-56 w-full"
340
+ [mentionSearch]="search"
341
+ [settings]="{ image: false, table: false }"
342
+ [(value)]="comment" />
343
+ </mona-form-field>
344
+ <p class="text-sm text-muted-foreground">
345
+ Mentioned:
346
+ @for (name of mentioned(); track name) {
347
+ {{ name }}{{ $last ? "" : ", " }}
348
+ } @empty {
349
+ nobody
350
+ }
351
+ </p>
352
+ </div>
353
+ ```
354
+
162
355
  ## Behavior
163
356
 
164
357
  - The form inputs (`disabled`, `invalid`, `readonly`, `required`, `touched`) are set by the form when the editor is bound with `[formField]`.
@@ -168,7 +361,7 @@ export class EditorFormsExampleComponent {
168
361
 
169
362
  Every tool is on by default. Pass only what you want to change, as in the toolbar example.
170
363
 
171
- `alignment`, `blockquote`, `bold`, `code`, `fontColor`, `fontFamily`, `fontHighlight`, `fontSize`, `headings`, `history`, `horizontalRule`, `image`, `indent`, `italic`, `link`, `list`, `strikethrough`, `subscript`, `superscript`, `table`, `taskList` and `underline` are booleans. `code: false` also turns off the code block.
364
+ `alignment`, `blockquote`, `bold`, `code`, `fontColor`, `fontFamily`, `fontHighlight`, `fontSize`, `headings`, `history`, `horizontalRule`, `image`, `indent`, `italic`, `link`, `list`, `mention`, `strikethrough`, `subscript`, `superscript`, `table`, `taskList` and `underline` are booleans. `code: false` also turns off the code block, and `image: false` image upload. `mention` has no tool: it keeps the mentions of the content, and offers them as `@` is typed when `mentionSearch` is set. Without it, mentions stay as text.
172
365
 
173
366
  | Setting | Type | Default | Description |
174
367
  | :------------- | :----------------- | :------ | :--------------------------------------------------------------------------------------------------------------------- |
@@ -179,20 +372,49 @@ If you remove a tool, content that used it stays as text.
179
372
 
180
373
  The text color and highlight pickers show the color of the selection. They show none when the selected text has no color or several, so any color you pick applies to all of it.
181
374
 
375
+ ### Image Upload
376
+
377
+ - While an image uploads, a placeholder shows where it goes, with a preview and the progress. The placeholder isn't content: the value changes once the image is in.
378
+ - The image goes where it was pasted or dropped, even as text is typed or deleted next to it. If a deletion takes the placeholder with it, the image isn't added. The images of one paste or drop go in their order, whichever finishes first.
379
+ - The upload returns the image's address as a string. If it fails, or returns anything else, the placeholder goes and the editor says the image couldn't be uploaded.
380
+ - A paste that carries HTML too, such as an image copied from a web page, is pasted as it is, with the image's own address.
381
+ - A file larger than `imageMaxFileSize` isn't uploaded, and the editor says so.
382
+ - When the editor is destroyed, its uploads stop: the upload's `signal` is aborted.
383
+ - With an image selected, the image tool edits it: its address, alt text and size.
384
+
385
+ ### Counts
386
+
387
+ - Characters are counted as people see them: an emoji or a letter with an accent counts once. Markup, images and line breaks don't count, and a mention counts as its `@label`.
388
+ - Words are found by the rules of the library locale, so text without spaces, such as Japanese, counts its words.
389
+ - Over the limit, the editor shows as invalid, as it does with `invalid`.
390
+
391
+ ### Mentions
392
+
393
+ - The suggestions open for an `@` at the start of a line or after a space, so an email address doesn't open them, and not in code.
394
+ - Each change to the query searches again. A late result for an earlier query is dropped, and a failed search shows no matches.
395
+ - Escape closes the suggestions until a new `@` is typed. They close when the content loses focus too.
396
+ - A mention is one piece: Backspace removes it at once.
397
+
182
398
  ### Content and Safety
183
399
 
184
400
  The editor reads HTML through its schema, so tags and attributes it doesn't know are dropped. Links typed in the link dialog keep `http:`, `https:`, `mailto:`, `tel:` and relative URLs, get `https://` when they are a bare host, and are refused with any other scheme, such as `javascript:`.
185
401
 
402
+ An uploaded image's address goes in as the upload returns it.
403
+
186
404
  These checks protect the editor, not your application. The value is user-written HTML: sanitize it on the server before you store it, and again before you render it anywhere else.
187
405
 
188
406
  ### Localization and Direction
189
407
 
190
- Toolbar labels and the default content name come from the `editor` messages (`EDITOR_DEFAULT_MESSAGES`), and follow the library locale. In right-to-left, the toolbar order mirrors and start and end alignment follow the direction.
408
+ Toolbar labels, the default content name, the counts and what the editor announces come from the `editor` messages (`EDITOR_DEFAULT_MESSAGES`), and follow the library locale. In right-to-left, the toolbar order mirrors and start and end alignment follow the direction.
191
409
 
192
410
  ## Accessibility
193
411
 
194
412
  The content has `role="textbox"` with `aria-multiline`, its name, and `aria-disabled`, `aria-readonly`, `aria-required` and `aria-invalid` when they apply. Link an error message or hint with `aria-describedby`. The toolbar is named and its toggle buttons report `aria-pressed`.
195
413
 
414
+ - While an image uploads, its placeholder is an image named for it, such as "Uploading chart.png". The editor announces when an upload starts and when the image is in, fails or is too large.
415
+ - With `characterLimit`, the content is described by the limit too, after your own `aria-describedby`. Once typing stops for a second, the editor announces how many characters are left or too many, while the content has focus. The counts under the content aren't announced as they change.
416
+ - Focus stays in the content while mention suggestions show. The content has `aria-autocomplete="list"`, `aria-controls` for the list and `aria-activedescendant` for the highlighted suggestion, and how many suggestions there are is announced.
417
+
196
418
  ### Keyboard Navigation
197
419
 
198
420
  The toolbar is one Tab stop.
@@ -206,14 +428,18 @@ The toolbar is one Tab stop.
206
428
  | `Tab` | Elsewhere in the content | Leaves the editor. |
207
429
  | `Ctrl` + `]` / `Ctrl` + `[` | Content | Indents or outdents. Use `Cmd` on macOS. |
208
430
  | `Escape`, then `Tab` | List in the content | Leaves the editor, so the keyboard isn't stuck in a long list. |
431
+ | `ArrowDown` / `ArrowUp` | Mention suggestions | Moves the highlight, wrapping at the ends. |
432
+ | `Enter` / `Tab` | Mention suggestions | Puts the highlighted suggestion in, with a space after it. |
433
+ | `Escape` | Mention suggestions | Closes them until a new `@` is typed. |
209
434
 
210
435
  ## API
211
436
 
212
437
  Everything below is imported from `@nanahoshi/mona-ui/editor`. Each name links to its reference page, with every input, output, property and method.
213
438
 
214
- | Name | Kind |
215
- | --- | --- |
216
- | [`EditorComponent`](api/editor.md#editorcomponent) | Component |
439
+ | Name | Kind | Description |
440
+ | --- | --- | --- |
441
+ | [`EditorComponent`](api/editor.md#editorcomponent) | Component | |
442
+ | [`countEditorText`](api/editor.md#counteditortext) | Function | Counts the characters or words of the editor's HTML value, as the editor's own count does: the text people see, without the markup. |
217
443
 
218
444
  ### Types
219
445
 
@@ -221,6 +447,9 @@ Everything below is imported from `@nanahoshi/mona-ui/editor`. Each name links t
221
447
  | --- | --- | --- |
222
448
  | [`EDITOR_DEFAULT_MESSAGES`](api/editor.md#editor-default-messages) | Constant | |
223
449
  | [`ContentChangeEvent`](api/editor.md#contentchangeevent) | Interface | The content of the editor after a user change, emitted by `EditorComponent.update`. |
450
+ | [`EditorMention`](api/editor.md#editormention) | Interface | Someone, or something, the content can mention. |
224
451
  | [`EditorSettings`](api/editor.md#editorsettings) | Interface | The features of an editor. |
452
+ | [`EditorMentionSearch`](api/editor.md#editormentionsearch) | Type alias | Finds the mentions to offer for the text typed after `@`, which is empty right after the `@`. |
453
+ | [`EditorTextUnit`](api/editor.md#editortextunit) | Type alias | What `countEditorText` counts: characters as people see them, or words. |
225
454
 
226
- **Related types:** [`MonaEditorMessages`](api/i18n.md#monaeditormessages)
455
+ **Related types:** [`FileUploadHandler`](api/file-upload.md#fileuploadhandler), [`MonaEditorMessages`](api/i18n.md#monaeditormessages)