@xoong/richtext 0.0.39 → 0.0.42

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/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "name": "@xoong/richtext",
3
- "version": "0.0.39",
3
+ "version": "0.0.42",
4
4
  "description": "A small rich text editor for xoong forms: contenteditable, no dependencies besides @xoong/core, sanitised HTML in and out. Toolbar, headings, bold, italic, lists, quote, links (http, https, mailto, tel only), media hook.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": {
9
+ "types": "./src/index.d.ts",
9
10
  "default": "./src/index.js"
10
11
  },
11
- "./sanitize": "./src/sanitize.js",
12
+ "./sanitize": {
13
+ "types": "./src/sanitize.d.ts",
14
+ "default": "./src/sanitize.js"
15
+ },
12
16
  "./richtext.css": "./src/richtext.css"
13
17
  },
14
18
  "sideEffects": true,
@@ -32,9 +36,9 @@
32
36
  "bugs": {
33
37
  "url": "https://github.com/fpt-duongtx/xoong/issues"
34
38
  },
35
- "homepage": "https://github.com/fpt-duongtx/xoong#readme",
39
+ "homepage": "https://fpt-duongtx.github.io/xoong/",
36
40
  "dependencies": {
37
- "@xoong/core": "0.0.39"
41
+ "@xoong/core": "0.0.42"
38
42
  },
39
43
  "publishConfig": {
40
44
  "access": "public"
package/src/index.d.ts ADDED
@@ -0,0 +1,61 @@
1
+ // Types for @xoong/richtext. Importing the package registers <xo-richtext>; these types describe the element, its tag, its events and the sanitiser.
2
+ // To add an element or event: an interface, a line in `XoRichtextTagMap` / `XoRichtextEventMap`.
3
+ import type { XoControl } from '@xoong/core';
4
+
5
+ export { sanitizeHtml, isSafeLink, isSafeImage, textOf } from './sanitize.js';
6
+
7
+ /** What `detail.insert` of `xo-pick-media` takes: a URL, or `{ src, alt }` (a string second argument is the alt text for a URL). */
8
+ export type XoRichtextInsert = (media: { src: string; alt?: string } | string, alt?: string) => void;
9
+
10
+ /** `<xo-richtext>`: a small rich text editor and a form control. Its value is sanitised HTML (p, h2, h3, strong, em, u, ul, ol, li, blockquote, br, a, img). */
11
+ export interface XoRichtextElement extends XoControl {
12
+ value: string;
13
+ label: string;
14
+ placeholder: string;
15
+ /** Space separated tools, in order: `undo redo style bold italic underline h2 ul ol quote link image`. `image` shows only with `media`. */
16
+ tools: string;
17
+ /** `full` or `compact` forces a toolbar size; by default the editor's own width decides. */
18
+ toolbar: string;
19
+ /** Shows the image tool, which fires `xo-pick-media`. */
20
+ media: boolean;
21
+ /** Most text characters allowed (0 is no limit). */
22
+ maxlength: number;
23
+ /** Whether the editor is open in its dialog. */
24
+ readonly expanded: boolean;
25
+ /** Open the same editor in a large dialog. Fires `xo-richtext-expand`. */
26
+ expand(): void;
27
+ /** Close the dialog (what Done and Escape do). Fires `xo-richtext-done`. */
28
+ collapse(): void;
29
+ /** Insert an image (http, https or a relative URL) at the caret, or at the range `at`. Returns false when it was refused (read-only, disabled, unsafe URL). */
30
+ insertImage(src: string, alt?: string, at?: Range | null): boolean;
31
+ }
32
+
33
+ /** The class behind `<xo-richtext>` (`customElements.get('xo-richtext')`). */
34
+ export interface XoRichtextConstructor {
35
+ new (): XoRichtextElement;
36
+ /** A typed address made into a link target: `a@b.test` becomes `mailto:`, `example.com` becomes `https://`. */
37
+ linkTarget(raw: string): string;
38
+ }
39
+
40
+ /** Every element of this package by tag name. */
41
+ export interface XoRichtextTagMap {
42
+ 'xo-richtext': XoRichtextElement;
43
+ }
44
+
45
+ /** The events that carry data, by name. */
46
+ export interface XoRichtextEventMap {
47
+ /**
48
+ * The image tool was used. Open your media library in a listener, call `preventDefault()`, and call `detail.insert` when the person has chosen
49
+ * (now or later). Without `preventDefault()` nothing is inserted.
50
+ */
51
+ 'xo-pick-media': CustomEvent<{ insert: XoRichtextInsert }>;
52
+ 'xo-richtext-expand': CustomEvent<null>;
53
+ 'xo-richtext-done': CustomEvent<null>;
54
+ }
55
+
56
+ declare global {
57
+ interface HTMLElementTagNameMap extends XoRichtextTagMap {}
58
+ interface HTMLElementEventMap extends XoRichtextEventMap {}
59
+ // The events bubble, so a listener on the document is typed too.
60
+ interface DocumentEventMap extends XoRichtextEventMap {}
61
+ }
@@ -0,0 +1,16 @@
1
+ // Types for @xoong/richtext/sanitize: the allow-list sanitiser behind <xo-richtext>. It needs a DOM (browser, happy-dom).
2
+
3
+ /** A link target the editor allows: http, https, mailto or tel. */
4
+ export function isSafeLink(url: string): boolean;
5
+
6
+ /** An image source the editor allows: http, https, or a URL without a scheme (relative or root-relative). */
7
+ export function isSafeImage(url: string): boolean;
8
+
9
+ /**
10
+ * Sanitise HTML to the editor's subset: p, h2, h3, strong, em, u, ul, ol, li, blockquote, br, a (safe links only), img (safe sources only).
11
+ * `images: false` removes `<img>`.
12
+ */
13
+ export function sanitizeHtml(html: string, options?: { images?: boolean }): string;
14
+
15
+ /** The text of sanitised or raw HTML, for "is it empty" checks and length limits. */
16
+ export function textOf(html: string): string;