-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nodeReducer and related functions
- Loading branch information
Showing
5 changed files
with
279 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { IndexPath } from 'tree-visit'; | ||
import { Model } from '../builders'; | ||
import { resolvedNodeReducer } from '../nodeReducer'; | ||
import { ResolvedHierarchy } from '../resolvedHierarchy'; | ||
import { createResolvedNode } from '../traversal'; | ||
import { NoyaResolvedNode } from '../types'; | ||
|
||
function createSimpleBox() { | ||
const component = Model.component({ | ||
componentID: 'custom', | ||
rootElement: Model.primitiveElement('box'), | ||
}); | ||
|
||
const components = { | ||
[component.componentID]: component, | ||
}; | ||
|
||
const element = Model.compositeElement({ | ||
componentID: component.componentID, | ||
}); | ||
|
||
return { | ||
resolvedNode: createResolvedNode( | ||
(componentID) => components[componentID], | ||
element, | ||
), | ||
}; | ||
} | ||
|
||
function classNamesAtIndexPath( | ||
resolvedNode: NoyaResolvedNode, | ||
indexPath: IndexPath, | ||
) { | ||
const node = ResolvedHierarchy.access(resolvedNode, indexPath); | ||
|
||
if (node.type !== 'noyaPrimitiveElement') { | ||
throw new Error('Expected primitive element'); | ||
} | ||
|
||
return node.classNames.map((c) => c.value); | ||
} | ||
|
||
it('adds class name', () => { | ||
let { resolvedNode } = createSimpleBox(); | ||
|
||
expect(classNamesAtIndexPath(resolvedNode, [0])).toEqual([]); | ||
|
||
resolvedNode = resolvedNodeReducer(resolvedNode, { | ||
type: 'addClassNames', | ||
indexPath: [0], | ||
classNames: ['foo'], | ||
}); | ||
|
||
expect(classNamesAtIndexPath(resolvedNode, [0])).toEqual(['foo']); | ||
}); | ||
|
||
it('removes class name', () => { | ||
let { resolvedNode } = createSimpleBox(); | ||
|
||
expect(classNamesAtIndexPath(resolvedNode, [0])).toEqual([]); | ||
|
||
resolvedNode = resolvedNodeReducer(resolvedNode, { | ||
type: 'addClassNames', | ||
indexPath: [0], | ||
classNames: ['foo'], | ||
}); | ||
|
||
expect(classNamesAtIndexPath(resolvedNode, [0])).toEqual(['foo']); | ||
|
||
resolvedNode = resolvedNodeReducer(resolvedNode, { | ||
type: 'removeClassNames', | ||
indexPath: [0], | ||
classNames: ['foo'], | ||
}); | ||
|
||
expect(classNamesAtIndexPath(resolvedNode, [0])).toEqual([]); | ||
}); | ||
|
||
it('sets name', () => { | ||
let { resolvedNode } = createSimpleBox(); | ||
|
||
expect(ResolvedHierarchy.access(resolvedNode, [0]).name).toEqual(undefined); | ||
|
||
resolvedNode = resolvedNodeReducer(resolvedNode, { | ||
type: 'setName', | ||
indexPath: [0], | ||
name: 'foo', | ||
}); | ||
|
||
expect(ResolvedHierarchy.access(resolvedNode, [0]).name).toEqual('foo'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import cloneDeep from 'lodash/cloneDeep'; | ||
import { IndexPath } from 'tree-visit'; | ||
import { Model } from './builders'; | ||
import { ResolvedHierarchy } from './resolvedHierarchy'; | ||
import { NoyaResolvedNode } from './types'; | ||
|
||
type Action = | ||
| { | ||
type: 'setName'; | ||
indexPath: IndexPath; | ||
name: string; | ||
} | ||
| { | ||
type: 'addClassNames'; | ||
indexPath: IndexPath; | ||
classNames: string[]; | ||
} | ||
| { | ||
type: 'removeClassNames'; | ||
indexPath: IndexPath; | ||
classNames: string[]; | ||
} | ||
| { | ||
type: 'insertNode'; | ||
indexPath: IndexPath; | ||
node: NoyaResolvedNode; | ||
} | ||
| { | ||
type: 'removeNode'; | ||
indexPath: IndexPath; | ||
} | ||
| { | ||
type: 'duplicateNode'; | ||
indexPath: IndexPath; | ||
}; | ||
|
||
export function resolvedNodeReducer( | ||
resolvedNode: NoyaResolvedNode, | ||
action: Action, | ||
): NoyaResolvedNode { | ||
switch (action.type) { | ||
case 'setName': { | ||
const { indexPath, name } = action; | ||
|
||
const node = ResolvedHierarchy.access(resolvedNode, indexPath); | ||
|
||
if (node?.type !== 'noyaPrimitiveElement') return resolvedNode; | ||
|
||
return ResolvedHierarchy.replace(resolvedNode, { | ||
at: indexPath, | ||
node: { | ||
...cloneDeep(node), | ||
name, | ||
}, | ||
}); | ||
} | ||
case 'addClassNames': { | ||
const { indexPath, classNames: className } = action; | ||
|
||
const node = ResolvedHierarchy.access(resolvedNode, indexPath); | ||
|
||
if (node?.type !== 'noyaPrimitiveElement') return resolvedNode; | ||
|
||
return ResolvedHierarchy.replace(resolvedNode, { | ||
at: indexPath, | ||
node: { | ||
...cloneDeep(node), | ||
classNames: [ | ||
...node.classNames, | ||
...className.map((c) => Model.className(c)), | ||
], | ||
}, | ||
}); | ||
} | ||
case 'removeClassNames': { | ||
const { indexPath, classNames: className } = action; | ||
|
||
const node = ResolvedHierarchy.access(resolvedNode, indexPath); | ||
|
||
if (node?.type !== 'noyaPrimitiveElement') return resolvedNode; | ||
|
||
return ResolvedHierarchy.replace(resolvedNode, { | ||
at: indexPath, | ||
node: { | ||
...cloneDeep(node), | ||
classNames: node.classNames.filter( | ||
(c) => !className.includes(c.value), | ||
), | ||
}, | ||
}); | ||
} | ||
case 'insertNode': { | ||
const { indexPath, node: child } = action; | ||
|
||
const node = ResolvedHierarchy.access(resolvedNode, indexPath); | ||
|
||
if (node?.type !== 'noyaPrimitiveElement') return resolvedNode; | ||
|
||
return ResolvedHierarchy.insert(resolvedNode, { | ||
at: [...indexPath, node.children.length], | ||
nodes: [child], | ||
}); | ||
} | ||
case 'removeNode': { | ||
const { indexPath } = action; | ||
|
||
const node = ResolvedHierarchy.access(resolvedNode, indexPath); | ||
|
||
if (node?.type !== 'noyaPrimitiveElement') return resolvedNode; | ||
|
||
return ResolvedHierarchy.remove(resolvedNode, { | ||
indexPaths: [indexPath], | ||
}); | ||
} | ||
case 'duplicateNode': { | ||
const { indexPath } = action; | ||
|
||
const node = ResolvedHierarchy.access(resolvedNode, indexPath); | ||
|
||
return ResolvedHierarchy.insert(resolvedNode, { | ||
at: [...indexPath.slice(0, -1), indexPath.at(-1)! + 1], | ||
nodes: [ResolvedHierarchy.clone(node)], | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.