@silkui/core 0.1.9 → 0.1.10
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/README.md +13 -1
- package/package.json +5 -1
- package/src/globals.d.ts +3 -0
- package/src/globals.js +38 -0
- package/src/index.d.ts +107 -6
- package/src/index.js +849 -79
package/README.md
CHANGED
|
@@ -74,7 +74,19 @@ An `Svg` is one node whose markup its `SvgElement` and text children make, as an
|
|
|
74
74
|
`fill`, `width` and every other attribute are markup, and React's spellings (`strokeWidth`)
|
|
75
75
|
work too. Changes are drawn once, before the host applies the batch (`flushSvg()` draws them
|
|
76
76
|
now). Without a CSS size it takes the `<svg>`'s `width` and `height`, else a square of the
|
|
77
|
-
font size.
|
|
77
|
+
font size. Listeners on its elements hear pointer events on their shapes, with
|
|
78
|
+
`pointerover`, `pointerout`, `pointerenter` and `pointerleave` as the pointer moves between
|
|
79
|
+
them; `pointer-events`, `visibility`, `display` and clip paths decide hits as on the web.
|
|
80
|
+
`getBBox()`, `getTotalLength()` and `getPointAtLength()` measure elements from their markup.
|
|
81
|
+
|
|
82
|
+
Once an `Svg` has drawn, later changes go to the tree element by element, and only the shapes
|
|
83
|
+
whose geometry changed rasterize again: a script can animate it every frame. Listeners on its
|
|
84
|
+
elements make it live from its first draw. The tree draws
|
|
85
|
+
`g`, `path`, `rect`, `circle`, `ellipse`, `line`, `polyline`, `polygon`, `clipPath` and linear
|
|
86
|
+
and radial gradients this way; anything else (text, masks, filters, `use`...) puts the `Svg`
|
|
87
|
+
back to drawing its markup whole, which is right but slower. Browser libraries that build SVG
|
|
88
|
+
with `document`, such as lottie-web, run after `import "@silkui/core/globals"`, which adds a
|
|
89
|
+
minimal `window` and `document` where the realm has none.
|
|
78
90
|
|
|
79
91
|
A node has `append`, `appendChild`, `insertBefore`, `removeChild`, `remove`, `contains`,
|
|
80
92
|
`parentNode`, `childNodes`, `firstChild`, `lastChild` and the sibling getters, as in the DOM.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@silkui/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"description": "The node and event API of a Silk UI tree for JavaScript applications that run on the embedded SilkJS engine",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
".": {
|
|
13
13
|
"types": "./src/index.d.ts",
|
|
14
14
|
"default": "./src/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./globals": {
|
|
17
|
+
"types": "./src/globals.d.ts",
|
|
18
|
+
"default": "./src/globals.js"
|
|
15
19
|
}
|
|
16
20
|
},
|
|
17
21
|
"files": [
|
package/src/globals.d.ts
ADDED
package/src/globals.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// A minimal `window` and `document` for libraries written for the browser that build SVG with
|
|
2
|
+
// the DOM, such as lottie-web. Import it before them; globals the realm has stay as they are.
|
|
3
|
+
import { createNode, createSvgElement, createText } from "./index.js";
|
|
4
|
+
|
|
5
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
6
|
+
const KIND_OF_TAG = { div: "box", span: "box", p: "box", a: "box", img: "image", input: "input", svg: "svg" };
|
|
7
|
+
|
|
8
|
+
// A `<canvas>` here draws nothing: libraries such as lottie-web make one at load to probe.
|
|
9
|
+
const inert = new Proxy({}, { get: (target, name) => (name in target ? target[name] : () => {}) });
|
|
10
|
+
|
|
11
|
+
function createElement(tag) {
|
|
12
|
+
tag = String(tag).toLowerCase();
|
|
13
|
+
if (tag === "canvas") return { localName: tag, width: 300, height: 150, style: {}, getContext: () => inert };
|
|
14
|
+
const node = createNode(KIND_OF_TAG[tag] ?? "box");
|
|
15
|
+
node.localName ??= tag;
|
|
16
|
+
return node;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const none = () => [];
|
|
20
|
+
|
|
21
|
+
globalThis.window ??= globalThis;
|
|
22
|
+
if (typeof globalThis.navigator !== "object" || globalThis.navigator === null) globalThis.navigator = {};
|
|
23
|
+
if (globalThis.navigator.userAgent === undefined) {
|
|
24
|
+
Object.defineProperty(globalThis.navigator, "userAgent", { value: "SilkUI", enumerable: true, configurable: true });
|
|
25
|
+
}
|
|
26
|
+
globalThis.document ??= {
|
|
27
|
+
readyState: "complete",
|
|
28
|
+
createElement,
|
|
29
|
+
createElementNS: (namespace, tag) => (namespace === SVG_NAMESPACE ? createSvgElement(tag) : createElement(tag)),
|
|
30
|
+
createTextNode: (data) => createText(data),
|
|
31
|
+
getElementsByTagName: none,
|
|
32
|
+
getElementsByClassName: none,
|
|
33
|
+
querySelectorAll: none,
|
|
34
|
+
querySelector: () => null,
|
|
35
|
+
getElementById: () => null,
|
|
36
|
+
addEventListener() {},
|
|
37
|
+
removeEventListener() {},
|
|
38
|
+
};
|
package/src/index.d.ts
CHANGED
|
@@ -368,6 +368,19 @@ export class Image extends Node {
|
|
|
368
368
|
src: string;
|
|
369
369
|
}
|
|
370
370
|
|
|
371
|
+
/**
|
|
372
|
+
* A canvas the application draws with WebGPU, as a `<canvas>`: `width` and `height` are the
|
|
373
|
+
* drawing's size in pixels and `getContext("webgpu")` gives its `GPUCanvasContext` (the types
|
|
374
|
+
* of `@webgpu/types` describe it). Drawing runs on a GPU device of the application's own.
|
|
375
|
+
*/
|
|
376
|
+
export class Canvas extends Node {
|
|
377
|
+
constructor();
|
|
378
|
+
width: number;
|
|
379
|
+
height: number;
|
|
380
|
+
getContext(kind: "webgpu"): unknown | null;
|
|
381
|
+
getContext(kind: string): unknown | null;
|
|
382
|
+
}
|
|
383
|
+
|
|
371
384
|
/** Text inside an `<svg>`. */
|
|
372
385
|
export class SvgText {
|
|
373
386
|
constructor(data?: string);
|
|
@@ -378,7 +391,10 @@ export class SvgText {
|
|
|
378
391
|
destroy(): void;
|
|
379
392
|
}
|
|
380
393
|
|
|
381
|
-
/**
|
|
394
|
+
/**
|
|
395
|
+
* An element inside an `<svg>` (`path`, `g`, `circle`...): markup its root draws, no node.
|
|
396
|
+
* Once the root has drawn, a change to its elements updates them one at a time.
|
|
397
|
+
*/
|
|
382
398
|
export class SvgElement {
|
|
383
399
|
constructor(tag: string);
|
|
384
400
|
readonly localName: string;
|
|
@@ -392,6 +408,26 @@ export class SvgElement {
|
|
|
392
408
|
/** SVG's names, or React's spellings (`strokeWidth`, `xlinkHref`). */
|
|
393
409
|
setAttribute(name: string, value: unknown): void;
|
|
394
410
|
removeAttribute(name: string): void;
|
|
411
|
+
setAttributeNS(namespace: string | null, name: string, value: unknown): void;
|
|
412
|
+
getAttributeNS(namespace: string | null, name: string): string | null;
|
|
413
|
+
removeAttributeNS(namespace: string | null, name: string): void;
|
|
414
|
+
/** The `style` attribute as declarations: `element.style.display = "none"`. */
|
|
415
|
+
readonly style: Record<string, string>;
|
|
416
|
+
/**
|
|
417
|
+
* Hears pointer events (`click`, `pointerdown`, `pointerenter`...) on its shapes, as on the
|
|
418
|
+
* web; its `<svg>` turns live so the tree can tell which shape is under the pointer.
|
|
419
|
+
*/
|
|
420
|
+
addEventListener<K extends keyof EventMap>(type: K, listener: Listener<EventMap[K]>, options?: ListenerOptions): void;
|
|
421
|
+
addEventListener(type: string, listener: (event: globalThis.Event) => void, options?: ListenerOptions): void;
|
|
422
|
+
removeEventListener<K extends keyof EventMap>(type: K, listener: Listener<EventMap[K]>, options?: ListenerOptions): void;
|
|
423
|
+
removeEventListener(type: string, listener: (event: globalThis.Event) => void, options?: ListenerOptions): void;
|
|
424
|
+
dispatchEvent(event: globalThis.Event): boolean;
|
|
425
|
+
/** The box of its geometry in its own user space: a shape's fill, a group's children. */
|
|
426
|
+
getBBox(): { x: number; y: number; width: number; height: number };
|
|
427
|
+
/** The length of a shape's outline in user units. */
|
|
428
|
+
getTotalLength(): number;
|
|
429
|
+
/** The point `distance` along a shape's outline, clamped to its ends. */
|
|
430
|
+
getPointAtLength(distance: number): { x: number; y: number };
|
|
395
431
|
insertBefore<T extends SvgElement | SvgText | Text>(child: T, before?: SvgElement | SvgText | Text | null): T;
|
|
396
432
|
appendChild<T extends SvgElement | SvgText | Text>(child: T): T;
|
|
397
433
|
append(...children: (SvgElement | SvgText | Text | string)[]): void;
|
|
@@ -404,11 +440,16 @@ export class SvgElement {
|
|
|
404
440
|
* An inline `<svg>`: one node drawing the markup its `SvgElement` children make, at its CSS
|
|
405
441
|
* size (else its `width` and `height`, else a square of the font size), with its `color` as
|
|
406
442
|
* `currentColor`. Class, look, events and ARIA are the node's; `viewBox`, `fill`, `width`
|
|
407
|
-
* and the rest are markup.
|
|
443
|
+
* and the rest are markup. After its first draw, changes go to the tree element by element
|
|
444
|
+
* and only the shapes that changed redraw, so scripts can animate it (lottie-web and the like).
|
|
408
445
|
*/
|
|
409
446
|
export class Svg extends Node {
|
|
410
447
|
constructor();
|
|
411
448
|
readonly outerHTML: string;
|
|
449
|
+
setAttributeNS(namespace: string | null, name: string, value: unknown): void;
|
|
450
|
+
getAttributeNS(namespace: string | null, name: string): string | null;
|
|
451
|
+
/** The box of what it draws, in its user space (inside `viewBox`). */
|
|
452
|
+
getBBox(): { x: number; y: number; width: number; height: number };
|
|
412
453
|
textContent: string;
|
|
413
454
|
}
|
|
414
455
|
|
|
@@ -541,6 +582,17 @@ export interface SelectionEvent<T extends Node = Input> extends Event {
|
|
|
541
582
|
readonly selectionEnd: number;
|
|
542
583
|
}
|
|
543
584
|
|
|
585
|
+
/** An edit about to change an input (`beforeinput`), as the web's `InputEvent`. Preventing it cancels the edit. */
|
|
586
|
+
export interface BeforeInputEvent<T extends Node = Input> extends Event {
|
|
587
|
+
readonly target: FieldOf<T>;
|
|
588
|
+
readonly inputType: string;
|
|
589
|
+
/** The text the edit inserts. */
|
|
590
|
+
readonly data: string | null;
|
|
591
|
+
readonly dataTransfer: DataTransfer | null;
|
|
592
|
+
readonly isComposing: boolean;
|
|
593
|
+
getTargetRanges(): never[];
|
|
594
|
+
}
|
|
595
|
+
|
|
544
596
|
export interface ScrollEvent extends Event {
|
|
545
597
|
readonly scrollLeft: number;
|
|
546
598
|
readonly scrollTop: number;
|
|
@@ -592,6 +644,10 @@ export interface EventMap {
|
|
|
592
644
|
blur: Event;
|
|
593
645
|
input: InputEvent;
|
|
594
646
|
selectionchange: SelectionEvent;
|
|
647
|
+
copy: ClipboardEvent;
|
|
648
|
+
cut: ClipboardEvent;
|
|
649
|
+
paste: ClipboardEvent;
|
|
650
|
+
beforeinput: BeforeInputEvent;
|
|
595
651
|
scroll: ScrollEvent;
|
|
596
652
|
layout: LayoutEvent;
|
|
597
653
|
gotpointercapture: Event;
|
|
@@ -616,6 +672,7 @@ export function createNode(kind: "text"): Text;
|
|
|
616
672
|
export function createNode(kind: "input"): Input;
|
|
617
673
|
export function createNode(kind: "richinput"): RichInput;
|
|
618
674
|
export function createNode(kind: "image" | "img"): Image;
|
|
675
|
+
export function createNode(kind: "canvas"): Canvas;
|
|
619
676
|
export function createNode(kind: "svg"): Svg;
|
|
620
677
|
export function createNode(kind: NodeKind): Node;
|
|
621
678
|
|
|
@@ -680,14 +737,58 @@ export class DroppedFile {
|
|
|
680
737
|
readonly type: string;
|
|
681
738
|
}
|
|
682
739
|
|
|
683
|
-
/**
|
|
740
|
+
/** A file in a `DataTransfer` that is no file on disk, such as a pasted image: the runtime's `File`. */
|
|
741
|
+
export interface DataFile {
|
|
742
|
+
readonly name: string;
|
|
743
|
+
readonly type: string;
|
|
744
|
+
readonly size: number;
|
|
745
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
746
|
+
text(): Promise<string>;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/** One entry of a `DataTransfer`. */
|
|
750
|
+
export interface DataTransferItem {
|
|
751
|
+
readonly kind: "string" | "file";
|
|
752
|
+
readonly type: string;
|
|
753
|
+
getAsString(callback: (value: string) => void): void;
|
|
754
|
+
getAsFile(): DataFile | DroppedFile | null;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
export interface DataTransferItemList extends Iterable<DataTransferItem> {
|
|
758
|
+
readonly length: number;
|
|
759
|
+
readonly [index: number]: DataTransferItem;
|
|
760
|
+
add(data: string, type: string): DataTransferItem;
|
|
761
|
+
add(file: DataFile | DroppedFile): DataTransferItem;
|
|
762
|
+
remove(index: number): void;
|
|
763
|
+
clear(): void;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* What a copy, cut, paste or file drag carries, as the web's. `new DataTransfer()` is empty
|
|
768
|
+
* and writable; a paste's is read-only, and after its event none can be read.
|
|
769
|
+
*/
|
|
684
770
|
export class DataTransfer {
|
|
685
|
-
|
|
686
|
-
readonly
|
|
687
|
-
readonly
|
|
771
|
+
constructor();
|
|
772
|
+
readonly files: readonly (DataFile | DroppedFile)[];
|
|
773
|
+
readonly items: DataTransferItemList;
|
|
774
|
+
readonly types: readonly string[];
|
|
688
775
|
dropEffect: string;
|
|
689
776
|
effectAllowed: string;
|
|
690
777
|
getData(format: string): string;
|
|
778
|
+
setData(format: string, data: string): void;
|
|
779
|
+
clearData(format?: string): void;
|
|
780
|
+
setDragImage(image: unknown, x: number, y: number): void;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* A copy, cut or paste at the focused node, as the web's. A `copy` or `cut` listener that sets
|
|
785
|
+
* data on `clipboardData` and prevents the default puts that data on the clipboard; a `paste`
|
|
786
|
+
* listener reads what it holds (`text/plain`, `text/html`, a pasted PNG in `files`), and
|
|
787
|
+
* preventing it keeps the paste out of the input.
|
|
788
|
+
*/
|
|
789
|
+
export class ClipboardEvent extends Event {
|
|
790
|
+
constructor(type: string, init?: { bubbles?: boolean; cancelable?: boolean; composed?: boolean; clipboardData?: DataTransfer | null });
|
|
791
|
+
readonly clipboardData: DataTransfer | null;
|
|
691
792
|
}
|
|
692
793
|
|
|
693
794
|
/** Files dragged from another app over the window (`dragenter`, `dragover`, `dragleave`, `drop`). */
|