jaxs 0.2.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 (67) hide show
  1. package/.env +1 -0
  2. package/.parcel-cache/6f14daf302269614-BundleGraph-0 +0 -0
  3. package/.parcel-cache/7d3d872b02d671a6-AssetGraph-0 +0 -0
  4. package/.parcel-cache/8e029bb14f8992df-RequestGraph-0 +0 -0
  5. package/.parcel-cache/a5394978e4ece10b-AssetGraph-0 +0 -0
  6. package/.parcel-cache/b5686051ae060930.txt +2 -0
  7. package/.parcel-cache/data.mdb +0 -0
  8. package/.parcel-cache/lock.mdb +0 -0
  9. package/README.md +15 -0
  10. package/bun.lockb +0 -0
  11. package/bundle.ts +5 -0
  12. package/bunfig.toml +0 -0
  13. package/cypress/e2e/add-remove-nested-children.cy.js +39 -0
  14. package/cypress/e2e/add-remove-root-children.cy.js +37 -0
  15. package/cypress/jaxs-apps/add-remove-nested-children.html +12 -0
  16. package/cypress/jaxs-apps/add-remove-nested-children.jsx +84 -0
  17. package/cypress/jaxs-apps/add-remove-root-children.html +15 -0
  18. package/cypress/jaxs-apps/add-remove-root-children.jsx +53 -0
  19. package/cypress/jaxs-apps/dist/add-remove-nested-children.afcab974.js +717 -0
  20. package/cypress/jaxs-apps/dist/add-remove-nested-children.afcab974.js.map +1 -0
  21. package/cypress/jaxs-apps/dist/add-remove-nested-children.html +12 -0
  22. package/cypress/jaxs-apps/dist/add-remove-root-children.3bb9b3f5.js +1682 -0
  23. package/cypress/jaxs-apps/dist/add-remove-root-children.3bb9b3f5.js.map +1 -0
  24. package/cypress/jaxs-apps/dist/add-remove-root-children.fbb4ec9b.js +706 -0
  25. package/cypress/jaxs-apps/dist/add-remove-root-children.fbb4ec9b.js.map +1 -0
  26. package/cypress/jaxs-apps/dist/add-remove-root-children.html +15 -0
  27. package/cypress/support/commands.js +25 -0
  28. package/cypress/support/e2e.js +20 -0
  29. package/cypress.config.js +10 -0
  30. package/dist/jaxs.js +1154 -0
  31. package/package.json +38 -0
  32. package/src/app.ts +64 -0
  33. package/src/debugging.js +5 -0
  34. package/src/jaxs.ts +8 -0
  35. package/src/jsx.js +27 -0
  36. package/src/messageBus.ts +70 -0
  37. package/src/navigation/findHref.js +10 -0
  38. package/src/navigation/routeState.js +15 -0
  39. package/src/navigation/setupHistory.js +38 -0
  40. package/src/navigation/setupNavigation.js +25 -0
  41. package/src/navigation.ts +2 -0
  42. package/src/rendering/change/compile.ts +1 -0
  43. package/src/rendering/change/instructions/attributes.ts +78 -0
  44. package/src/rendering/change/instructions/children.ts +128 -0
  45. package/src/rendering/change/instructions/element.ts +42 -0
  46. package/src/rendering/change/instructions/events.ts +51 -0
  47. package/src/rendering/change/instructions/generate.ts +122 -0
  48. package/src/rendering/change/instructions/idMap.js +55 -0
  49. package/src/rendering/change/instructions/node.ts +38 -0
  50. package/src/rendering/change/instructions/text.ts +10 -0
  51. package/src/rendering/change.ts +131 -0
  52. package/src/rendering/dom/attributesAndEvents.ts +33 -0
  53. package/src/rendering/dom/create.ts +68 -0
  54. package/src/rendering/templates/bound.js +55 -0
  55. package/src/rendering/templates/children.ts +91 -0
  56. package/src/rendering/templates/root.ts +55 -0
  57. package/src/rendering/templates/tag.ts +70 -0
  58. package/src/rendering/templates/text.ts +17 -0
  59. package/src/state/equality.js +36 -0
  60. package/src/state/stores.js +63 -0
  61. package/src/state/testingTypes.js +6 -0
  62. package/src/state.js +89 -0
  63. package/src/types.ts +149 -0
  64. package/src/views/conditionals.jsx +18 -0
  65. package/src/views/link.jsx +5 -0
  66. package/src/views.js +7 -0
  67. package/tsconfig.json +26 -0
