@oscarpalmer/abydon 0.16.0 → 0.19.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.
- package/dist/{abydon.full.js → abydon.full.mjs} +542 -234
- package/dist/constants.d.mts +37 -0
- package/dist/constants.mjs +44 -0
- package/dist/fragment-9qTBAvtR.d.mts +82 -0
- package/dist/fragment.d.mts +2 -0
- package/dist/{fragment.js → fragment.mjs} +35 -6
- package/dist/fragments.d.mts +16 -0
- package/dist/{fragments.js → fragments.mjs} +11 -6
- package/dist/helpers/dom.d.mts +7 -0
- package/dist/helpers/{dom.js → dom.mjs} +7 -2
- package/dist/helpers/index.d.mts +10 -0
- package/dist/helpers/{index.js → index.mjs} +5 -6
- package/dist/index.d.mts +21 -0
- package/dist/index.mjs +24 -0
- package/dist/models.d.mts +2 -0
- package/dist/models.mjs +1 -0
- package/dist/node/attribute/index.d.mts +6 -0
- package/dist/node/attribute/{index.js → index.mjs} +11 -4
- package/dist/node/attribute/value.d.mts +6 -0
- package/dist/node/attribute/{value.js → value.mjs} +13 -10
- package/dist/node/event.d.mts +4 -0
- package/dist/node/{event.js → event.mjs} +5 -3
- package/dist/node/index.d.mts +6 -0
- package/dist/node/{index.js → index.mjs} +9 -7
- package/dist/node/value.d.mts +7 -0
- package/dist/node/{value.js → value.mjs} +9 -6
- package/dist/parse.d.mts +6 -0
- package/dist/{parse.js → parse.mjs} +3 -1
- package/package.json +42 -41
- package/src/constants.ts +38 -4
- package/src/fragment.ts +2 -2
- package/src/fragments.ts +10 -4
- package/src/helpers/dom.ts +10 -0
- package/src/helpers/index.ts +13 -10
- package/src/node/attribute/index.ts +11 -1
- package/src/node/attribute/value.ts +18 -10
- package/src/node/event.ts +9 -4
- package/src/node/value.ts +3 -2
- package/dist/constants.js +0 -25
- package/dist/index.js +0 -11
- package/dist/models.js +0 -0
- package/types/constants.d.ts +0 -17
- package/types/fragment.d.ts +0 -38
- package/types/fragments.d.ts +0 -13
- package/types/helpers/dom.d.ts +0 -3
- package/types/helpers/index.d.ts +0 -5
- package/types/index.d.ts +0 -20
- package/types/models.d.ts +0 -41
- package/types/node/attribute/index.d.ts +0 -2
- package/types/node/attribute/value.d.ts +0 -2
- package/types/node/event.d.ts +0 -1
- package/types/node/index.d.ts +0 -2
- package/types/node/value.d.ts +0 -3
- package/types/parse.d.ts +0 -2
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/constants.d.ts
|
|
2
|
+
declare const ARRAY_COMPARISON_ADDED = "added";
|
|
3
|
+
declare const ARRAY_COMPARISON_DISSIMILAR = "dissimilar";
|
|
4
|
+
declare const ARRAY_COMPARISON_REMOVED = "removed";
|
|
5
|
+
declare const ATTRIBUTE_CLASS_PREFIX_LENGTH = 6;
|
|
6
|
+
declare const ATTRIBUTE_NAME_DELIMITER = ".";
|
|
7
|
+
declare const ERROR_FRAGMENT = "Fragment function must return a Fragment instance";
|
|
8
|
+
declare const ERROR_IDENTIFIER_DUPLICATE = "Duplicate identifier found: '<id>'";
|
|
9
|
+
declare const ERROR_IDENTIFIER_TYPE = "Identifier cannot be null or undefined";
|
|
10
|
+
declare const EVENT_CHANGE = "change";
|
|
11
|
+
declare const EVENT_INPUT = "input";
|
|
12
|
+
declare const EVENT_SUBMIT = "submit";
|
|
13
|
+
declare const EVENT_DEFAULTS: Record<string, string>;
|
|
14
|
+
declare const EVENT_ON_PREFIXED = "@on";
|
|
15
|
+
declare const EVENT_ON_VALUE = "on";
|
|
16
|
+
declare const EVENT_OPTIONS_DELIMITER = ":";
|
|
17
|
+
declare const EXPRESSION_ABYDON_ATTRIBUTE_FULL: RegExp;
|
|
18
|
+
declare const EXPRESSION_ABYDON_ATTRIBUTE_PREFIX: RegExp;
|
|
19
|
+
declare const EXPRESSION_ABYDON_CONTENT: RegExp;
|
|
20
|
+
declare const EXPRESSION_ATTRIBUTE_CLASS: RegExp;
|
|
21
|
+
declare const EXPRESSION_ATTRIBUTE_STYLE_FULL: RegExp;
|
|
22
|
+
declare const EXPRESSION_ATTRIBUTE_STYLE_PREFIX: RegExp;
|
|
23
|
+
declare const EXPRESSION_EVENT_CHANGE_TYPES: RegExp;
|
|
24
|
+
declare const EXPRESSION_EVENT_NAME: RegExp;
|
|
25
|
+
declare const EXPRESSION_EVENT_OPTIONS_ACTIVE: RegExp;
|
|
26
|
+
declare const EXPRESSION_EVENT_OPTIONS_CAPTURE: RegExp;
|
|
27
|
+
declare const EXPRESSION_EVENT_OPTIONS_ONCE: RegExp;
|
|
28
|
+
declare const EXPRESSION_EVENT_PREFIX: RegExp;
|
|
29
|
+
declare const EXPRESSION_PERIOD: RegExp;
|
|
30
|
+
declare const NAME_FRAGMENT = "$fragment";
|
|
31
|
+
declare const NAME_FRAGMENTS = "$fragments";
|
|
32
|
+
declare const PROPERTY_CHECKED = "checked";
|
|
33
|
+
declare const PROPERTY_IDENTIFIER = "identifier";
|
|
34
|
+
declare const PROPERTY_VALUE = "value";
|
|
35
|
+
declare const TEMPLATE_ITEM = "<>";
|
|
36
|
+
//#endregion
|
|
37
|
+
export { ARRAY_COMPARISON_ADDED, ARRAY_COMPARISON_DISSIMILAR, ARRAY_COMPARISON_REMOVED, ATTRIBUTE_CLASS_PREFIX_LENGTH, ATTRIBUTE_NAME_DELIMITER, ERROR_FRAGMENT, ERROR_IDENTIFIER_DUPLICATE, ERROR_IDENTIFIER_TYPE, EVENT_CHANGE, EVENT_DEFAULTS, EVENT_INPUT, EVENT_ON_PREFIXED, EVENT_ON_VALUE, EVENT_OPTIONS_DELIMITER, EVENT_SUBMIT, EXPRESSION_ABYDON_ATTRIBUTE_FULL, EXPRESSION_ABYDON_ATTRIBUTE_PREFIX, EXPRESSION_ABYDON_CONTENT, EXPRESSION_ATTRIBUTE_CLASS, EXPRESSION_ATTRIBUTE_STYLE_FULL, EXPRESSION_ATTRIBUTE_STYLE_PREFIX, EXPRESSION_EVENT_CHANGE_TYPES, EXPRESSION_EVENT_NAME, EXPRESSION_EVENT_OPTIONS_ACTIVE, EXPRESSION_EVENT_OPTIONS_CAPTURE, EXPRESSION_EVENT_OPTIONS_ONCE, EXPRESSION_EVENT_PREFIX, EXPRESSION_PERIOD, NAME_FRAGMENT, NAME_FRAGMENTS, PROPERTY_CHECKED, PROPERTY_IDENTIFIER, PROPERTY_VALUE, TEMPLATE_ITEM };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
const ARRAY_COMPARISON_ADDED = "added";
|
|
3
|
+
const ARRAY_COMPARISON_DISSIMILAR = "dissimilar";
|
|
4
|
+
const ARRAY_COMPARISON_REMOVED = "removed";
|
|
5
|
+
const ATTRIBUTE_CLASS_PREFIX_LENGTH = 6;
|
|
6
|
+
const ATTRIBUTE_NAME_DELIMITER = ".";
|
|
7
|
+
const ERROR_FRAGMENT = "Fragment function must return a Fragment instance";
|
|
8
|
+
const ERROR_IDENTIFIER_DUPLICATE = "Duplicate identifier found: '<id>'";
|
|
9
|
+
const ERROR_IDENTIFIER_TYPE = "Identifier cannot be null or undefined";
|
|
10
|
+
const EVENT_CHANGE = "change";
|
|
11
|
+
const EVENT_INPUT = "input";
|
|
12
|
+
const EVENT_SUBMIT = "submit";
|
|
13
|
+
const EVENT_DEFAULTS = {
|
|
14
|
+
A: "click",
|
|
15
|
+
BUTTON: "click",
|
|
16
|
+
DETAILS: "toggle",
|
|
17
|
+
FORM: EVENT_SUBMIT,
|
|
18
|
+
SELECT: EVENT_CHANGE,
|
|
19
|
+
TEXTAREA: EVENT_INPUT
|
|
20
|
+
};
|
|
21
|
+
const EVENT_ON_PREFIXED = "@on";
|
|
22
|
+
const EVENT_ON_VALUE = "on";
|
|
23
|
+
const EVENT_OPTIONS_DELIMITER = ":";
|
|
24
|
+
const EXPRESSION_ABYDON_ATTRIBUTE_FULL = /(@?[\w-]+)="<!--abydon.(\d+)-->"/g;
|
|
25
|
+
const EXPRESSION_ABYDON_ATTRIBUTE_PREFIX = /^_/;
|
|
26
|
+
const EXPRESSION_ABYDON_CONTENT = /^@?abydon\.(\d+)@?$/;
|
|
27
|
+
const EXPRESSION_ATTRIBUTE_CLASS = /^class\./;
|
|
28
|
+
const EXPRESSION_ATTRIBUTE_STYLE_FULL = /^style\.([\w-]+)(?:\.([\w-]+))?$/;
|
|
29
|
+
const EXPRESSION_ATTRIBUTE_STYLE_PREFIX = /^style\./;
|
|
30
|
+
const EXPRESSION_EVENT_CHANGE_TYPES = /^(checkbox|radio)$/;
|
|
31
|
+
const EXPRESSION_EVENT_NAME = /^@([\w-]+)(?::([a-z:]+))?$/i;
|
|
32
|
+
const EXPRESSION_EVENT_OPTIONS_ACTIVE = /^a(ctive)$/i;
|
|
33
|
+
const EXPRESSION_EVENT_OPTIONS_CAPTURE = /^c(apture)$/i;
|
|
34
|
+
const EXPRESSION_EVENT_OPTIONS_ONCE = /^o(nce)$/i;
|
|
35
|
+
const EXPRESSION_EVENT_PREFIX = /^@/;
|
|
36
|
+
const EXPRESSION_PERIOD = /\./;
|
|
37
|
+
const NAME_FRAGMENT = "$fragment";
|
|
38
|
+
const NAME_FRAGMENTS = "$fragments";
|
|
39
|
+
const PROPERTY_CHECKED = "checked";
|
|
40
|
+
const PROPERTY_IDENTIFIER = "identifier";
|
|
41
|
+
const PROPERTY_VALUE = "value";
|
|
42
|
+
const TEMPLATE_ITEM = "<>";
|
|
43
|
+
//#endregion
|
|
44
|
+
export { ARRAY_COMPARISON_ADDED, ARRAY_COMPARISON_DISSIMILAR, ARRAY_COMPARISON_REMOVED, ATTRIBUTE_CLASS_PREFIX_LENGTH, ATTRIBUTE_NAME_DELIMITER, ERROR_FRAGMENT, ERROR_IDENTIFIER_DUPLICATE, ERROR_IDENTIFIER_TYPE, EVENT_CHANGE, EVENT_DEFAULTS, EVENT_INPUT, EVENT_ON_PREFIXED, EVENT_ON_VALUE, EVENT_OPTIONS_DELIMITER, EVENT_SUBMIT, EXPRESSION_ABYDON_ATTRIBUTE_FULL, EXPRESSION_ABYDON_ATTRIBUTE_PREFIX, EXPRESSION_ABYDON_CONTENT, EXPRESSION_ATTRIBUTE_CLASS, EXPRESSION_ATTRIBUTE_STYLE_FULL, EXPRESSION_ATTRIBUTE_STYLE_PREFIX, EXPRESSION_EVENT_CHANGE_TYPES, EXPRESSION_EVENT_NAME, EXPRESSION_EVENT_OPTIONS_ACTIVE, EXPRESSION_EVENT_OPTIONS_CAPTURE, EXPRESSION_EVENT_OPTIONS_ONCE, EXPRESSION_EVENT_PREFIX, EXPRESSION_PERIOD, NAME_FRAGMENT, NAME_FRAGMENTS, PROPERTY_CHECKED, PROPERTY_IDENTIFIER, PROPERTY_VALUE, TEMPLATE_ITEM };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Reactive, ReactiveArray, Unsubscribe } from "@oscarpalmer/mora";
|
|
2
|
+
|
|
3
|
+
//#region src/models.d.ts
|
|
4
|
+
type FragmentConfiguration = {
|
|
5
|
+
/**
|
|
6
|
+
* Should the template be cached? _(defaults to `true`)_
|
|
7
|
+
*/
|
|
8
|
+
cache?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Identifier for the fragment
|
|
11
|
+
*
|
|
12
|
+
* _(An identifier can be used to uniquely identify a fragment,
|
|
13
|
+
* which helps prevent re-rendering in certain scenarios)_
|
|
14
|
+
*/
|
|
15
|
+
identifier?: unknown;
|
|
16
|
+
};
|
|
17
|
+
type FragmentData = {
|
|
18
|
+
expressions: unknown[];
|
|
19
|
+
items: FragmentItem[];
|
|
20
|
+
mora: MoraData;
|
|
21
|
+
strings: TemplateStringsArray;
|
|
22
|
+
template?: string;
|
|
23
|
+
values: unknown[];
|
|
24
|
+
};
|
|
25
|
+
type FragmentItem = {
|
|
26
|
+
fragments?: Fragment[];
|
|
27
|
+
nodes?: ChildNode[];
|
|
28
|
+
text?: Text;
|
|
29
|
+
};
|
|
30
|
+
type FragmentsState = {
|
|
31
|
+
array: ReactiveArray<unknown>;
|
|
32
|
+
fragment: (item: unknown) => Fragment;
|
|
33
|
+
identify: (item: unknown) => unknown;
|
|
34
|
+
instances: Record<string, Fragment>;
|
|
35
|
+
mapped: ReactiveArray<Fragment>;
|
|
36
|
+
subscriber: Unsubscribe | undefined;
|
|
37
|
+
};
|
|
38
|
+
type MoraData = {
|
|
39
|
+
subscribers: Set<() => void>;
|
|
40
|
+
values: Set<Reactive<unknown>>;
|
|
41
|
+
};
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/fragment.d.ts
|
|
44
|
+
declare class Fragment {
|
|
45
|
+
#private;
|
|
46
|
+
/**
|
|
47
|
+
* Fragment identifier
|
|
48
|
+
*/
|
|
49
|
+
get identifier(): unknown;
|
|
50
|
+
constructor(strings: TemplateStringsArray, expressions: unknown[]);
|
|
51
|
+
/**
|
|
52
|
+
* Append the fragment to the given element
|
|
53
|
+
* @param element Element to append to
|
|
54
|
+
*/
|
|
55
|
+
appendTo(element: Element): void;
|
|
56
|
+
/**
|
|
57
|
+
* Configure the fragment
|
|
58
|
+
* @param configuration Configuration options
|
|
59
|
+
* @returns Fragment
|
|
60
|
+
*/
|
|
61
|
+
configure(configuration: FragmentConfiguration): Fragment;
|
|
62
|
+
/**
|
|
63
|
+
* Get a list of the fragment's nodes
|
|
64
|
+
* @returns List of nodes
|
|
65
|
+
*/
|
|
66
|
+
get(): ChildNode[];
|
|
67
|
+
/**
|
|
68
|
+
* Set an identifier for the fragment
|
|
69
|
+
*
|
|
70
|
+
* _An identifier can be used to uniquely identify a fragment,
|
|
71
|
+
* which helps prevent re-rendering in certain scenarios._
|
|
72
|
+
* @param identifier Identifier
|
|
73
|
+
* @returns Fragment
|
|
74
|
+
*/
|
|
75
|
+
identify(identifier: unknown): Fragment;
|
|
76
|
+
/**
|
|
77
|
+
* Remove the fragment from the DOM
|
|
78
|
+
*/
|
|
79
|
+
remove(): void;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
export { FragmentsState as a, FragmentItem as i, FragmentConfiguration as n, FragmentData as r, Fragment as t };
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import { NAME_FRAGMENT } from "./constants.
|
|
2
|
-
import { isFragments } from "./helpers/index.
|
|
3
|
-
import { handleFragments } from "./fragments.
|
|
4
|
-
import { removeNodes } from "./helpers/dom.
|
|
5
|
-
import { mapNodes } from "./node/index.
|
|
6
|
-
import { parse } from "./parse.
|
|
1
|
+
import { NAME_FRAGMENT } from "./constants.mjs";
|
|
2
|
+
import { isFragments } from "./helpers/index.mjs";
|
|
3
|
+
import { handleFragments } from "./fragments.mjs";
|
|
4
|
+
import { removeNodes } from "./helpers/dom.mjs";
|
|
5
|
+
import { mapNodes } from "./node/index.mjs";
|
|
6
|
+
import { parse } from "./parse.mjs";
|
|
7
7
|
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
8
8
|
import { html } from "@oscarpalmer/toretto/html";
|
|
9
|
+
//#region src/fragment.ts
|
|
9
10
|
var Fragment = class {
|
|
10
11
|
#data;
|
|
11
12
|
#configuration = {
|
|
12
13
|
identifier: void 0,
|
|
13
14
|
cache: true
|
|
14
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Fragment identifier
|
|
18
|
+
*/
|
|
15
19
|
get identifier() {
|
|
16
20
|
return this.#configuration.identifier;
|
|
17
21
|
}
|
|
@@ -28,15 +32,28 @@ var Fragment = class {
|
|
|
28
32
|
values: []
|
|
29
33
|
};
|
|
30
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Append the fragment to the given element
|
|
37
|
+
* @param element Element to append to
|
|
38
|
+
*/
|
|
31
39
|
appendTo(element) {
|
|
32
40
|
element.append(...this.get());
|
|
33
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Configure the fragment
|
|
44
|
+
* @param configuration Configuration options
|
|
45
|
+
* @returns Fragment
|
|
46
|
+
*/
|
|
34
47
|
configure(configuration) {
|
|
35
48
|
const actual = isPlainObject(configuration) ? configuration : {};
|
|
36
49
|
if ("identifier" in actual) this.#configuration.identifier = actual.identifier;
|
|
37
50
|
if (typeof actual.cache === "boolean") this.#configuration.cache = actual.cache;
|
|
38
51
|
return this;
|
|
39
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Get a list of the fragment's nodes
|
|
55
|
+
* @returns List of nodes
|
|
56
|
+
*/
|
|
40
57
|
get() {
|
|
41
58
|
const data = this.#data;
|
|
42
59
|
if (data.items.length === 0) {
|
|
@@ -46,10 +63,21 @@ var Fragment = class {
|
|
|
46
63
|
}
|
|
47
64
|
return data.items.flatMap((item) => item.fragments?.flatMap((fragment) => fragment.get()) ?? item.nodes ?? []);
|
|
48
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Set an identifier for the fragment
|
|
68
|
+
*
|
|
69
|
+
* _An identifier can be used to uniquely identify a fragment,
|
|
70
|
+
* which helps prevent re-rendering in certain scenarios._
|
|
71
|
+
* @param identifier Identifier
|
|
72
|
+
* @returns Fragment
|
|
73
|
+
*/
|
|
49
74
|
identify(identifier) {
|
|
50
75
|
this.#configuration.identifier = identifier;
|
|
51
76
|
return this;
|
|
52
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Remove the fragment from the DOM
|
|
80
|
+
*/
|
|
53
81
|
remove() {
|
|
54
82
|
removeFragment(this.#data);
|
|
55
83
|
}
|
|
@@ -76,4 +104,5 @@ function removeMora(data) {
|
|
|
76
104
|
data.mora.values.clear();
|
|
77
105
|
for (const unsubscribe of unsubscribers) unsubscribe();
|
|
78
106
|
}
|
|
107
|
+
//#endregion
|
|
79
108
|
export { Fragment };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { a as FragmentsState, t as Fragment } from "./fragment-9qTBAvtR.mjs";
|
|
2
|
+
import { ReactiveArray } from "@oscarpalmer/mora";
|
|
3
|
+
|
|
4
|
+
//#region src/fragments.d.ts
|
|
5
|
+
declare class Fragments {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Fragment items
|
|
9
|
+
*/
|
|
10
|
+
get items(): ReactiveArray<Fragment>;
|
|
11
|
+
constructor(items: ReactiveArray<unknown>, identify: (item: unknown) => unknown, fragment: (item: unknown) => Fragment);
|
|
12
|
+
}
|
|
13
|
+
declare function handleFragments(item: Fragments | FragmentsState, remove: boolean): void;
|
|
14
|
+
declare function initializeFragments(state: FragmentsState): void;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { Fragments, handleFragments, initializeFragments };
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { NAME_FRAGMENTS } from "./constants.
|
|
2
|
-
import { isFragment, isFragments } from "./helpers/index.
|
|
1
|
+
import { ERROR_FRAGMENT, ERROR_IDENTIFIER_DUPLICATE, ERROR_IDENTIFIER_TYPE, NAME_FRAGMENTS } from "./constants.mjs";
|
|
2
|
+
import { isFragment, isFragments } from "./helpers/index.mjs";
|
|
3
3
|
import { getString } from "@oscarpalmer/atoms/string";
|
|
4
4
|
import { array } from "@oscarpalmer/mora";
|
|
5
|
+
//#region src/fragments.ts
|
|
5
6
|
var Fragments = class {
|
|
6
7
|
#state;
|
|
8
|
+
/**
|
|
9
|
+
* Fragment items
|
|
10
|
+
*/
|
|
7
11
|
get items() {
|
|
8
12
|
return this.#state.mapped;
|
|
9
13
|
}
|
|
@@ -32,13 +36,13 @@ function handleItems(state, items) {
|
|
|
32
36
|
for (let index = 0; index < length; index += 1) {
|
|
33
37
|
const item = items[index];
|
|
34
38
|
const identifier = state.identify(item);
|
|
35
|
-
if (identifier == null) throw new
|
|
39
|
+
if (identifier == null) throw new TypeError(ERROR_IDENTIFIER_TYPE);
|
|
36
40
|
const key = getString(identifier);
|
|
37
|
-
if (keys.has(key)) throw new Error(
|
|
41
|
+
if (keys.has(key)) throw new Error(ERROR_IDENTIFIER_DUPLICATE.replace("<>", key));
|
|
38
42
|
let instance = state.instances[key];
|
|
39
43
|
if (instance == null) {
|
|
40
44
|
instance = state.fragment(item);
|
|
41
|
-
if (!isFragment(instance)) throw new Error(
|
|
45
|
+
if (!isFragment(instance)) throw new Error(ERROR_FRAGMENT);
|
|
42
46
|
}
|
|
43
47
|
instance.identify(key);
|
|
44
48
|
state.instances[key] = instance;
|
|
@@ -71,5 +75,6 @@ function updateFragments(state, active) {
|
|
|
71
75
|
state.instances = next;
|
|
72
76
|
active?.clear();
|
|
73
77
|
}
|
|
74
|
-
|
|
78
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
79
|
+
//#endregion
|
|
75
80
|
export { Fragments, handleFragments, initializeFragments };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/helpers/dom.d.ts
|
|
2
|
+
declare function createNodes(value: unknown): ChildNode[];
|
|
3
|
+
declare function isInputElement(node: Node): node is HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
4
|
+
declare function removeNodes(nodes: ChildNode[]): void;
|
|
5
|
+
declare function replaceNodes(from: ChildNode[], to: ChildNode[]): void;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { createNodes, isInputElement, removeNodes, replaceNodes };
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import { isFragment } from "./index.
|
|
1
|
+
import { isFragment } from "./index.mjs";
|
|
2
2
|
import { getString } from "@oscarpalmer/atoms/string";
|
|
3
3
|
import { isChildNode } from "@oscarpalmer/toretto/is";
|
|
4
|
+
//#region src/helpers/dom.ts
|
|
4
5
|
function createNodes(value) {
|
|
5
6
|
if (isFragment(value)) return value.get();
|
|
6
7
|
if (isChildNode(value)) return [value];
|
|
7
8
|
return [new Text(getString(value))];
|
|
8
9
|
}
|
|
10
|
+
function isInputElement(node) {
|
|
11
|
+
return node instanceof HTMLInputElement || node instanceof HTMLSelectElement || node instanceof HTMLTextAreaElement;
|
|
12
|
+
}
|
|
9
13
|
function removeNodes(nodes) {
|
|
10
14
|
const { length } = nodes;
|
|
11
15
|
for (let index = 0; index < length; index += 1) nodes[index].remove();
|
|
@@ -15,4 +19,5 @@ function replaceNodes(from, to) {
|
|
|
15
19
|
const { length } = from;
|
|
16
20
|
for (let index = 1; index < length; index += 1) from[index].remove();
|
|
17
21
|
}
|
|
18
|
-
|
|
22
|
+
//#endregion
|
|
23
|
+
export { createNodes, isInputElement, removeNodes, replaceNodes };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ARRAY_COMPARISON_ADDED, ARRAY_COMPARISON_DISSIMILAR, ARRAY_COMPARISON_REMOVED } from "../constants.mjs";
|
|
2
|
+
import { t as Fragment } from "../fragment-9qTBAvtR.mjs";
|
|
3
|
+
import { Fragments } from "../fragments.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/helpers/index.d.ts
|
|
6
|
+
declare function compareArrays(first: unknown[], second: unknown[]): typeof ARRAY_COMPARISON_ADDED | typeof ARRAY_COMPARISON_DISSIMILAR | typeof ARRAY_COMPARISON_REMOVED;
|
|
7
|
+
declare function isFragment(value: unknown): value is Fragment;
|
|
8
|
+
declare function isFragments(value: unknown): value is Fragments;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { compareArrays, isFragment, isFragments };
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { NAME_FRAGMENT, NAME_FRAGMENTS } from "../constants.
|
|
1
|
+
import { ARRAY_COMPARISON_ADDED, ARRAY_COMPARISON_DISSIMILAR, ARRAY_COMPARISON_REMOVED, NAME_FRAGMENT, NAME_FRAGMENTS } from "../constants.mjs";
|
|
2
|
+
//#region src/helpers/index.ts
|
|
2
3
|
function compareArrays(first, second) {
|
|
3
4
|
const firstIsLarger = first.length > second.length;
|
|
4
5
|
const from = firstIsLarger ? first : second;
|
|
5
6
|
const to = firstIsLarger ? second : first;
|
|
6
|
-
if (!from.filter((key) => to.includes(key)).every((key, index) => to[index] === key)) return
|
|
7
|
-
return firstIsLarger ?
|
|
7
|
+
if (!from.filter((key) => to.includes(key)).every((key, index) => to[index] === key)) return ARRAY_COMPARISON_DISSIMILAR;
|
|
8
|
+
return firstIsLarger ? ARRAY_COMPARISON_REMOVED : ARRAY_COMPARISON_ADDED;
|
|
8
9
|
}
|
|
9
10
|
function isFragment(value) {
|
|
10
11
|
return isNamed(value, NAME_FRAGMENT);
|
|
@@ -15,7 +16,5 @@ function isFragments(value) {
|
|
|
15
16
|
function isNamed(value, name) {
|
|
16
17
|
return typeof value === "object" && value != null && name in value && value[name] === true;
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
-
var COMPARISON_DISSIMILAR = "dissimilar";
|
|
20
|
-
var COMPARISON_REMOVED = "removed";
|
|
19
|
+
//#endregion
|
|
21
20
|
export { compareArrays, isFragment, isFragments };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { t as Fragment } from "./fragment-9qTBAvtR.mjs";
|
|
2
|
+
import { Fragments } from "./fragments.mjs";
|
|
3
|
+
import { ReactiveArray } from "@oscarpalmer/mora";
|
|
4
|
+
export * from "@oscarpalmer/mora";
|
|
5
|
+
|
|
6
|
+
//#region src/index.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Create Fragments from a reactive array
|
|
9
|
+
* @param array Reactive array
|
|
10
|
+
* @param identify Function to identify item
|
|
11
|
+
* @param fragment Function to create fragment from item
|
|
12
|
+
* @returns Fragments
|
|
13
|
+
*/
|
|
14
|
+
declare function fragments<Item>(array: ReactiveArray<Item>, identify: (item: Item) => unknown, fragment: (item: Item) => Fragment): Fragments;
|
|
15
|
+
/**
|
|
16
|
+
* Create Fragment from a template
|
|
17
|
+
* @returns Fragment
|
|
18
|
+
*/
|
|
19
|
+
declare function html(template: TemplateStringsArray, ...values: unknown[]): Fragment;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { type Fragment, type Fragments, fragments, html };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Fragments } from "./fragments.mjs";
|
|
2
|
+
import { Fragment } from "./fragment.mjs";
|
|
3
|
+
import "@oscarpalmer/mora";
|
|
4
|
+
export * from "@oscarpalmer/mora";
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
/**
|
|
7
|
+
* Create Fragments from a reactive array
|
|
8
|
+
* @param array Reactive array
|
|
9
|
+
* @param identify Function to identify item
|
|
10
|
+
* @param fragment Function to create fragment from item
|
|
11
|
+
* @returns Fragments
|
|
12
|
+
*/
|
|
13
|
+
function fragments(array, identify, fragment) {
|
|
14
|
+
return new Fragments(array, identify, fragment);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create Fragment from a template
|
|
18
|
+
* @returns Fragment
|
|
19
|
+
*/
|
|
20
|
+
function html(template, ...values) {
|
|
21
|
+
return new Fragment(template, values);
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { fragments, html };
|
package/dist/models.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { EXPRESSION_ABYDON_ATTRIBUTE_PREFIX, EXPRESSION_ABYDON_CONTENT, EXPRESSION_EVENT_PREFIX, EXPRESSION_PERIOD } from "../../constants.
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { EXPRESSION_ABYDON_ATTRIBUTE_PREFIX, EXPRESSION_ABYDON_CONTENT, EXPRESSION_EVENT_PREFIX, EXPRESSION_PERIOD } from "../../constants.mjs";
|
|
2
|
+
import { isInputElement } from "../../helpers/dom.mjs";
|
|
3
|
+
import { mapEvent } from "../event.mjs";
|
|
4
|
+
import { setAttribute } from "./value.mjs";
|
|
5
|
+
import { parse } from "@oscarpalmer/atoms/string";
|
|
6
|
+
import { computed, isReactive, isSignal } from "@oscarpalmer/mora";
|
|
7
|
+
//#region src/node/attribute/index.ts
|
|
5
8
|
function getValue(data, original) {
|
|
6
9
|
const matches = EXPRESSION_ABYDON_CONTENT.exec(original ?? "");
|
|
7
10
|
return matches == null ? original : data.values[+matches[1]];
|
|
@@ -30,10 +33,14 @@ function mapAttributes(data, element) {
|
|
|
30
33
|
function mapValue(data, element, name, value) {
|
|
31
34
|
if (typeof value === "function") setComputedAttribute(data, element, name, value);
|
|
32
35
|
else setAttribute(data, element, name, value);
|
|
36
|
+
if (name === "value" && isInputElement(element) && isSignal(value)) mapEvent(element, "@on", () => {
|
|
37
|
+
value.set(parse(element.value) ?? element.value);
|
|
38
|
+
});
|
|
33
39
|
}
|
|
34
40
|
function setComputedAttribute(data, element, name, callback) {
|
|
35
41
|
const value = computed(callback);
|
|
36
42
|
data.mora.values.add(value);
|
|
37
43
|
setAttribute(data, element, name, value);
|
|
38
44
|
}
|
|
45
|
+
//#endregion
|
|
39
46
|
export { mapAttributes };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { r as FragmentData } from "../../fragment-9qTBAvtR.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/node/attribute/value.d.ts
|
|
4
|
+
declare function setAttribute(data: FragmentData, element: HTMLElement | SVGElement, name: string, value: unknown): void;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { setAttribute };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EVENT_CHANGE, EVENT_INPUT, EXPRESSION_ATTRIBUTE_CLASS, EXPRESSION_ATTRIBUTE_STYLE_FULL, EXPRESSION_ATTRIBUTE_STYLE_PREFIX, PROPERTY_CHECKED, PROPERTY_VALUE } from "../../constants.mjs";
|
|
2
2
|
import { isReactive } from "@oscarpalmer/mora";
|
|
3
|
-
import { isBooleanAttribute, setAttribute as setAttribute$1
|
|
3
|
+
import { isBooleanAttribute, setAttribute as setAttribute$1 } from "@oscarpalmer/toretto/attribute";
|
|
4
|
+
//#region src/node/attribute/value.ts
|
|
4
5
|
function getCallback(element, name) {
|
|
5
6
|
if (isBooleanAttribute(name) && name in element) return name === "checked" ? updateChecked : updateProperty;
|
|
6
7
|
return name === "value" ? updateValue : setAttribute$1;
|
|
@@ -19,8 +20,8 @@ function setAttribute(data, element, name, value) {
|
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
function setClasses(data, element, name, value) {
|
|
22
|
-
function update(value
|
|
23
|
-
if (value
|
|
23
|
+
function update(value) {
|
|
24
|
+
if (value === true) element.classList.add(...classes);
|
|
24
25
|
else element.classList.remove(...classes);
|
|
25
26
|
}
|
|
26
27
|
const classes = name.slice(6).split(".");
|
|
@@ -30,9 +31,9 @@ function setClasses(data, element, name, value) {
|
|
|
30
31
|
function setStyle(data, element, name, value) {
|
|
31
32
|
const [, property, unit] = EXPRESSION_ATTRIBUTE_STYLE_FULL.exec(name) ?? [];
|
|
32
33
|
if (property == null) return;
|
|
33
|
-
function update(value
|
|
34
|
-
if (value
|
|
35
|
-
else element.style.setProperty(property, value
|
|
34
|
+
function update(value) {
|
|
35
|
+
if (value == null || value === false || value === true && unit == null) element.style.removeProperty(property);
|
|
36
|
+
else element.style.setProperty(property, value === true ? unit : String(value));
|
|
36
37
|
}
|
|
37
38
|
if (isReactive(value)) data.mora.subscribers.add(value.subscribe(update));
|
|
38
39
|
else update(value);
|
|
@@ -45,18 +46,20 @@ function setValue(data, element, name, value) {
|
|
|
45
46
|
else callback(element, name, value);
|
|
46
47
|
}
|
|
47
48
|
function updateChecked(element, name, value) {
|
|
48
|
-
updateElement(
|
|
49
|
+
updateElement(EVENT_CHANGE, PROPERTY_CHECKED, element, name, value, value === true);
|
|
49
50
|
}
|
|
50
51
|
function updateElement(event, property, element, name, value, next) {
|
|
51
52
|
if (!(property in element) || element[property] === next) return;
|
|
52
53
|
setAttribute$1(element, name, value);
|
|
54
|
+
if (element[property] === next) return;
|
|
53
55
|
element[property] = next;
|
|
54
56
|
element.dispatchEvent(new Event(event, { bubbles: true }));
|
|
55
57
|
}
|
|
56
58
|
function updateProperty(element, name, value) {
|
|
57
|
-
|
|
59
|
+
setAttribute$1(element, name, value === true);
|
|
58
60
|
}
|
|
59
61
|
function updateValue(element, name, value) {
|
|
60
|
-
updateElement(element instanceof HTMLSelectElement ?
|
|
62
|
+
updateElement(element instanceof HTMLSelectElement ? EVENT_CHANGE : EVENT_INPUT, PROPERTY_VALUE, element, name, value, String(value));
|
|
61
63
|
}
|
|
64
|
+
//#endregion
|
|
62
65
|
export { setAttribute };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { EVENT_DEFAULTS, EXPRESSION_EVENT_CHANGE_TYPES, EXPRESSION_EVENT_NAME, EXPRESSION_EVENT_OPTIONS_ACTIVE, EXPRESSION_EVENT_OPTIONS_CAPTURE, EXPRESSION_EVENT_OPTIONS_ONCE } from "../constants.
|
|
1
|
+
import { EVENT_CHANGE, EVENT_DEFAULTS, EVENT_INPUT, EVENT_SUBMIT, EXPRESSION_EVENT_CHANGE_TYPES, EXPRESSION_EVENT_NAME, EXPRESSION_EVENT_OPTIONS_ACTIVE, EXPRESSION_EVENT_OPTIONS_CAPTURE, EXPRESSION_EVENT_OPTIONS_ONCE } from "../constants.mjs";
|
|
2
2
|
import { on } from "@oscarpalmer/toretto/event";
|
|
3
|
+
//#region src/node/event.ts
|
|
3
4
|
function getOptions(options) {
|
|
4
5
|
const parts = options.split(":");
|
|
5
6
|
return {
|
|
@@ -11,8 +12,8 @@ function getOptions(options) {
|
|
|
11
12
|
function getType(element, type) {
|
|
12
13
|
if (type !== "on") return type;
|
|
13
14
|
if (element instanceof HTMLInputElement) {
|
|
14
|
-
if (EXPRESSION_EVENT_CHANGE_TYPES.test(element.type)) return
|
|
15
|
-
return element.type === "submit" ?
|
|
15
|
+
if (EXPRESSION_EVENT_CHANGE_TYPES.test(element.type)) return EVENT_CHANGE;
|
|
16
|
+
return element.type === "submit" ? EVENT_SUBMIT : EVENT_INPUT;
|
|
16
17
|
}
|
|
17
18
|
return EVENT_DEFAULTS[element.tagName] ?? type;
|
|
18
19
|
}
|
|
@@ -20,4 +21,5 @@ function mapEvent(element, name, value) {
|
|
|
20
21
|
const [, type, options] = EXPRESSION_EVENT_NAME.exec(name) ?? [];
|
|
21
22
|
if (type != null && typeof value === "function") on(element, getType(element, type), value, getOptions(options ?? ""));
|
|
22
23
|
}
|
|
24
|
+
//#endregion
|
|
23
25
|
export { mapEvent };
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { EXPRESSION_ABYDON_CONTENT } from "../constants.
|
|
2
|
-
import { isFragment, isFragments } from "../helpers/index.
|
|
3
|
-
import { handleFragments } from "../fragments.
|
|
4
|
-
import { createNodes } from "../helpers/dom.
|
|
5
|
-
import { mapAttributes } from "./attribute/index.
|
|
6
|
-
import { setReactiveValue } from "./value.
|
|
1
|
+
import { EXPRESSION_ABYDON_CONTENT } from "../constants.mjs";
|
|
2
|
+
import { isFragment, isFragments } from "../helpers/index.mjs";
|
|
3
|
+
import { handleFragments } from "../fragments.mjs";
|
|
4
|
+
import { createNodes } from "../helpers/dom.mjs";
|
|
5
|
+
import { mapAttributes } from "./attribute/index.mjs";
|
|
6
|
+
import { setReactiveValue } from "./value.mjs";
|
|
7
7
|
import { computed, isReactive } from "@oscarpalmer/mora";
|
|
8
8
|
import { isHTMLOrSVGElement } from "@oscarpalmer/toretto/is";
|
|
9
|
+
//#region src/node/index.ts
|
|
9
10
|
function mapNode(data, comment) {
|
|
10
11
|
const matches = EXPRESSION_ABYDON_CONTENT.exec(comment.textContent ?? "");
|
|
11
12
|
const value = matches == null ? null : data.values[+matches[1]];
|
|
@@ -41,7 +42,7 @@ function mapValue(data, comment, value) {
|
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
function replaceComment(data, comment, value) {
|
|
44
|
-
const item = data.items.find((item
|
|
45
|
+
const item = data.items.find((item) => item.nodes?.includes(comment));
|
|
45
46
|
const nodes = createNodes(value);
|
|
46
47
|
if (item != null) {
|
|
47
48
|
item.fragments = isFragment(value) ? [value] : void 0;
|
|
@@ -54,4 +55,5 @@ function setComputedValue(data, comment, callback) {
|
|
|
54
55
|
data.mora.values.add(value);
|
|
55
56
|
setReactiveValue(data, comment, value);
|
|
56
57
|
}
|
|
58
|
+
//#endregion
|
|
57
59
|
export { mapNodes };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { r as FragmentData } from "../fragment-9qTBAvtR.mjs";
|
|
2
|
+
import { Reactive } from "@oscarpalmer/mora";
|
|
3
|
+
|
|
4
|
+
//#region src/node/value.d.ts
|
|
5
|
+
declare function setReactiveValue(data: FragmentData, comment: Comment, reactive: Reactive<unknown>): void;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { setReactiveValue };
|