-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move multiple entity editing inside action
Widen EntityStore type
- Loading branch information
1 parent
c4ab7bf
commit b06586a
Showing
6 changed files
with
55 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,55 +1,47 @@ | ||
import type { AutomergeSvelteStore } from "../automerge-svelte-store.type"; | ||
import { Extend } from "@automerge/automerge"; | ||
import { patch } from "@onsetsoftware/automerge-patcher"; | ||
import type { Path, PathValue } from "dot-path-value"; | ||
import { getByPath, setByPath } from "dot-path-value"; | ||
import { getTextPatches } from "../diff-to-patches"; | ||
import { patch } from "@onsetsoftware/automerge-patcher"; | ||
import { quickClone } from "../helpers/quick-clone"; | ||
import { Extend } from "@automerge/automerge"; | ||
import { inputAction } from "./input-action"; | ||
import { BindOptions } from "./types/bind-options.type"; | ||
|
||
export function bindTextDeferred<T extends Record<string, any>>( | ||
node: HTMLInputElement, | ||
{ | ||
store, | ||
path, | ||
title, | ||
}: { store: AutomergeSvelteStore<T>; path: Path<T>; title?: string }, | ||
{ store, path, title }: BindOptions<T>, | ||
) { | ||
const subscription = store.subscribe((doc) => { | ||
node.value = getByPath(doc, path)?.toString(); | ||
}); | ||
|
||
const inputListener = () => { | ||
store.localChange((doc) => { | ||
doc = quickClone(doc); | ||
setByPath(doc, path, node.value as PathValue<T, Path<T>>); | ||
|
||
return doc; | ||
}); | ||
}; | ||
|
||
const changeListener = () => { | ||
store.change( | ||
(doc) => { | ||
const lastValue = getByPath(doc, path as Path<Extend<T>>); | ||
const patches = getTextPatches(String(lastValue), node.value); | ||
|
||
patches.forEach((p) => { | ||
p.path.unshift(...path.split(".")); | ||
patch(doc, p); | ||
return inputAction<BindOptions<T>>( | ||
{ | ||
subscribe: (node, { store, path }) => { | ||
return store.subscribe((doc) => { | ||
node.value = getByPath(doc, path)?.toString() || ""; | ||
}); | ||
}, | ||
title ? { message: `Update ${title}` } : {}, | ||
); | ||
}; | ||
inputListener: (node, { store, path }) => { | ||
store.localChange((doc) => { | ||
doc = quickClone(doc); | ||
setByPath(doc, path, node.value as PathValue<T, Path<T>>); | ||
|
||
node.addEventListener("input", inputListener); | ||
node.addEventListener("change", changeListener); | ||
return doc; | ||
}); | ||
}, | ||
changeListener: (node, { store, path, title }) => { | ||
store.change( | ||
(doc) => { | ||
const lastValue = getByPath(doc, path as Path<Extend<T>>); | ||
const patches = getTextPatches(String(lastValue), node.value); | ||
|
||
return { | ||
destroy() { | ||
subscription(); | ||
node.removeEventListener("input", inputListener); | ||
node.removeEventListener("change", changeListener); | ||
patches.forEach((p) => { | ||
p.path.unshift(...path.split(".")); | ||
patch(doc, p); | ||
}); | ||
}, | ||
title ? { message: `Update ${title}` } : {}, | ||
); | ||
}, | ||
}, | ||
}; | ||
node, | ||
{ store, path, title }, | ||
); | ||
} |
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