jaxs 0.2.1 → 0.3.1

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.
@@ -1,6 +1,7 @@
1
1
  import type { Dom, ExpandedElement, Instructions } from '../../../types';
2
- import { compileForElement } from './element';
2
+ import { compileForElement, compileForSvg } from './element';
3
3
  import { compileForText } from './text';
4
+ import { isSvg } from '../../dom/svg'
4
5
 
5
6
  enum NodeTypes {
6
7
  ElementNode = 1,
@@ -11,7 +12,23 @@ export const compileForNodeGenerator =
11
12
  (compileForCollection: any) => (source: Dom, target: Dom) => {
12
13
  let instructions = [] as Instructions;
13
14
 
14
- if (source.nodeType === NodeTypes.ElementNode) {
15
+ if (source.nodeType === NodeTypes.ElementNode &&
16
+ isSvg(source as SVGElement)) {
17
+ const sourceElement = source as ExpandedElement;
18
+ const targetElement = target as ExpandedElement;
19
+ const baseInstructions = compileForSvg(
20
+ sourceElement,
21
+ targetElement
22
+ )
23
+
24
+ const childrenInstructions = compileForCollection(
25
+ sourceElement.childNodes,
26
+ targetElement.childNodes,
27
+ sourceElement,
28
+ );
29
+
30
+ instructions = baseInstructions.concat(childrenInstructions);
31
+ } else if (source.nodeType === NodeTypes.ElementNode) {
15
32
  const sourceElement = source as ExpandedElement;
16
33
  const targetElement = target as ExpandedElement;
17
34
 
@@ -12,7 +12,7 @@ import type {
12
12
  } from '../types';
13
13
  import { ChangeInstructions } from '../types'
14
14
  import { compileChange } from './change/compile';
15
- import { debug } from '../debugging';
15
+ // import { debug } from '../debugging';
16
16
 
17
17
  export const change = (
18
18
  source: HtmlChildren,
@@ -21,7 +21,7 @@ export const change = (
21
21
  ) => {
22
22
  const instructions = compileChange(source, target, parent);
23
23
 
24
- debug('instructions', instructions.map((instruction) => instruction.type))
24
+ // debug('instructions', instructions.map((instruction) => instruction.type))
25
25
 
26
26
  instructions.forEach((instruction) => {
27
27
  performInstruction(instruction);
@@ -43,7 +43,7 @@ const changeText: Updater = (instruction: Instruction) => {
43
43
  const removeNode: Updater = (instruction: Instruction) => {
44
44
  const { source } = instruction;
45
45
  source.remove();
46
- debug('removeNode called on', source.nodeName)
46
+ // debug('removeNode called on', source.nodeName)
47
47
  };
48
48
 
49
49
  const insertNode: Updater = (instruction: Instruction) => {
@@ -61,22 +61,30 @@ const insertNode: Updater = (instruction: Instruction) => {
61
61
  const replaceNode: Updater = (instruction: Instruction) => {
62
62
  const { source, target } = instruction;
63
63
  source.replaceWith(target);
64
- debug('replaceNode called on', source.nodeName, 'with', target.nodeName)
65
- debug('parent', source.parentElement)
64
+ // debug('replaceNode called on', source.nodeName, 'with', target.nodeName)
65
+ // debug('parent', source.parentElement)
66
66
  };
67
67
 
68
68
  const removeAttribute: Updater = (instruction: Instruction) => {
69
69
  const { source, data } = instruction;
70
- const { name } = data as RemoveInstructionData;
70
+ const { name, isSvg } = data as RemoveInstructionData;
71
71
 
72
- (source as ExpandedElement).removeAttribute(name);
72
+ if (isSvg) {
73
+ (source as SVGElement).removeAttributeNS(null, name)
74
+ } else {
75
+ (source as ExpandedElement).removeAttribute(name);
76
+ }
73
77
  };
74
78
 
75
79
  const addAttribute: Updater = (instruction: Instruction) => {
76
80
  const { source, data } = instruction;
77
- const { name, value } = data as AttributeInstructionData;
81
+ const { name, value, isSvg } = data as AttributeInstructionData;
78
82
 
79
- (source as ExpandedElement).setAttribute(name, value);
83
+ if (isSvg) {
84
+ (source as SVGElement).setAttributeNS(null, name, value)
85
+ } else {
86
+ (source as ExpandedElement).setAttribute(name, value);
87
+ }
80
88
  };
81
89
 
82
90
  const updateAttribute: Updater = (instruction: Instruction) => {
@@ -1,16 +1,18 @@
1
1
  import type { RenderKit, Attributes, ExpandedElement } from "../../types"
2
2
 
3
+ export const namespace = 'http://www.w3.org/2000/svg'
3
4
  export const isSvgTag = (tagType: string) => tagType === 'svg'
5
+ export const isSvg = (element: SVGElement) => element.namespaceURI === namespace
4
6
 
5
7
  export const createSvgNode = (type: string, attributes: Attributes, renderKit: RenderKit) => {
6
8
  const document = renderKit && renderKit.document || window.document;
7
- const xmlns = 'http://www.w3.org/2000/svg'
8
- const node = document.createElementNS(xmlns, type, xmlns)
9
+ const node = document.createElementNS(namespace, type)
9
10
 
10
11
  for (const key in attributes) {
11
12
  if (key === '__self' || key === 'xmlns') continue;
13
+ // adding namespace in as first argument makes it not really render!
12
14
  node.setAttributeNS(null, key, attributes[key]);
13
15
  }
14
16
 
15
- return node as ExpandedElement
17
+ return node as unknown as ExpandedElement
16
18
  }
@@ -1,10 +1,11 @@
1
1
  import { eventName } from '../../state'
2
2
  import { change } from '../change'
3
3
 
4
+ const passThroughViewModel = (state) => state
4
5
  export class Bound {
5
6
  constructor (TemplateClass, viewModel, subscriptions, attributes) {
6
7
  this.TemplateClass = TemplateClass
7
- this.viewModel = viewModel
8
+ this.viewModel = viewModel || passThroughViewModel
8
9
  this.attributes = attributes || {}
9
10
  this.subscriptions = subscriptions
10
11
  this.dom = []
package/src/types.ts CHANGED
@@ -20,7 +20,7 @@ export interface InputElement extends ExpandedElement {
20
20
  // deno-lint-ignore no-explicit-any
21
21
  value: any;
22
22
  }
23
- export type Dom = Text | ExpandedElement;
23
+ export type Dom = Text | ExpandedElement | SVGElement;
24
24
  export type DomCollection = Dom[];
25
25
  export type HtmlChildren =
26
26
  | HTMLCollection
@@ -81,11 +81,13 @@ export type BusListenersMap = Record<BusEventName, BusPayload>;
81
81
  // Change instructions
82
82
  export type RemoveInstructionData = {
83
83
  name: string;
84
+ isSvg?: boolean;
84
85
  };
85
86
 
86
87
  export type AttributeInstructionData = {
87
88
  name: string;
88
89
  value: string;
90
+ isSvg?: boolean;
89
91
  };
90
92
 
91
93
  export type EventInstructionData = {