Skip to content

Commit

Permalink
Move multiple entity editing inside action
Browse files Browse the repository at this point in the history
Widen EntityStore type
  • Loading branch information
acurrieclark committed May 16, 2023
1 parent c4ab7bf commit b06586a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions src/actions/bind-entity-string-deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export function bindEntityStringDeferred<
>(node: HTMLInputElement, options: BindEntityOptions<U, T>) {
return inputAction<BindEntityOptions<U, T>>(
{
subscribe: (node, { store, ids, property, hide }) => {
subscribe: (node, { store, ids, property }) => {
return store.subscribe((doc) => {
if (!hide) {
const values = new Set(ids.map((id) => doc.entities[id]?.[property]));

if (values.size === 1) {
node.value = doc.entities[ids[0]]?.[property] || "";
} else {
node.value = "";
Expand All @@ -29,19 +31,18 @@ export function bindEntityStringDeferred<
return doc;
});
},
changeListener: (node, { store, ids, property, title }) => {
changeListener: (node, options) => {
const { store, ids, property, title } = options;

store.change(
(doc) => {
const lastValue = doc.entities[ids[0]][property as keyof Extend<T>];
const patches = getStringPatches(String(lastValue), node.value);

patches.forEach((p) => {
const path = p.path.slice();
ids.forEach((id) => {
ids.forEach((id) => {
const lastValue =
doc.entities[id][property as keyof Extend<T>] || "";
const patches = getStringPatches(String(lastValue), node.value);
patches.forEach((p) => {
p.path.unshift(...[property as string]);

patch(doc.entities[id], p);
p.path = path;
});
});
},
Expand Down
6 changes: 4 additions & 2 deletions src/actions/bind-entity-value-deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export function bindEntityValueDeferred<
>(node: HTMLInputElement, options: BindEntityOptions<U, T>) {
return inputAction<BindEntityOptions<U, T>>(
{
subscribe: (node, { store, ids, property, hide }) => {
subscribe: (node, { store, ids, property }) => {
return store.subscribe((doc) => {
if (!hide) {
const values = new Set(ids.map((id) => doc.entities[id]?.[property]));

if (values.size === 1) {
node.value = doc.entities[ids[0]]?.[property] || "";
} else {
node.value = "";
Expand Down
74 changes: 33 additions & 41 deletions src/actions/bind-text-deferred.ts
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 },
);
}
1 change: 0 additions & 1 deletion src/actions/types/bind-entity-options.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ export type BindEntityOptions<
store: AutomergeEntityStore<U, T>;
ids: string[];
property: keyof T;
hide?: boolean;
title?: string;
};
2 changes: 1 addition & 1 deletion src/automerge-entity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type EntityTitles = {

export class AutomergeEntityStore<
U,
T extends { id: string },
T extends { id: string; [key: string]: any },
> extends AutomergeDerivedStore<EntityState<T>, U> {
readonly #titles: EntityTitles | undefined;
constructor(
Expand Down

0 comments on commit b06586a

Please sign in to comment.