@@ -0,0 +1,70 @@
1
+ import type {
2
+ Attributes,
3
+ DomCollection,
4
+ EventAttributes,
5
+ RenderKit,
6
+ Template,
7
+ } from '../../types';
8
+
9
+ import { createDecoratedNode } from '../dom/create';
10
+ import { separateAttrsAndEvents } from '../dom/attributesAndEvents';
11
+ import { Children } from './children';
12
+
13
+ export class Tag implements Template {
14
+ type: string;
15
+ events: EventAttributes;
16
+ attributes: Attributes;
17
+ children: Children;
18
+
19
+ constructor(
20
+ tagType: string,
21
+ combinedAttributes: Attributes,
22
+ children: Template[],
23
+ ) {
24
+ this.type = tagType;
25
+
26
+ const { events, attributes } = separateAttrsAndEvents(combinedAttributes);
27
+ this.events = events;
28
+ this.attributes = attributes;
29
+
30
+ this.children = new Children(children);
31
+ }
32
+
33
+ render(renderKit: RenderKit): DomCollection {
34
+ const dom = this.generateDom(renderKit);
35
+ if (!dom) return [];
36
+
37
+ this.children.render(renderKit, dom);
38
+ return [dom];
39
+ }
40
+
41
+ generateDom(renderKit: RenderKit) {
42
+ const node = createDecoratedNode(
43
+ this.type,
44
+ this.attributes,
45
+ this.events,
46
+ renderKit,
47
+ );
48
+ node.__jsx = this.key();
49
+ return node;
50
+ }
51
+
52
+ key() {
53
+ return this.attributes.key || this.source() || this.createKey();
54
+ }
55
+
56
+ source() {
57
+ if (this.attributes.__source) {
58
+ const { fileName, lineNumber, columnNumber } = this.attributes.__source;
59
+ return `${fileName}:${lineNumber}:${columnNumber}`;
60
+ }
61
+ }
62
+
63
+ createKey() {
64
+ const id = this.attributes.id ? `#${this.attributes.id}` : '';
65
+ const type = this.attributes.type ? `[type=${this.attributes.type}]` : '';
66
+ const name = this.attributes.name ? `[name=${this.attributes.name}]` : '';
67
+
68
+ return `${this.type}${id}${type}${name}`;
69
+ }
70
+ }
@@ -0,0 +1,17 @@
1
+ import type { JsxId, RenderKit, Template, TextValue } from '../../types';
2
+ import { createTextNode } from '../dom/create';
3
+
4
+ export class TextTemplate implements Template {
5
+ value: string;
6
+
7
+ constructor(content: TextValue) {
8
+ this.value = content.toString();
9
+ }
10
+
11
+ render(renderKit: RenderKit) {
12
+ const textNode = createTextNode(this.value, renderKit.document);
13
+ if (!textNode) return [];
14
+ (textNode as unknown as JsxId).__jsx = 'TextNode';
15
+ return [textNode];
16
+ }
17
+ }
@@ -0,0 +1,36 @@
1
+ import { isObject, isArray } from './testingTypes'
2
+
3
+ export const areElementEqual = (oldValue, newValue) => oldValue === newValue
4
+
5
+ const keyLengthSame = (oldValue, newValue) =>
6
+ Object.keys(oldValue).length === Object.keys(newValue).length
7
+
8
+ export const areObjectsEqual = (oldValue, newValue) => {
9
+ if (!(isObject(oldValue) && isObject(newValue))) return false
10
+ if (!keyLengthSame(oldValue, newValue)) return false
11
+
12
+ Object.keys(oldValue).every((key) => {
13
+ const oldInnerValue = oldValue[key]
14
+ const newInnerValue = newValue[key]
15
+
16
+ return areEqual(oldInnerValue, newInnerValue)
17
+ })
18
+ }
19
+
20
+ export const areArraysEqual = (oldValue, newValue) => {
21
+ if (!(isArray(oldValue) && isArray(newValue))) return false
22
+ if (oldValue.length !== newValue.length) return false
23
+
24
+ oldValue.every((oldInnerValue, index) => {
25
+ const newInnerValue = newValue[index]
26
+
27
+ return areEqual(oldInnerValue, newInnerValue)
28
+ })
29
+ }
30
+
31
+ export const areEqual = (oldValue, newValue) => {
32
+ if (isObject(oldValue)) return areObjectsEqual(oldValue, newValue)
33
+ if (isArray(oldValue)) return areArraysEqual(oldValue, newValue)
34
+
35
+ return areElementEqual(oldValue, newValue)
36
+ }
@@ -0,0 +1,63 @@
1
+ import { areElementEqual, areArraysEqual, areObjectsEqual } from './equality'
2
+
3
+ export class GeneralStore {
4
+ nullEvent = {}
5
+
6
+ constructor ({ name, value, parent }) {
7
+ this.name = name
8
+ this.value = value
9
+ this.parent = parent
10
+ this.initialState = value
11
+ }
12
+
13
+ update (newValue) {
14
+ if (this.isEqual(newValue)) return
15
+
16
+ this.value = newValue
17
+ return this.parent.publish(this.event())
18
+ }
19
+
20
+ reset () {
21
+ this.update(this.initialState)
22
+ }
23
+
24
+ isEqual (newValue) {
25
+ return areElementEqual(this.value, newValue)
26
+ }
27
+
28
+ event () {
29
+ return {
30
+ name: this.name,
31
+ value: this.value
32
+ }
33
+ }
34
+ }
35
+
36
+ export class BooleanStore extends GeneralStore {
37
+ toggle () {
38
+ this.update(!this.value)
39
+ }
40
+ }
41
+
42
+ export class NumericStore extends GeneralStore {
43
+ }
44
+
45
+ export class StringStore extends GeneralStore {
46
+ }
47
+
48
+ export class ListStore extends GeneralStore {
49
+ isEqual (newValue) {
50
+ return areArraysEqual(this.value, newValue)
51
+ }
52
+
53
+ push (newValue) {
54
+ const value = [...this.value, newValue]
55
+ this.update(value)
56
+ }
57
+ }
58
+
59
+ export class RecordStore extends GeneralStore {
60
+ isEqual (newValue) {
61
+ return areObjectsEqual(this.value, newValue)
62
+ }
63
+ }
@@ -0,0 +1,6 @@
1
+ export const isBoolean = (value) => typeof (value) === 'boolean'
2
+ export const isNumber = (value) => typeof (value) === 'number'
3
+ export const isString = (value) => typeof (value) === 'string'
4
+ export const isArray = (value) => Array.isArray(value)
5
+ export const isObject = (value) =>
6
+ value !== null && !isArray(value) && typeof (value) === 'object'
package/src/state.js ADDED
@@ -0,0 +1,89 @@
1
+ import {
2
+ isArray,
3
+ isObject,
4
+ isBoolean,
5
+ isNumber,
6
+ isString
7
+ } from './state/testingTypes'
8
+
9
+ import {
10
+ GeneralStore,
11
+ ListStore,
12
+ NumericStore,
13
+ BooleanStore,
14
+ StringStore,
15
+ RecordStore
16
+ } from './state/stores'
17
+
18
+ const eventPrefix = 'state-change'
19
+ export const eventName = (name) => `${eventPrefix}:${name}`
20
+
21
+ export class State {
22
+ constructor (publish) {
23
+ this.publisher = publish
24
+ this.stores = {}
25
+ this.events = []
26
+ this.transacting = false
27
+ }
28
+
29
+ create (name, value) {
30
+ const StoreClass = this.storeTypeFor(value)
31
+ const store = new StoreClass({ name, value, parent: this })
32
+ this.addStore(name, store)
33
+ }
34
+
35
+ add (store) {
36
+ const name = store.name
37
+ this.addStore(name, store)
38
+ }
39
+
40
+ getStore (name) {
41
+ return this.stores[name]
42
+ }
43
+
44
+ addStore (name, store) {
45
+ this.stores[name] = store
46
+ this[name] = store
47
+ }
48
+
49
+ storeTypeFor (value) {
50
+ if (isArray(value)) return ListStore
51
+ if (isObject(value)) return RecordStore
52
+ if (isNumber(value)) return NumericStore
53
+ if (isBoolean(value)) return BooleanStore
54
+ if (isString(value)) return StringStore
55
+
56
+ return GeneralStore
57
+ }
58
+
59
+ publish (event) {
60
+ this.events.push(event)
61
+ if (!this.transacting) this.publishAll()
62
+ }
63
+
64
+ publishAll () {
65
+ const publishedStores = []
66
+ this.events.reverse().forEach((event) => {
67
+ const { name, value } = event
68
+ if (!publishedStores.includes(name)) {
69
+ publishedStores.push(name)
70
+ this.publisher(`${eventPrefix}:${name}`, value)
71
+ }
72
+ })
73
+ this.events = []
74
+ }
75
+
76
+ transaction (setter) {
77
+ this.transacting = true
78
+ setter(this)
79
+ this.transacting = false
80
+ this.publishAll()
81
+ }
82
+
83
+ value () {
84
+ return Object.keys(this.stores).reduce((valueObject, key) => {
85
+ valueObject[key] = this.stores[key].value
86
+ return valueObject
87
+ }, {})
88
+ }
89
+ }
package/src/types.ts ADDED
@@ -0,0 +1,149 @@
1
+ // Rendering & Dom ------
2
+ import { State } from './state'
3
+ export type DomEventPublisher = (eventName: string, domEvent: Event) => void;
4
+
5
+ export type RenderKit = {
6
+ document: Document;
7
+ publish: DomEventPublisher;
8
+ subscribe: BusSubscribe;
9
+ state: State;
10
+ parent?: Element | null;
11
+ };
12
+
13
+ export interface JsxId {
14
+ __jsx: string;
15
+ }
16
+ export interface ExpandedElement extends Element, JsxId {
17
+ eventMaps: EventMaps;
18
+ }
19
+ export interface InputElement extends ExpandedElement {
20
+ // deno-lint-ignore no-explicit-any
21
+ value: any;
22
+ }
23
+ export type Dom = Text | ExpandedElement;
24
+ export type DomCollection = Dom[];
25
+ export type HtmlChildren =
26
+ | HTMLCollection
27
+ | NodeListOf<ChildNode>
28
+ | DomCollection;
29
+ export type TextValue = string | number;
30
+ export type EventMap = {
31
+ domEvent: string;
32
+ busEvent: string;
33
+ listener: EventListener;
34
+ };
35
+ export type EventMaps = Record<string, EventMap>;
36
+
37
+ // deno-lint-ignore no-explicit-any
38
+ export type Attributes = Record<string, any>;
39
+ export type EventAttributes = Record<string, string>;
40
+ export type AttributesAndEvents = {
41
+ attributes: Attributes;
42
+ events: EventAttributes;
43
+ };
44
+
45
+ export type TemplateEventListeners = Record<string, EventListener>;
46
+
47
+ export interface Template {
48
+ render: (
49
+ renderKit: RenderKit,
50
+ parentElement?: Element,
51
+ ) => DomCollection;
52
+ }
53
+ export type TemplateClass = (attributes: Attributes) => Template;
54
+
55
+ export type ViewModel = (state: Record<string,any>) => State;
56
+
57
+ // Message Bus ----
58
+ export type BusEventName = string;
59
+ // deno-lint-ignore no-explicit-any
60
+ export type BusOptions = Record<string, any>;
61
+ // deno-lint-ignore no-explicit-any
62
+ export type BusPayload = any;
63
+ export type BusPublish = (eventName: BusEventName, payload: BusPayload) => void;
64
+ export type BusSubscribe = (
65
+ eventName: BusEventName,
66
+ listener: BusListener,
67
+ ) => void;
68
+ export interface BusListenerKit {
69
+ publish: BusPublish;
70
+ eventName: BusEventName;
71
+ // deno-lint-ignore no-explicit-any
72
+ [key: string]: any;
73
+ }
74
+ export type BusListener = (
75
+ payload: BusPayload,
76
+ listenerKit: BusListenerKit,
77
+ ) => void;
78
+ export type BusListenersMap = Record<BusEventName, BusPayload>;
79
+
80
+ // Change instructions
81
+ export type RemoveInstructionData = {
82
+ name: string;
83
+ };
84
+
85
+ export type AttributeInstructionData = {
86
+ name: string;
87
+ value: string;
88
+ };
89
+
90
+ export type EventInstructionData = {
91
+ name: string;
92
+ value: EventListener;
93
+ };
94
+
95
+ export type UpdateEventInstructionData = {
96
+ name: string;
97
+ sourceValue: EventListener;
98
+ targetValue: EventListener;
99
+ };
100
+
101
+ export type InsertNodeData = {
102
+ parent: ExpandedElement;
103
+ index: number;
104
+ };
105
+
106
+ type NullData = Record<string, never>;
107
+
108
+ export type InstructionData =
109
+ | RemoveInstructionData
110
+ | AttributeInstructionData
111
+ | EventInstructionData
112
+ | UpdateEventInstructionData
113
+ | InsertNodeData
114
+ | NullData;
115
+
116
+ export type Instruction = {
117
+ source: Dom;
118
+ target: Dom;
119
+ type: ChangeInstructions;
120
+ data: InstructionData;
121
+ };
122
+
123
+ export type Instructions = Array<Instruction>;
124
+
125
+ export enum ChangeInstructions {
126
+ removeNode,
127
+ insertNode, // can be to move an existing element in the dom, or to add one
128
+ replaceNode,
129
+ removeAttribute,
130
+ addAttribute,
131
+ updateAttribute,
132
+ removeEvent,
133
+ addEvent,
134
+ updateEvent,
135
+ changeValue,
136
+ changeText,
137
+ }
138
+
139
+ export type Updater = (instruction: Instruction) => void;
140
+
141
+ export type App = {
142
+ publish?: BusPublish;
143
+ subscribe?: BusSubscribe;
144
+ // deno-lint-ignore no-explicit-any
145
+ bus?: any;
146
+ state?: State
147
+ renderKit: RenderKit;
148
+ render: (template: Template, selector: string) => Template;
149
+ };
@@ -0,0 +1,18 @@
1
+ import jsx from '../jsx'
2
+
3
+ export const If = ({ condition, children }) => {
4
+ if (!condition) return
5
+ return <>{children}</>
6
+ }
7
+
8
+ export const Unless = ({ condition, children }) => {
9
+ if (condition) return
10
+ return <>{children}</>
11
+ }
12
+
13
+ export const IfElse = ({ condition, children }) => {
14
+ const [first, ...rest] = children
15
+
16
+ if (condition) return <>{first}</>
17
+ return <>{rest}</>
18
+ }
@@ -0,0 +1,5 @@
1
+ import jsx from '../jsx'
2
+
3
+ export const Link = ({ children, ...props }) => {
4
+ return <a {...props} onClick='goToHref'>{children}</a>
5
+ }
package/src/views.js ADDED
@@ -0,0 +1,7 @@
1
+ import * as conditionals from './views/conditionals.jsx'
2
+ import { Link } from './views/link.jsx'
3
+
4
+ export const views = {
5
+ ...conditionals,
6
+ Link
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": ["ESNext"],
4
+ "target": "ESNext",
5
+ "module": "ESNext",
6
+ "moduleDetection": "force",
7
+ "allowJs": true,
8
+
9
+ /* Bundler mode */
10
+ "moduleResolution": "bundler",
11
+ "allowImportingTsExtensions": true,
12
+ "verbatimModuleSyntax": true,
13
+ "noEmit": true,
14
+
15
+ /* Linting */
16
+ "skipLibCheck": true,
17
+ "strict": true,
18
+ "noFallthroughCasesInSwitch": true,
19
+ "forceConsistentCasingInFileNames": true,
20
+
21
+ /* Jaxs JSX */
22
+ "jsx": "react",
23
+ "jsxFactory": "jsx",
24
+ "jsxFragmentFactory": "jsx.fragment"
25
+ }
26
+ }