Skip to content

Commit

Permalink
fix: more accurately recalculate focus stack
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Nov 7, 2020
1 parent 1ec33fd commit 1fdb3ca
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/breezy-lemons-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hydrofoil/shaperone-playground": patch
"@hydrofoil/shaperone-core": patch
---

Prevent breaking focus stack when resources are new pointers of same data
33 changes: 28 additions & 5 deletions packages/core/models/forms/reducers/datasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,38 @@ export const setRootResource = formStateReducer(({ state, editors, multiEditors
return
}

draft.focusStack = [rootPointer]
draft.resourceGraph = rootPointer.dataset
draft.focusNodes = {
[rootPointer.value]: initialiseFocusNode({
focusNode: rootPointer,

if (!state.focusStack.length || rootPointer.value !== draft.focusStack[0].value) {
draft.focusStack = [rootPointer]
draft.focusNodes = {
[rootPointer.value]: initialiseFocusNode({
focusNode: rootPointer,
editors,
multiEditors,
shapes: state.shapes,
shouldEnableEditorChoice: draft.shouldEnableEditorChoice,
}, state.focusNodes[rootPointer.value]),
}
return
}

const focusStack = []
const focusNodes: Record<string, FocusNodeState> = {}
for (const currentFocusNode of state.focusStack) {
const focusNode = rootPointer.node(currentFocusNode)
if (!focusNode.out().values) break

focusStack.push(focusNode)
focusNodes[focusNode.value] = initialiseFocusNode({
focusNode,
editors,
multiEditors,
shapes: state.shapes,
shouldEnableEditorChoice: draft.shouldEnableEditorChoice,
}, state.focusNodes[rootPointer.value]),
}, state.focusNodes[focusNode.value])
}

draft.focusStack = focusStack
draft.focusNodes = focusNodes
}))
19 changes: 19 additions & 0 deletions packages/core/test/models/forms/reducers/datasets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ describe('core/models/forms/reducers/datasets', () => {
expect(after).to.eq(before)
})

it('does note replace stack if all resources exist in dataset', () => {
// given
const graph = cf({ dataset: $rdf.dataset() })
const { form, state: before } = testState()
const initialFoo = graph.node(ex.Foo)
const initialBar = graph.node(ex.Bar)
before.instances.get(form)!.focusStack = [initialFoo, initialBar]

// when
const newButSame = cf({ dataset: $rdf.dataset([...graph.dataset]) }).node(initialFoo)
const after = setRootResource(before, {
form,
rootPointer: newButSame,
})

// then
expect(after).to.eq(before)
})

it('populates focus node state for root node', () => {
// given
const { form, state } = testState()
Expand Down
8 changes: 5 additions & 3 deletions packages/wc/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export type Dispatch = StoreDispatch<typeof config>
export type Store = ModelStore<Dispatch, State>

export const store = (() => {
const store = createStore(config)
let debug = false
let store = createStore(config)

return () => {
if (window.Shaperone?.DEBUG === true) {
return devtools(store)
if (window.Shaperone?.DEBUG === true && !debug) {
debug = true
store = devtools(store)
}

return store
Expand Down

0 comments on commit 1fdb3ca

Please sign in to comment.