mark-fe-fwk 1.0.0 → 1.0.2

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 (2) hide show
  1. package/dist/mark-fe-fwk.js +240 -1
  2. package/package.json +2 -1
@@ -1 +1,240 @@
1
- console.log('This will be a frontend framework!');
1
+ function addEventListener(eventName, handler, el) {
2
+ el.addEventListener(eventName, handler);
3
+ return handler
4
+ }
5
+ function addEventListeners(listeners = {}, el) {
6
+ const addedListeners = {};
7
+ Object.entries(listeners).forEach(([eventName, handler]) => {
8
+ const listener = addEventListener(eventName, handler, el);
9
+ addedListeners[eventName = listener];
10
+ });
11
+ }
12
+ function removeEventListeners(listeners = {}, el) {
13
+ Object.entries(listeners).forEach(([eventName, handler]) => {
14
+ el.removeEventListener(eventName, handler);
15
+ });
16
+ }
17
+
18
+ function withoutNulls(arr) {
19
+ return arr.filter((item) => !(item === null || item === undefined))
20
+ }
21
+
22
+ const DOM_TYPES = {
23
+ TEXT: 'text',
24
+ ELEMENT: 'element',
25
+ FRAGMENT: 'fragment',
26
+ };
27
+ function h(tag, props = {}, children = []) {
28
+ return {
29
+ tag,
30
+ props,
31
+ children: mapTextNodes(withoutNulls(children)),
32
+ type: DOM_TYPES.ELEMENT
33
+ }
34
+ }
35
+ function mapTextNodes(children) {
36
+ return children.map((child) => typeof child === 'string' ? hString(child) : child)
37
+ }
38
+ function hString(str) {
39
+ return {type: DOM_TYPES.TEXT, value: str }
40
+ }
41
+ function hFragment(vNodes) {
42
+ return {
43
+ type: DOM_TYPES.FRAGMENT,
44
+ children: mapTextNodes(withoutNulls(vNodes)),
45
+ }
46
+ }
47
+
48
+ function destroyDOM(vdom) {
49
+ const { type } = vdom;
50
+ switch (type) {
51
+ case DOM_TYPES.TEXT: {
52
+ removeTextNode(vdom);
53
+ break
54
+ }
55
+ case DOM_TYPES.ELEMENT: {
56
+ removeElementNode(vdom);
57
+ break
58
+ }
59
+ case DOM_TYPES.FRAGMENT: {
60
+ removeFragmentNodes(vdom);
61
+ break
62
+ }
63
+ default: {
64
+ throw new Error(`Can't destroy DOM of type: ${type}`)
65
+ }
66
+ }
67
+ delete vdom.el;
68
+ }
69
+ function removeTextNode(vdom) {
70
+ const {el} = vdom;
71
+ el.remove();
72
+ }
73
+ function removeElementNode(vdom) {
74
+ const { el, children, listeners } = vdom;
75
+ el.remove();
76
+ children.forEach(destroyDOM);
77
+ if (listeners) {
78
+ removeEventListeners(listeners, el);
79
+ delete vdom.listeners;
80
+ }
81
+ }
82
+ function removeFragmentNodes(vdom) {
83
+ const { children } = vdom;
84
+ children.forEach(destroyDOM);
85
+ }
86
+
87
+ function setAttributes(el, attrs) {
88
+ const { class: className, style, ...otherAttrs } = attrs;
89
+ if (className) {
90
+ setClass(el, className);
91
+ }
92
+ if (style) {
93
+ Object.entries(style).forEach(([prompt, value]) => {
94
+ setStyle(el, prop, value);
95
+ });
96
+ }
97
+ for (const [name, value] of Object.entries(otherAttrs)) {
98
+ setAttribute(el, name, value);
99
+ }
100
+ }
101
+ function setClass(el, className) {
102
+ el.className = '';
103
+ if(typeof className ==='string') {
104
+ el.className = className;
105
+ }
106
+ if(Array.isArray(className)) {
107
+ el.classList.add(...className);
108
+ }
109
+ }
110
+ function setStyle(el, name, value) {
111
+ el.style[name] = value;
112
+ }
113
+ function setAttribute(el, name, value) {
114
+ if(value == null) {
115
+ removeAttribute(el, name);
116
+ } else if (name.startsWith('data-')) {
117
+ el.setAttribute(name, value);
118
+ } else {
119
+ el[name] = value;
120
+ }
121
+ }
122
+ function removeAttribute(el, name) {
123
+ el[name] = null;
124
+ el.removeAttribute(name);
125
+ }
126
+
127
+ function mountDOM(vdom, parentEl) {
128
+ switch (vdom.type) {
129
+ case DOM_TYPES.TEXT: {
130
+ createTextNode(vdom, parentEl);
131
+ break
132
+ }
133
+ case DOM_TYPES.ELEMENT: {
134
+ createElementNode(vdom, parentEl);
135
+ break
136
+ }
137
+ case DOM_TYPES.FRAGMENT: {
138
+ createFragmentNodes(vdom, parentEl);
139
+ break
140
+ }
141
+ default: {
142
+ throw new Error(`Can't mount DOM of type: ${vdom.type}`)
143
+ }
144
+ }
145
+ }
146
+ function createTextNode(vdom, parentEl) {
147
+ const value = vdom.value;
148
+ const textNode = document.createTextNode(value);
149
+ vdom.el = textNode;
150
+ parentEl.append(textNode);
151
+ }
152
+ function createFragmentNodes(vdom, parentEl) {
153
+ const { children } = vdom;
154
+ vdom.el = parentEl;
155
+ children.forEach((child) => mountDOM(child, parentEl));
156
+ }
157
+ function createElementNode(vdom, parentEl) {
158
+ const { tag, props, children } = vdom;
159
+ const element = document.createElement(tag);
160
+ addProps(element, props, vdom);
161
+ vdom.el = element;
162
+ children.forEach((child) => mountDOM(child, element));
163
+ parentEl.append(element);
164
+ }
165
+ function addProps(el, props, vdom) {
166
+ const { on: events, ...attrs } = props;
167
+ vdom.listeners = addEventListeners(events, el);
168
+ setAttributes(el, attrs);
169
+ }
170
+
171
+ class Dispatcher {
172
+ #subs = new Map()
173
+ #afterHandlers = []
174
+ subscribe(commandName, handler) {
175
+ if(!this.#subs.has(commandName)) {
176
+ this.#subs.set(commandName, []);
177
+ }
178
+ const handlers = this.#subs.get(commandName);
179
+ if (handlers.includes(handler)) {
180
+ return () => {}
181
+ }
182
+ handlers.push(handler);
183
+ return () => {
184
+ const idx = handlers.indexOf(handler);
185
+ handlers.splice(idx,1);
186
+ }
187
+ }
188
+ afterEveryCommand(handler) {
189
+ this.#afterHandlers.push(handler);
190
+ return () => {
191
+ const idx = this.#afterHandlers.indexOf(handler);
192
+ this.#afterHandlers.splice(idx, 1);
193
+ }
194
+ }
195
+ dispatch(commandName, payload) {
196
+ if (this.#subs.has(commandName)) {
197
+ this.#subs.get(commandName).forEach((handler) => handler(payload));
198
+ } else {
199
+ console.warn(`No handlers for command: ${commandName}`);
200
+ }
201
+ this.#afterHandlers.forEach((handler) => handler());
202
+ }
203
+ }
204
+
205
+ function createApp({state, view, reducers = {} }) {
206
+ let parentEl = null;
207
+ let vdom = null;
208
+ const dispatcher = new Dispatcher();
209
+ const subscriptions = [dispatcher.afterEveryCommand(renderApp)];
210
+ function emit(eventName, payload) {
211
+ dispatcher.dispatch(eventName, payload);
212
+ }
213
+ for( const actionName in reducers) {
214
+ const reducer = reducers[actionName];
215
+ const subs = dispatcher.subscribe(actionName, (payload) => {
216
+ state = reducer(state, payload);
217
+ });
218
+ subscriptions.push(subs);
219
+ }
220
+ function renderApp() {
221
+ if(vdom) {
222
+ destroyDOM(vdom);
223
+ }
224
+ vdom = view(state, emit);
225
+ mountDOM(vdom, parentEl);
226
+ }
227
+ return {
228
+ mount(_parentEl) {
229
+ parentEl = _parentEl;
230
+ renderApp();
231
+ },
232
+ unmount() {
233
+ destroyDOM(vdom);
234
+ vdom = null;
235
+ subscriptions.forEach((unsubscribe) => unsubscribe());
236
+ }
237
+ }
238
+ }
239
+
240
+ export { createApp, h, hFragment, hString };
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "mark-fe-fwk",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
+ "author": "Mark C",
4
5
  "main": "dist/mark-fe-fwk.js",
5
6
  "files": [
6
7
  "dist/mark-fe-fwk.js"