@derweili/payload-visual-editing 0.0.0-stage → 0.1.0
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.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +142 -2
- package/dist/client/VisualEditingAdmin.d.ts +9 -0
- package/dist/client/VisualEditingAdmin.d.ts.map +1 -0
- package/dist/client/VisualEditingAdmin.js +109 -0
- package/dist/client/VisualEditingAdmin.js.map +1 -0
- package/dist/client/dom-adapter.d.ts +61 -0
- package/dist/client/dom-adapter.d.ts.map +1 -0
- package/dist/client/dom-adapter.js +177 -0
- package/dist/client/dom-adapter.js.map +1 -0
- package/dist/client/focus.d.ts +36 -0
- package/dist/client/focus.d.ts.map +1 -0
- package/dist/client/focus.js +61 -0
- package/dist/client/focus.js.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +7 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/keep-in-view.d.ts +6 -0
- package/dist/client/keep-in-view.d.ts.map +1 -0
- package/dist/client/keep-in-view.js +78 -0
- package/dist/client/keep-in-view.js.map +1 -0
- package/dist/client/resolve-focus.d.ts +56 -0
- package/dist/client/resolve-focus.d.ts.map +1 -0
- package/dist/client/resolve-focus.js +138 -0
- package/dist/client/resolve-focus.js.map +1 -0
- package/dist/server/encode-document.d.ts +23 -0
- package/dist/server/encode-document.d.ts.map +1 -0
- package/dist/server/encode-document.js +147 -0
- package/dist/server/encode-document.js.map +1 -0
- package/dist/server/hooks.d.ts +17 -0
- package/dist/server/hooks.d.ts.map +1 -0
- package/dist/server/hooks.js +79 -0
- package/dist/server/hooks.js.map +1 -0
- package/dist/server/index.d.ts +10 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +10 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/options.d.ts +58 -0
- package/dist/server/options.d.ts.map +1 -0
- package/dist/server/options.js +18 -0
- package/dist/server/options.js.map +1 -0
- package/dist/server/path-matcher.d.ts +8 -0
- package/dist/server/path-matcher.d.ts.map +1 -0
- package/dist/server/path-matcher.js +32 -0
- package/dist/server/path-matcher.js.map +1 -0
- package/dist/server/plugin.d.ts +13 -0
- package/dist/server/plugin.d.ts.map +1 -0
- package/dist/server/plugin.js +102 -0
- package/dist/server/plugin.js.map +1 -0
- package/package.json +74 -3
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ancestorPaths, findFieldElement, focusFieldElement, revealElement, selectTab, tabsInScope } from "./dom-adapter.js";
|
|
2
|
+
/**
|
|
3
|
+
* Expands a collapsed array/blocks row the way Payload's array field does: form state through
|
|
4
|
+
* `SET_ROW_COLLAPSED`, and the user's collapsed rows saved in the document preferences.
|
|
5
|
+
*/ export function expandRow(api, path, index) {
|
|
6
|
+
const rows = api.getField(path)?.rows;
|
|
7
|
+
if (!rows?.[index]?.collapsed) return;
|
|
8
|
+
const updatedRows = rows.map((row, i)=>i === index ? {
|
|
9
|
+
...row,
|
|
10
|
+
collapsed: false
|
|
11
|
+
} : row);
|
|
12
|
+
api.dispatchFields({
|
|
13
|
+
type: 'SET_ROW_COLLAPSED',
|
|
14
|
+
path,
|
|
15
|
+
updatedRows: updatedRows
|
|
16
|
+
});
|
|
17
|
+
api.setDocFieldPreferences(path, {
|
|
18
|
+
collapsed: updatedRows.filter((row)=>row.collapsed).map((row)=>row.id)
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/** blockType of the block row at `rowPath` (e.g. `layout.2`), from form state. */ export function blockTypeOf(api, rowPath) {
|
|
22
|
+
const dot = rowPath.lastIndexOf('.');
|
|
23
|
+
const row = api.getField(rowPath.slice(0, dot))?.rows?.[Number(rowPath.slice(dot + 1))];
|
|
24
|
+
if (row?.blockType) return row.blockType;
|
|
25
|
+
const value = api.getField(`${rowPath}.blockType`)?.value;
|
|
26
|
+
return typeof value === 'string' ? value : undefined;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Runs a focus plan: opens tabs, expands rows, then focuses the field once Payload rendered it.
|
|
30
|
+
* True when the field was focused. Never throws.
|
|
31
|
+
*/ export async function runFocusPlan(plan, api, root = document.body) {
|
|
32
|
+
try {
|
|
33
|
+
let scope = root;
|
|
34
|
+
for (const step of plan.steps){
|
|
35
|
+
if (step.type === 'row') {
|
|
36
|
+
expandRow(api, step.path, step.index);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
// Tabs inside array rows can't be located: focus works only if that tab is already open
|
|
40
|
+
if (step.ordinal === null) continue;
|
|
41
|
+
const ordinal = step.ordinal;
|
|
42
|
+
const currentScope = scope;
|
|
43
|
+
const tabs = await revealElement(()=>tabsInScope(currentScope)[ordinal] ?? null, {
|
|
44
|
+
root
|
|
45
|
+
});
|
|
46
|
+
if (!tabs) return false;
|
|
47
|
+
scope = selectTab(currentScope, ordinal, step.index) ?? tabs;
|
|
48
|
+
}
|
|
49
|
+
const element = await revealElement(()=>findFieldElement(plan.path, root), {
|
|
50
|
+
root,
|
|
51
|
+
ancestors: ancestorPaths(plan.path)
|
|
52
|
+
});
|
|
53
|
+
if (!element) return false;
|
|
54
|
+
focusFieldElement(element);
|
|
55
|
+
return true;
|
|
56
|
+
} catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//# sourceMappingURL=focus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/focus.ts"],"sourcesContent":["import { ancestorPaths, findFieldElement, focusFieldElement, revealElement, selectTab, tabsInScope } from './dom-adapter.ts'\nimport type { FocusPlan } from './resolve-focus.ts'\n\n/** The parts of Payload's form and document APIs that focusing needs (all public). */\nexport interface FocusApi {\n /** `useForm().getField` */\n getField: (path: string) => { rows?: { id: string; collapsed?: boolean; blockType?: string }[]; value?: unknown } | undefined\n /** `useForm().dispatchFields` */\n dispatchFields: (action: { type: 'SET_ROW_COLLAPSED'; path: string; updatedRows: never[] }) => void\n /** `useDocumentInfo().setDocFieldPreferences` */\n setDocFieldPreferences: (path: string, preferences: { collapsed: string[] }) => void\n}\n\n/**\n * Expands a collapsed array/blocks row the way Payload's array field does: form state through\n * `SET_ROW_COLLAPSED`, and the user's collapsed rows saved in the document preferences.\n */\nexport function expandRow(api: FocusApi, path: string, index: number): void {\n const rows = api.getField(path)?.rows\n if (!rows?.[index]?.collapsed) return\n const updatedRows = rows.map((row, i) => (i === index ? { ...row, collapsed: false } : row))\n api.dispatchFields({ type: 'SET_ROW_COLLAPSED', path, updatedRows: updatedRows as never[] })\n api.setDocFieldPreferences(path, { collapsed: updatedRows.filter((row) => row.collapsed).map((row) => row.id) })\n}\n\n/** blockType of the block row at `rowPath` (e.g. `layout.2`), from form state. */\nexport function blockTypeOf(api: FocusApi, rowPath: string): string | undefined {\n const dot = rowPath.lastIndexOf('.')\n const row = api.getField(rowPath.slice(0, dot))?.rows?.[Number(rowPath.slice(dot + 1))]\n if (row?.blockType) return row.blockType\n const value = api.getField(`${rowPath}.blockType`)?.value\n return typeof value === 'string' ? value : undefined\n}\n\n/**\n * Runs a focus plan: opens tabs, expands rows, then focuses the field once Payload rendered it.\n * True when the field was focused. Never throws.\n */\nexport async function runFocusPlan(plan: FocusPlan, api: FocusApi, root: Element = document.body): Promise<boolean> {\n try {\n let scope: Element = root\n for (const step of plan.steps) {\n if (step.type === 'row') {\n expandRow(api, step.path, step.index)\n continue\n }\n // Tabs inside array rows can't be located: focus works only if that tab is already open\n if (step.ordinal === null) continue\n const ordinal = step.ordinal\n const currentScope = scope\n const tabs = await revealElement(() => tabsInScope(currentScope)[ordinal] ?? null, { root })\n if (!tabs) return false\n scope = selectTab(currentScope, ordinal, step.index) ?? tabs\n }\n const element = await revealElement(() => findFieldElement(plan.path, root), {\n root,\n ancestors: ancestorPaths(plan.path),\n })\n if (!element) return false\n focusFieldElement(element)\n return true\n } catch {\n return false\n }\n}\n"],"names":["ancestorPaths","findFieldElement","focusFieldElement","revealElement","selectTab","tabsInScope","expandRow","api","path","index","rows","getField","collapsed","updatedRows","map","row","i","dispatchFields","type","setDocFieldPreferences","filter","id","blockTypeOf","rowPath","dot","lastIndexOf","slice","Number","blockType","value","undefined","runFocusPlan","plan","root","document","body","scope","step","steps","ordinal","currentScope","tabs","element","ancestors"],"mappings":"AAAA,SAASA,aAAa,EAAEC,gBAAgB,EAAEC,iBAAiB,EAAEC,aAAa,EAAEC,SAAS,EAAEC,WAAW,QAAQ,mBAAkB;AAa5H;;;CAGC,GACD,OAAO,SAASC,UAAUC,GAAa,EAAEC,IAAY,EAAEC,KAAa;IAClE,MAAMC,OAAOH,IAAII,QAAQ,CAACH,OAAOE;IACjC,IAAI,CAACA,MAAM,CAACD,MAAM,EAAEG,WAAW;IAC/B,MAAMC,cAAcH,KAAKI,GAAG,CAAC,CAACC,KAAKC,IAAOA,MAAMP,QAAQ;YAAE,GAAGM,GAAG;YAAEH,WAAW;QAAM,IAAIG;IACvFR,IAAIU,cAAc,CAAC;QAAEC,MAAM;QAAqBV;QAAMK,aAAaA;IAAuB;IAC1FN,IAAIY,sBAAsB,CAACX,MAAM;QAAEI,WAAWC,YAAYO,MAAM,CAAC,CAACL,MAAQA,IAAIH,SAAS,EAAEE,GAAG,CAAC,CAACC,MAAQA,IAAIM,EAAE;IAAE;AAChH;AAEA,gFAAgF,GAChF,OAAO,SAASC,YAAYf,GAAa,EAAEgB,OAAe;IACxD,MAAMC,MAAMD,QAAQE,WAAW,CAAC;IAChC,MAAMV,MAAMR,IAAII,QAAQ,CAACY,QAAQG,KAAK,CAAC,GAAGF,OAAOd,MAAM,CAACiB,OAAOJ,QAAQG,KAAK,CAACF,MAAM,IAAI;IACvF,IAAIT,KAAKa,WAAW,OAAOb,IAAIa,SAAS;IACxC,MAAMC,QAAQtB,IAAII,QAAQ,CAAC,GAAGY,QAAQ,UAAU,CAAC,GAAGM;IACpD,OAAO,OAAOA,UAAU,WAAWA,QAAQC;AAC7C;AAEA;;;CAGC,GACD,OAAO,eAAeC,aAAaC,IAAe,EAAEzB,GAAa,EAAE0B,OAAgBC,SAASC,IAAI;IAC9F,IAAI;QACF,IAAIC,QAAiBH;QACrB,KAAK,MAAMI,QAAQL,KAAKM,KAAK,CAAE;YAC7B,IAAID,KAAKnB,IAAI,KAAK,OAAO;gBACvBZ,UAAUC,KAAK8B,KAAK7B,IAAI,EAAE6B,KAAK5B,KAAK;gBACpC;YACF;YACA,wFAAwF;YACxF,IAAI4B,KAAKE,OAAO,KAAK,MAAM;YAC3B,MAAMA,UAAUF,KAAKE,OAAO;YAC5B,MAAMC,eAAeJ;YACrB,MAAMK,OAAO,MAAMtC,cAAc,IAAME,YAAYmC,aAAa,CAACD,QAAQ,IAAI,MAAM;gBAAEN;YAAK;YAC1F,IAAI,CAACQ,MAAM,OAAO;YAClBL,QAAQhC,UAAUoC,cAAcD,SAASF,KAAK5B,KAAK,KAAKgC;QAC1D;QACA,MAAMC,UAAU,MAAMvC,cAAc,IAAMF,iBAAiB+B,KAAKxB,IAAI,EAAEyB,OAAO;YAC3EA;YACAU,WAAW3C,cAAcgC,KAAKxB,IAAI;QACpC;QACA,IAAI,CAACkC,SAAS,OAAO;QACrBxC,kBAAkBwC;QAClB,OAAO;IACT,EAAE,OAAM;QACN,OAAO;IACT;AACF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client entry: the admin component that focuses fields (deep link and Live Preview).
|
|
3
|
+
* Referenced from Payload's import map as "@derweili/payload-visual-editing/client#VisualEditingAdmin".
|
|
4
|
+
*/
|
|
5
|
+
export { VisualEditingAdmin } from './VisualEditingAdmin.tsx';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AACA;;;GAGG;AACH,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* Client entry: the admin component that focuses fields (deep link and Live Preview).
|
|
4
|
+
* Referenced from Payload's import map as "@derweili/payload-visual-editing/client#VisualEditingAdmin".
|
|
5
|
+
*/ export { VisualEditingAdmin } from "./VisualEditingAdmin.jsx";
|
|
6
|
+
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/index.ts"],"sourcesContent":["'use client'\n/**\n * Client entry: the admin component that focuses fields (deep link and Live Preview).\n * Referenced from Payload's import map as \"@derweili/payload-visual-editing/client#VisualEditingAdmin\".\n */\nexport { VisualEditingAdmin } from './VisualEditingAdmin.tsx'\n"],"names":["VisualEditingAdmin"],"mappings":"AAAA;AACA;;;CAGC,GACD,SAASA,kBAAkB,QAAQ,2BAA0B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keep-in-view.d.ts","sourceRoot":"","sources":["../../src/client/keep-in-view.ts"],"names":[],"mappings":"AAYA,wBAAgB,UAAU,CACxB,OAAO,EAAE,OAAO,EAChB,EAAE,MAAa,EAAE,GAAU,EAAE,MAAM,EAAE,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,IAAI,CAAA;CAAO,GACjG,MAAM,IAAI,CA6DZ"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps a just-focused field in the vertical centre of the window while the page is still
|
|
3
|
+
* settling. Lazy-loaded images in the admin change the layout after the first scroll and push the
|
|
4
|
+
* field away (out of view, or behind the admin's fixed top bar).
|
|
5
|
+
*
|
|
6
|
+
* Standard browser APIs only (ResizeObserver). Stops at the first user input, so it never fights
|
|
7
|
+
* a human: wheel, touch, pointer or key. Otherwise it stops once the layout is quiet for
|
|
8
|
+
* `settle` ms, and after `max` ms at the latest. `onDone` runs once when it stops, for whatever
|
|
9
|
+
* reason. Returns a function that stops it early.
|
|
10
|
+
*/ const CENTER_TOLERANCE = 4;
|
|
11
|
+
export function keepInView(element, { settle = 1000, max = 5000, onDone } = {}) {
|
|
12
|
+
const window = element.ownerDocument.defaultView;
|
|
13
|
+
if (!window || typeof window.ResizeObserver !== 'function') {
|
|
14
|
+
onDone?.();
|
|
15
|
+
return ()=>{};
|
|
16
|
+
}
|
|
17
|
+
const document = element.ownerDocument;
|
|
18
|
+
const USER_INPUT = [
|
|
19
|
+
'wheel',
|
|
20
|
+
'touchstart',
|
|
21
|
+
'pointerdown',
|
|
22
|
+
'keydown'
|
|
23
|
+
];
|
|
24
|
+
let settleTimer;
|
|
25
|
+
let stopped = false;
|
|
26
|
+
const stop = ()=>{
|
|
27
|
+
if (stopped) return;
|
|
28
|
+
stopped = true;
|
|
29
|
+
observer.disconnect();
|
|
30
|
+
clearTimeout(settleTimer);
|
|
31
|
+
clearTimeout(maxTimer);
|
|
32
|
+
for (const type of USER_INPUT)window.removeEventListener(type, stop, true);
|
|
33
|
+
onDone?.();
|
|
34
|
+
};
|
|
35
|
+
/** Off the window's vertical centre by more than a couple of pixels. */ const isOffCenter = ()=>{
|
|
36
|
+
const rect = element.getBoundingClientRect();
|
|
37
|
+
return Math.abs(rect.top + rect.height / 2 - window.innerHeight / 2) > CENTER_TOLERANCE;
|
|
38
|
+
};
|
|
39
|
+
// ResizeObserver reports every target once when observing starts. Only real size changes count:
|
|
40
|
+
// reacting to that first report would cut the initial smooth scroll short with an instant jump
|
|
41
|
+
const sizes = new WeakMap();
|
|
42
|
+
const onResize = (entries)=>{
|
|
43
|
+
let changed = false;
|
|
44
|
+
for (const { target, contentRect } of entries){
|
|
45
|
+
const size = `${contentRect.width}x${contentRect.height}`;
|
|
46
|
+
if (sizes.has(target) && sizes.get(target) !== size) changed = true;
|
|
47
|
+
sizes.set(target, size);
|
|
48
|
+
}
|
|
49
|
+
if (changed) onLayoutChange();
|
|
50
|
+
};
|
|
51
|
+
const onLayoutChange = ()=>{
|
|
52
|
+
if (stopped) return;
|
|
53
|
+
if (!element.isConnected) return stop();
|
|
54
|
+
// Instant, not smooth: a smooth scroll would race the next layout change
|
|
55
|
+
if (isOffCenter()) element.scrollIntoView({
|
|
56
|
+
block: 'center',
|
|
57
|
+
behavior: 'instant'
|
|
58
|
+
});
|
|
59
|
+
clearTimeout(settleTimer);
|
|
60
|
+
settleTimer = setTimeout(stop, settle);
|
|
61
|
+
};
|
|
62
|
+
// The field's ancestors, not just <body>: the Payload admin fixes <body> to the viewport height
|
|
63
|
+
// and scrolls the document, so only the containers around the field grow when content loads
|
|
64
|
+
const observer = new window.ResizeObserver(onResize);
|
|
65
|
+
for(let node = element; node && node !== document.documentElement; node = node.parentElement){
|
|
66
|
+
observer.observe(node);
|
|
67
|
+
}
|
|
68
|
+
const maxTimer = setTimeout(stop, max);
|
|
69
|
+
settleTimer = setTimeout(stop, settle);
|
|
70
|
+
// Capture phase: also when a field or Payload's UI stops the event
|
|
71
|
+
for (const type of USER_INPUT)window.addEventListener(type, stop, {
|
|
72
|
+
capture: true,
|
|
73
|
+
passive: true
|
|
74
|
+
});
|
|
75
|
+
return stop;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//# sourceMappingURL=keep-in-view.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/keep-in-view.ts"],"sourcesContent":["/**\n * Keeps a just-focused field in the vertical centre of the window while the page is still\n * settling. Lazy-loaded images in the admin change the layout after the first scroll and push the\n * field away (out of view, or behind the admin's fixed top bar).\n *\n * Standard browser APIs only (ResizeObserver). Stops at the first user input, so it never fights\n * a human: wheel, touch, pointer or key. Otherwise it stops once the layout is quiet for\n * `settle` ms, and after `max` ms at the latest. `onDone` runs once when it stops, for whatever\n * reason. Returns a function that stops it early.\n */\nconst CENTER_TOLERANCE = 4\n\nexport function keepInView(\n element: Element,\n { settle = 1000, max = 5000, onDone }: { settle?: number; max?: number; onDone?: () => void } = {},\n): () => void {\n const window = element.ownerDocument.defaultView\n if (!window || typeof window.ResizeObserver !== 'function') {\n onDone?.()\n return () => {}\n }\n const document = element.ownerDocument\n\n const USER_INPUT = ['wheel', 'touchstart', 'pointerdown', 'keydown'] as const\n let settleTimer: ReturnType<typeof setTimeout> | undefined\n let stopped = false\n\n const stop = () => {\n if (stopped) return\n stopped = true\n observer.disconnect()\n clearTimeout(settleTimer)\n clearTimeout(maxTimer)\n for (const type of USER_INPUT) window.removeEventListener(type, stop, true)\n onDone?.()\n }\n\n /** Off the window's vertical centre by more than a couple of pixels. */\n const isOffCenter = () => {\n const rect = element.getBoundingClientRect()\n return Math.abs(rect.top + rect.height / 2 - window.innerHeight / 2) > CENTER_TOLERANCE\n }\n\n // ResizeObserver reports every target once when observing starts. Only real size changes count:\n // reacting to that first report would cut the initial smooth scroll short with an instant jump\n const sizes = new WeakMap<Element, string>()\n const onResize = (entries: ResizeObserverEntry[]) => {\n let changed = false\n for (const { target, contentRect } of entries) {\n const size = `${contentRect.width}x${contentRect.height}`\n if (sizes.has(target) && sizes.get(target) !== size) changed = true\n sizes.set(target, size)\n }\n if (changed) onLayoutChange()\n }\n\n const onLayoutChange = () => {\n if (stopped) return\n if (!element.isConnected) return stop()\n // Instant, not smooth: a smooth scroll would race the next layout change\n if (isOffCenter()) element.scrollIntoView({ block: 'center', behavior: 'instant' as ScrollBehavior })\n clearTimeout(settleTimer)\n settleTimer = setTimeout(stop, settle)\n }\n\n // The field's ancestors, not just <body>: the Payload admin fixes <body> to the viewport height\n // and scrolls the document, so only the containers around the field grow when content loads\n const observer = new window.ResizeObserver(onResize)\n for (let node: Element | null = element; node && node !== document.documentElement; node = node.parentElement) {\n observer.observe(node)\n }\n const maxTimer = setTimeout(stop, max)\n settleTimer = setTimeout(stop, settle)\n // Capture phase: also when a field or Payload's UI stops the event\n for (const type of USER_INPUT) window.addEventListener(type, stop, { capture: true, passive: true })\n return stop\n}\n"],"names":["CENTER_TOLERANCE","keepInView","element","settle","max","onDone","window","ownerDocument","defaultView","ResizeObserver","document","USER_INPUT","settleTimer","stopped","stop","observer","disconnect","clearTimeout","maxTimer","type","removeEventListener","isOffCenter","rect","getBoundingClientRect","Math","abs","top","height","innerHeight","sizes","WeakMap","onResize","entries","changed","target","contentRect","size","width","has","get","set","onLayoutChange","isConnected","scrollIntoView","block","behavior","setTimeout","node","documentElement","parentElement","observe","addEventListener","capture","passive"],"mappings":"AAAA;;;;;;;;;CASC,GACD,MAAMA,mBAAmB;AAEzB,OAAO,SAASC,WACdC,OAAgB,EAChB,EAAEC,SAAS,IAAI,EAAEC,MAAM,IAAI,EAAEC,MAAM,EAA0D,GAAG,CAAC,CAAC;IAElG,MAAMC,SAASJ,QAAQK,aAAa,CAACC,WAAW;IAChD,IAAI,CAACF,UAAU,OAAOA,OAAOG,cAAc,KAAK,YAAY;QAC1DJ;QACA,OAAO,KAAO;IAChB;IACA,MAAMK,WAAWR,QAAQK,aAAa;IAEtC,MAAMI,aAAa;QAAC;QAAS;QAAc;QAAe;KAAU;IACpE,IAAIC;IACJ,IAAIC,UAAU;IAEd,MAAMC,OAAO;QACX,IAAID,SAAS;QACbA,UAAU;QACVE,SAASC,UAAU;QACnBC,aAAaL;QACbK,aAAaC;QACb,KAAK,MAAMC,QAAQR,WAAYL,OAAOc,mBAAmB,CAACD,MAAML,MAAM;QACtET;IACF;IAEA,sEAAsE,GACtE,MAAMgB,cAAc;QAClB,MAAMC,OAAOpB,QAAQqB,qBAAqB;QAC1C,OAAOC,KAAKC,GAAG,CAACH,KAAKI,GAAG,GAAGJ,KAAKK,MAAM,GAAG,IAAIrB,OAAOsB,WAAW,GAAG,KAAK5B;IACzE;IAEA,gGAAgG;IAChG,+FAA+F;IAC/F,MAAM6B,QAAQ,IAAIC;IAClB,MAAMC,WAAW,CAACC;QAChB,IAAIC,UAAU;QACd,KAAK,MAAM,EAAEC,MAAM,EAAEC,WAAW,EAAE,IAAIH,QAAS;YAC7C,MAAMI,OAAO,GAAGD,YAAYE,KAAK,CAAC,CAAC,EAAEF,YAAYR,MAAM,EAAE;YACzD,IAAIE,MAAMS,GAAG,CAACJ,WAAWL,MAAMU,GAAG,CAACL,YAAYE,MAAMH,UAAU;YAC/DJ,MAAMW,GAAG,CAACN,QAAQE;QACpB;QACA,IAAIH,SAASQ;IACf;IAEA,MAAMA,iBAAiB;QACrB,IAAI5B,SAAS;QACb,IAAI,CAACX,QAAQwC,WAAW,EAAE,OAAO5B;QACjC,yEAAyE;QACzE,IAAIO,eAAenB,QAAQyC,cAAc,CAAC;YAAEC,OAAO;YAAUC,UAAU;QAA4B;QACnG5B,aAAaL;QACbA,cAAckC,WAAWhC,MAAMX;IACjC;IAEA,gGAAgG;IAChG,4FAA4F;IAC5F,MAAMY,WAAW,IAAIT,OAAOG,cAAc,CAACsB;IAC3C,IAAK,IAAIgB,OAAuB7C,SAAS6C,QAAQA,SAASrC,SAASsC,eAAe,EAAED,OAAOA,KAAKE,aAAa,CAAE;QAC7GlC,SAASmC,OAAO,CAACH;IACnB;IACA,MAAM7B,WAAW4B,WAAWhC,MAAMV;IAClCQ,cAAckC,WAAWhC,MAAMX;IAC/B,mEAAmE;IACnE,KAAK,MAAMgB,QAAQR,WAAYL,OAAO6C,gBAAgB,CAAChC,MAAML,MAAM;QAAEsC,SAAS;QAAMC,SAAS;IAAK;IAClG,OAAOvC;AACT"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns a data path (`team.1.links.0.label`) into the steps that make the field visible in the
|
|
3
|
+
* admin form: which tab to open, which array/block row to expand. Pure: works on the client
|
|
4
|
+
* config's field tree, no DOM, no React.
|
|
5
|
+
*/
|
|
6
|
+
/** The parts of a client field this module reads (structural subset of Payload's ClientField). */
|
|
7
|
+
export interface FieldLike {
|
|
8
|
+
type: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
fields?: FieldLike[];
|
|
11
|
+
tabs?: {
|
|
12
|
+
name?: string;
|
|
13
|
+
fields: FieldLike[];
|
|
14
|
+
}[];
|
|
15
|
+
blocks?: {
|
|
16
|
+
slug: string;
|
|
17
|
+
fields: FieldLike[];
|
|
18
|
+
}[];
|
|
19
|
+
blockReferences?: (string | {
|
|
20
|
+
slug: string;
|
|
21
|
+
fields: FieldLike[];
|
|
22
|
+
})[];
|
|
23
|
+
}
|
|
24
|
+
export type FocusStep =
|
|
25
|
+
/**
|
|
26
|
+
* Open tab `index` of a tabs field. `ordinal` is the position of that tabs field among the
|
|
27
|
+
* tabs fields of its scope (the form, or the tab content it is nested in), in render order.
|
|
28
|
+
* null: the tabs field sits inside an array/block row, where it can't be located.
|
|
29
|
+
*/
|
|
30
|
+
{
|
|
31
|
+
type: 'tab';
|
|
32
|
+
ordinal: number | null;
|
|
33
|
+
index: number;
|
|
34
|
+
}
|
|
35
|
+
/** Expand row `index` of the array or blocks field at `path`. */
|
|
36
|
+
| {
|
|
37
|
+
type: 'row';
|
|
38
|
+
path: string;
|
|
39
|
+
index: number;
|
|
40
|
+
};
|
|
41
|
+
export interface FocusPlan {
|
|
42
|
+
steps: FocusStep[];
|
|
43
|
+
/** Path of the field to focus (the data path, as Payload's field ids use it). */
|
|
44
|
+
path: string;
|
|
45
|
+
}
|
|
46
|
+
export interface ResolveOptions {
|
|
47
|
+
/** Block definitions referenced by slug (`config.blocksMap` on the client). */
|
|
48
|
+
blocksMap?: Record<string, {
|
|
49
|
+
slug: string;
|
|
50
|
+
fields: FieldLike[];
|
|
51
|
+
} | undefined>;
|
|
52
|
+
/** The `blockType` of the block row at `rowPath`, e.g. `layout.2` (from form state). */
|
|
53
|
+
getBlockType?: (path: string) => string | undefined;
|
|
54
|
+
}
|
|
55
|
+
export declare function resolveFocus(fields: FieldLike[], path: string, options?: ResolveOptions): FocusPlan | null;
|
|
56
|
+
//# sourceMappingURL=resolve-focus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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M,GAAG,SAAS,CAAA;CACpD;AAyBD,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,SAAS,GAAG,IAAI,CAY9G"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { isIndexSegment, parsePath } from '@derweili/payload-visual-editing-core';
|
|
2
|
+
/** Tabs fields directly in this scope (not inside tabs, arrays or blocks), in render order. */ const PASS_THROUGH = new Set([
|
|
3
|
+
'row',
|
|
4
|
+
'collapsible'
|
|
5
|
+
]);
|
|
6
|
+
/** Number of tabs fields this field renders directly into the current scope. */ function countTabs(field) {
|
|
7
|
+
if (field.type === 'tabs') return 1;
|
|
8
|
+
if (PASS_THROUGH.has(field.type) || field.type === 'group') {
|
|
9
|
+
return (field.fields ?? []).reduce((sum, child)=>sum + countTabs(child), 0);
|
|
10
|
+
}
|
|
11
|
+
return 0;
|
|
12
|
+
}
|
|
13
|
+
const join = (prefix, segment)=>prefix ? `${prefix}.${segment}` : String(segment);
|
|
14
|
+
export function resolveFocus(fields, path, options = {}) {
|
|
15
|
+
let segments;
|
|
16
|
+
try {
|
|
17
|
+
segments = parsePath(path);
|
|
18
|
+
} catch {
|
|
19
|
+
return null // malformed, e.g. "a..b"
|
|
20
|
+
;
|
|
21
|
+
}
|
|
22
|
+
if (segments.length === 0) return null;
|
|
23
|
+
const steps = [];
|
|
24
|
+
return walkFields(fields, {
|
|
25
|
+
segments,
|
|
26
|
+
prefix: '',
|
|
27
|
+
ordinal: {
|
|
28
|
+
next: 0
|
|
29
|
+
},
|
|
30
|
+
steps
|
|
31
|
+
}, options) ? {
|
|
32
|
+
steps,
|
|
33
|
+
path
|
|
34
|
+
} : null;
|
|
35
|
+
}
|
|
36
|
+
/** Walks one list of fields; true when the path was found (steps are then filled in). */ function walkFields(fields, walk, options) {
|
|
37
|
+
for (const field of fields){
|
|
38
|
+
const before = walk.ordinal?.next ?? 0;
|
|
39
|
+
if (walkField(field, walk, options)) return true;
|
|
40
|
+
// Whatever a failed search counted inside, this field adds exactly its own tabs fields
|
|
41
|
+
if (walk.ordinal) walk.ordinal.next = before + countTabs(field);
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
function walkField(field, walk, options) {
|
|
46
|
+
const [head, ...rest] = walk.segments;
|
|
47
|
+
if (head === undefined) return false;
|
|
48
|
+
// Fields without data of their own: their children live in the same scope and path
|
|
49
|
+
if (PASS_THROUGH.has(field.type) || field.type === 'group' && !field.name) {
|
|
50
|
+
return walkFields(field.fields ?? [], walk, options);
|
|
51
|
+
}
|
|
52
|
+
if (field.type === 'tabs') {
|
|
53
|
+
const ordinal = walk.ordinal ? walk.ordinal.next : null;
|
|
54
|
+
for (const [index, tab] of (field.tabs ?? []).entries()){
|
|
55
|
+
const step = {
|
|
56
|
+
type: 'tab',
|
|
57
|
+
ordinal,
|
|
58
|
+
index
|
|
59
|
+
};
|
|
60
|
+
// A tab's content is its own scope for nested tabs fields
|
|
61
|
+
const inner = {
|
|
62
|
+
ordinal: walk.ordinal ? {
|
|
63
|
+
next: 0
|
|
64
|
+
} : null,
|
|
65
|
+
steps: [
|
|
66
|
+
...walk.steps,
|
|
67
|
+
step
|
|
68
|
+
]
|
|
69
|
+
};
|
|
70
|
+
const found = tab.name ? head === tab.name && walkFields(tab.fields, {
|
|
71
|
+
...walk,
|
|
72
|
+
...inner,
|
|
73
|
+
segments: rest,
|
|
74
|
+
prefix: join(walk.prefix, tab.name)
|
|
75
|
+
}, options) : walkFields(tab.fields, {
|
|
76
|
+
...walk,
|
|
77
|
+
...inner
|
|
78
|
+
}, options);
|
|
79
|
+
if (found) {
|
|
80
|
+
walk.steps.splice(0, walk.steps.length, ...inner.steps);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (!field.name || field.name !== head) return false;
|
|
87
|
+
const path = join(walk.prefix, head);
|
|
88
|
+
if (field.type === 'group') {
|
|
89
|
+
return rest.length > 0 && walkFields(field.fields ?? [], {
|
|
90
|
+
...walk,
|
|
91
|
+
segments: rest,
|
|
92
|
+
prefix: path
|
|
93
|
+
}, options);
|
|
94
|
+
}
|
|
95
|
+
if (field.type === 'array' || field.type === 'blocks') {
|
|
96
|
+
const [index, ...inside] = rest;
|
|
97
|
+
// A path that ends at the array itself focuses the array field
|
|
98
|
+
if (index === undefined) return true;
|
|
99
|
+
if (!isIndexSegment(index)) return false;
|
|
100
|
+
const rowPath = `${path}.${index}`;
|
|
101
|
+
const rowFields = field.type === 'array' ? field.fields ?? [] : blockFields(field, rowPath, options);
|
|
102
|
+
if (!rowFields) return false;
|
|
103
|
+
const steps = [
|
|
104
|
+
...walk.steps,
|
|
105
|
+
{
|
|
106
|
+
type: 'row',
|
|
107
|
+
path,
|
|
108
|
+
index: Number(index)
|
|
109
|
+
}
|
|
110
|
+
];
|
|
111
|
+
if (inside.length === 0) {
|
|
112
|
+
walk.steps.splice(0, walk.steps.length, ...steps);
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
const inner = {
|
|
116
|
+
segments: inside,
|
|
117
|
+
prefix: rowPath,
|
|
118
|
+
ordinal: null,
|
|
119
|
+
steps
|
|
120
|
+
};
|
|
121
|
+
if (!walkFields(rowFields, inner, options)) return false;
|
|
122
|
+
walk.steps.splice(0, walk.steps.length, ...inner.steps);
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
// Any other named field (text, richText, upload, …) is the target
|
|
126
|
+
return rest.length === 0;
|
|
127
|
+
}
|
|
128
|
+
function blockFields(field, rowPath, options) {
|
|
129
|
+
const blockType = options.getBlockType?.(rowPath);
|
|
130
|
+
if (!blockType) return undefined;
|
|
131
|
+
for (const block of field.blockReferences ?? field.blocks ?? []){
|
|
132
|
+
const resolved = typeof block === 'string' ? options.blocksMap?.[block] : block;
|
|
133
|
+
if (resolved?.slug === blockType) return resolved.fields;
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
//# sourceMappingURL=resolve-focus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/resolve-focus.ts"],"sourcesContent":["import { isIndexSegment, parsePath, type PathSegment } from '@derweili/payload-visual-editing-core'\n\n/**\n * Turns a data path (`team.1.links.0.label`) into the steps that make the field visible in the\n * admin form: which tab to open, which array/block row to expand. Pure: works on the client\n * config's field tree, no DOM, no React.\n */\n\n/** The parts of a client field this module reads (structural subset of Payload's ClientField). */\nexport interface FieldLike {\n type: string\n name?: string\n fields?: FieldLike[]\n tabs?: { name?: string; fields: FieldLike[] }[]\n blocks?: { slug: string; fields: FieldLike[] }[]\n blockReferences?: (string | { slug: string; fields: FieldLike[] })[]\n}\n\nexport type FocusStep =\n /**\n * Open tab `index` of a tabs field. `ordinal` is the position of that tabs field among the\n * tabs fields of its scope (the form, or the tab content it is nested in), in render order.\n * null: the tabs field sits inside an array/block row, where it can't be located.\n */\n | { type: 'tab'; ordinal: number | null; index: number }\n /** Expand row `index` of the array or blocks field at `path`. */\n | { type: 'row'; path: string; index: number }\n\nexport interface FocusPlan {\n steps: FocusStep[]\n /** Path of the field to focus (the data path, as Payload's field ids use it). */\n path: string\n}\n\nexport interface ResolveOptions {\n /** Block definitions referenced by slug (`config.blocksMap` on the client). */\n blocksMap?: Record<string, { slug: string; fields: FieldLike[] } | undefined>\n /** The `blockType` of the block row at `rowPath`, e.g. `layout.2` (from form state). */\n getBlockType?: (path: string) => string | undefined\n}\n\n/** Tabs fields directly in this scope (not inside tabs, arrays or blocks), in render order. */\nconst PASS_THROUGH = new Set(['row', 'collapsible'])\n\n/** Number of tabs fields this field renders directly into the current scope. */\nfunction countTabs(field: FieldLike): number {\n if (field.type === 'tabs') return 1\n if (PASS_THROUGH.has(field.type) || field.type === 'group') {\n return (field.fields ?? []).reduce((sum, child) => sum + countTabs(child), 0)\n }\n return 0\n}\n\ninterface Walk {\n segments: PathSegment[]\n /** Data path walked so far. */\n prefix: string\n /** Tabs fields seen before in the current scope; null inside array/block rows. */\n ordinal: { next: number } | null\n steps: FocusStep[]\n}\n\nconst join = (prefix: string, segment: PathSegment) => (prefix ? `${prefix}.${segment}` : String(segment))\n\nexport function resolveFocus(fields: FieldLike[], path: string, options: ResolveOptions = {}): FocusPlan | null {\n let segments: PathSegment[]\n try {\n segments = parsePath(path)\n } catch {\n return null // malformed, e.g. \"a..b\"\n }\n if (segments.length === 0) return null\n const steps: FocusStep[] = []\n return walkFields(fields, { segments, prefix: '', ordinal: { next: 0 }, steps }, options)\n ? { steps, path }\n : null\n}\n\n/** Walks one list of fields; true when the path was found (steps are then filled in). */\nfunction walkFields(fields: FieldLike[], walk: Walk, options: ResolveOptions): boolean {\n for (const field of fields) {\n const before = walk.ordinal?.next ?? 0\n if (walkField(field, walk, options)) return true\n // Whatever a failed search counted inside, this field adds exactly its own tabs fields\n if (walk.ordinal) walk.ordinal.next = before + countTabs(field)\n }\n return false\n}\n\nfunction walkField(field: FieldLike, walk: Walk, options: ResolveOptions): boolean {\n const [head, ...rest] = walk.segments\n if (head === undefined) return false\n\n // Fields without data of their own: their children live in the same scope and path\n if (PASS_THROUGH.has(field.type) || (field.type === 'group' && !field.name)) {\n return walkFields(field.fields ?? [], walk, options)\n }\n\n if (field.type === 'tabs') {\n const ordinal = walk.ordinal ? walk.ordinal.next : null\n for (const [index, tab] of (field.tabs ?? []).entries()) {\n const step: FocusStep = { type: 'tab', ordinal, index }\n // A tab's content is its own scope for nested tabs fields\n const inner = { ordinal: walk.ordinal ? { next: 0 } : null, steps: [...walk.steps, step] }\n const found = tab.name\n ? head === tab.name &&\n walkFields(tab.fields, { ...walk, ...inner, segments: rest, prefix: join(walk.prefix, tab.name) }, options)\n : walkFields(tab.fields, { ...walk, ...inner }, options)\n if (found) {\n walk.steps.splice(0, walk.steps.length, ...inner.steps)\n return true\n }\n }\n return false\n }\n\n if (!field.name || field.name !== head) return false\n const path = join(walk.prefix, head)\n\n if (field.type === 'group') {\n return rest.length > 0 && walkFields(field.fields ?? [], { ...walk, segments: rest, prefix: path }, options)\n }\n\n if (field.type === 'array' || field.type === 'blocks') {\n const [index, ...inside] = rest\n // A path that ends at the array itself focuses the array field\n if (index === undefined) return true\n if (!isIndexSegment(index)) return false\n const rowPath = `${path}.${index}`\n const rowFields = field.type === 'array' ? (field.fields ?? []) : blockFields(field, rowPath, options)\n if (!rowFields) return false\n const steps = [...walk.steps, { type: 'row', path, index: Number(index) } as const]\n if (inside.length === 0) {\n walk.steps.splice(0, walk.steps.length, ...steps)\n return true\n }\n const inner: Walk = { segments: inside, prefix: rowPath, ordinal: null, steps }\n if (!walkFields(rowFields, inner, options)) return false\n walk.steps.splice(0, walk.steps.length, ...inner.steps)\n return true\n }\n\n // Any other named field (text, richText, upload, …) is the target\n return rest.length === 0\n}\n\nfunction blockFields(field: FieldLike, rowPath: string, options: ResolveOptions): FieldLike[] | undefined {\n const blockType = options.getBlockType?.(rowPath)\n if (!blockType) return undefined\n for (const block of field.blockReferences ?? field.blocks ?? []) {\n const resolved = typeof block === 'string' ? options.blocksMap?.[block] : block\n if (resolved?.slug === blockType) return resolved.fields\n }\n return undefined\n}\n"],"names":["isIndexSegment","parsePath","PASS_THROUGH","Set","countTabs","field","type","has","fields","reduce","sum","child","join","prefix","segment","String","resolveFocus","path","options","segments","length","steps","walkFields","ordinal","next","walk","before","walkField","head","rest","undefined","name","index","tab","tabs","entries","step","inner","found","splice","inside","rowPath","rowFields","blockFields","Number","blockType","getBlockType","block","blockReferences","blocks","resolved","blocksMap","slug"],"mappings":"AAAA,SAASA,cAAc,EAAEC,SAAS,QAA0B,wCAAuC;AAyCnG,6FAA6F,GAC7F,MAAMC,eAAe,IAAIC,IAAI;IAAC;IAAO;CAAc;AAEnD,8EAA8E,GAC9E,SAASC,UAAUC,KAAgB;IACjC,IAAIA,MAAMC,IAAI,KAAK,QAAQ,OAAO;IACl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S1C,MAAM2C,eAAe,IAAI3C,MAAM4C,MAAM,IAAI,EAAE,CAAE;QAC/D,MAAMC,WAAW,OAAOH,UAAU,WAAW7B,QAAQiC,SAAS,EAAE,CAACJ,MAAM,GAAGA;QAC1E,IAAIG,UAAUE,SAASP,WAAW,OAAOK,SAAS1C,MAAM;IAC1D;IACA,OAAOsB;AACT"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { FlattenedBlock, FlattenedField } from 'payload';
|
|
2
|
+
export type EncodeTarget = {
|
|
3
|
+
collection: string;
|
|
4
|
+
id: string | number;
|
|
5
|
+
} | {
|
|
6
|
+
global: string;
|
|
7
|
+
};
|
|
8
|
+
export interface EncodeDocumentArgs {
|
|
9
|
+
fields: FlattenedField[];
|
|
10
|
+
data: Record<string, unknown>;
|
|
11
|
+
target: EncodeTarget;
|
|
12
|
+
/** `req.locale`: a locale code, "all", or undefined without localization. */
|
|
13
|
+
locale: string | undefined;
|
|
14
|
+
/** Locale codes of the config, or null without localization. */
|
|
15
|
+
locales: string[] | null;
|
|
16
|
+
skip: 'auto' | boolean;
|
|
17
|
+
/** Whether a data path should get a ref (include/exclude). */
|
|
18
|
+
shouldEncodePath: (path: string) => boolean;
|
|
19
|
+
/** Resolves blocks referenced by slug (`config.blocks`). */
|
|
20
|
+
resolveBlock: (slug: string) => FlattenedBlock | undefined;
|
|
21
|
+
}
|
|
22
|
+
export declare function encodeDocument({ fields, data, ...walk }: EncodeDocumentArgs): void;
|
|
23
|
+
//# sourceMappingURL=encode-document.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode-document.d.ts","sourceRoot":"","sources":["../../src/server/encode-document.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7D,MAAM,MAAM,YAAY,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAAA;AAE3F,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,MAAM,EAAE,YAAY,CAAA;IACpB,6EAA6E;IAC7E,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,gEAAgE;IAChE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;IACxB,IAAI,EAAE,MAAM,GAAG,OAAO,CAAA;IACtB,8DAA8D;IAC9D,gBAAgB,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAA;IAC3C,4DAA4D;IAC5D,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,cAAc,GAAG,SAAS,CAAA;CAC3D;AASD,wBAAgB,cAAc,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,EAAE,kBAAkB,GAAG,IAAI,CAElF"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walks a document alongside its `flattenedFields` and appends a stega ref to every text value.
|
|
3
|
+
* Fields that can't carry stega text (uploads, relationships, numbers, dates, selects, …, and text
|
|
4
|
+
* left unencoded: excluded or URL/date-like) get a source tag instead (core `source.ts`): populated
|
|
5
|
+
* documents are replaced by a tagged copy, other values are recorded on their parent.
|
|
6
|
+
*
|
|
7
|
+
* `flattenedFields` already drops unnamed tabs, rows and collapsibles (they don't nest data);
|
|
8
|
+
* named tabs appear as `type: 'tab'`. So the walk only follows data nesting: groups, named tabs,
|
|
9
|
+
* arrays and blocks. Mutates `data` in place, like Payload's own afterRead hooks.
|
|
10
|
+
*/ import { appendPath, encodeRef, hasStega, setFieldSource, stegaEncode, withSource } from '@derweili/payload-visual-editing-core';
|
|
11
|
+
const isRecord = (value)=>typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
12
|
+
const isLocalized = (field)=>'localized' in field && field.localized === true;
|
|
13
|
+
export function encodeDocument({ fields, data, ...walk }) {
|
|
14
|
+
encodeFields(walk, fields, data, '', undefined);
|
|
15
|
+
}
|
|
16
|
+
function encodeFields(walk, fields, data, basePath, /** Set once the value is known to belong to one locale. */ valueLocale) {
|
|
17
|
+
for (const field of fields){
|
|
18
|
+
// IDs (document, array row, block) are identifiers, never content
|
|
19
|
+
if (field.name === 'id') continue;
|
|
20
|
+
if ('virtual' in field && field.virtual) continue;
|
|
21
|
+
if (!(field.name in data)) continue;
|
|
22
|
+
const path = appendPath(basePath, field.name);
|
|
23
|
+
const value = data[field.name];
|
|
24
|
+
// locale "all": the outermost localized field holds { de: …, en: … }
|
|
25
|
+
if (walk.locale === 'all' && valueLocale === undefined && isLocalized(field)) {
|
|
26
|
+
if (isRecord(value) && walk.locales) {
|
|
27
|
+
for (const code of walk.locales){
|
|
28
|
+
if (code in value) value[code] = encodeValue(walk, field, value[code], path, code);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const locale = valueLocale ?? (isLocalized(field) && walk.locale !== 'all' ? walk.locale : undefined);
|
|
34
|
+
const encoded = encodeValue(walk, field, value, path, locale);
|
|
35
|
+
data[field.name] = encoded;
|
|
36
|
+
tagSource(walk, field, data, encoded, path, locale);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Data-nesting fields: their content is tagged or encoded field by field. */ const CONTAINERS = new Set([
|
|
40
|
+
'group',
|
|
41
|
+
'tab',
|
|
42
|
+
'array',
|
|
43
|
+
'blocks'
|
|
44
|
+
]);
|
|
45
|
+
/** Gives a value without stega text a source tag (see the module comment). */ function tagSource(walk, field, parent, value, path, locale) {
|
|
46
|
+
if (value === null || value === undefined || CONTAINERS.has(field.type) || field.type === 'richText') return;
|
|
47
|
+
if (typeof value === 'string' && hasStega(value)) return;
|
|
48
|
+
const source = location(walk, path, locale);
|
|
49
|
+
if (field.type === 'upload' || field.type === 'relationship') {
|
|
50
|
+
// Populated documents (objects) carry the tag themselves, so it survives mapping them
|
|
51
|
+
if (Array.isArray(value)) {
|
|
52
|
+
parent[field.name] = value.map((item)=>isRecord(item) ? withSource(item, source) : item);
|
|
53
|
+
} else if (isRecord(value)) {
|
|
54
|
+
parent[field.name] = withSource(value, source);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
setFieldSource(parent, field.name, source);
|
|
58
|
+
}
|
|
59
|
+
function encodeValue(walk, field, value, path, locale) {
|
|
60
|
+
if (value === null || value === undefined) return value;
|
|
61
|
+
switch(field.type){
|
|
62
|
+
case 'text':
|
|
63
|
+
case 'textarea':
|
|
64
|
+
{
|
|
65
|
+
if (typeof value !== 'string' || !walk.shouldEncodePath(path)) return value;
|
|
66
|
+
return stegaEncode(value, ref(walk, path, locale), {
|
|
67
|
+
skip: walk.skip
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
case 'richText':
|
|
71
|
+
{
|
|
72
|
+
if (walk.shouldEncodePath(path)) {
|
|
73
|
+
encodeLexicalText(value, (text)=>stegaEncode(text, ref(walk, path, locale), {
|
|
74
|
+
skip: walk.skip
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
case 'group':
|
|
80
|
+
case 'tab':
|
|
81
|
+
{
|
|
82
|
+
if (isRecord(value)) encodeFields(walk, field.flattenedFields, value, path, locale);
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
case 'array':
|
|
86
|
+
{
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
value.forEach((row, index)=>{
|
|
89
|
+
if (isRecord(row)) encodeFields(walk, field.flattenedFields, row, appendPath(path, index), locale);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
case 'blocks':
|
|
95
|
+
{
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
value.forEach((row, index)=>{
|
|
98
|
+
if (!isRecord(row) || typeof row.blockType !== 'string') return;
|
|
99
|
+
const block = findBlock(walk, field.blockReferences ?? field.blocks, row.blockType);
|
|
100
|
+
if (block) encodeFields(walk, block.flattenedFields, row, appendPath(path, index), locale);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
default:
|
|
106
|
+
// relationship/upload: a populated document is encoded by its own collection's hook.
|
|
107
|
+
// number, date, select, email, json, code, point, checkbox, join: not text content.
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function findBlock(walk, blocks, slug) {
|
|
112
|
+
for (const block of blocks){
|
|
113
|
+
if (typeof block === 'string') {
|
|
114
|
+
if (block === slug) return walk.resolveBlock(slug);
|
|
115
|
+
} else if (block.slug === slug) {
|
|
116
|
+
return block;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
function location(walk, path, locale) {
|
|
122
|
+
return {
|
|
123
|
+
...walk.target,
|
|
124
|
+
path,
|
|
125
|
+
...locale ? {
|
|
126
|
+
locale
|
|
127
|
+
} : {}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function ref(walk, path, locale) {
|
|
131
|
+
return encodeRef(location(walk, path, locale));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Encodes the text nodes of a Lexical state. Every node gets the ref of the field itself;
|
|
135
|
+
* node-level refs are a later option (ADR 0001). Only `children` are followed, so populated
|
|
136
|
+
* documents inside upload/relationship nodes and the fields of link and block nodes stay as they are.
|
|
137
|
+
*/ function encodeLexicalText(state, encode) {
|
|
138
|
+
if (!isRecord(state) || !isRecord(state.root)) return;
|
|
139
|
+
const visit = (node)=>{
|
|
140
|
+
if (!isRecord(node)) return;
|
|
141
|
+
if (node.type === 'text' && typeof node.text === 'string') node.text = encode(node.text);
|
|
142
|
+
if (Array.isArray(node.children)) node.children.forEach(visit);
|
|
143
|
+
};
|
|
144
|
+
visit(state.root);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
//# sourceMappingURL=encode-document.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/encode-document.ts"],"sourcesContent":["/**\n * Walks a document alongside its `flattenedFields` and appends a stega ref to every text value.\n * Fields that can't carry stega text (uploads, relationships, numbers, dates, selects, …, and text\n * left unencoded: excluded or URL/date-like) get a source tag instead (core `source.ts`): populated\n * documents are replaced by a tagged copy, other values are recorded on their parent.\n *\n * `flattenedFields` already drops unnamed tabs, rows and collapsibles (they don't nest data);\n * named tabs appear as `type: 'tab'`. So the walk only follows data nesting: groups, named tabs,\n * arrays and blocks. Mutates `data` in place, like Payload's own afterRead hooks.\n */\nimport {\n appendPath,\n encodeRef,\n hasStega,\n setFieldSource,\n stegaEncode,\n withSource,\n type SourceLocation,\n} from '@derweili/payload-visual-editing-core'\nimport type { FlattenedBlock, FlattenedField } from 'payload'\n\nexport type EncodeTarget = { collection: string; id: string | number } | { global: string }\n\nexport interface EncodeDocumentArgs {\n fields: FlattenedField[]\n data: Record<string, unknown>\n target: EncodeTarget\n /** `req.locale`: a locale code, \"all\", or undefined without localization. */\n locale: string | undefined\n /** Locale codes of the config, or null without localization. */\n locales: string[] | null\n skip: 'auto' | boolean\n /** Whether a data path should get a ref (include/exclude). */\n shouldEncodePath: (path: string) => boolean\n /** Resolves blocks referenced by slug (`config.blocks`). */\n resolveBlock: (slug: string) => FlattenedBlock | undefined\n}\n\ntype Walk = Omit<EncodeDocumentArgs, 'fields' | 'data'>\n\nconst isRecord = (value: unknown): value is Record<string, any> =>\n typeof value === 'object' && value !== null && !Array.isArray(value)\n\nconst isLocalized = (field: FlattenedField) => 'localized' in field && field.localized === true\n\nexport function encodeDocument({ fields, data, ...walk }: EncodeDocumentArgs): void {\n encodeFields(walk, fields, data, '', undefined)\n}\n\nfunction encodeFields(\n walk: Walk,\n fields: FlattenedField[],\n data: Record<string, any>,\n basePath: string,\n /** Set once the value is known to belong to one locale. */\n valueLocale: string | undefined,\n): void {\n for (const field of fields) {\n // IDs (document, array row, block) are identifiers, never content\n if (field.name === 'id') continue\n if ('virtual' in field && field.virtual) continue\n if (!(field.name in data)) continue\n\n const path = appendPath(basePath, field.name)\n const value = data[field.name]\n\n // locale \"all\": the outermost localized field holds { de: …, en: … }\n if (walk.locale === 'all' && valueLocale === undefined && isLocalized(field)) {\n if (isRecord(value) && walk.locales) {\n for (const code of walk.locales) {\n if (code in value) value[code] = encodeValue(walk, field, value[code], path, code)\n }\n }\n continue\n }\n\n const locale =\n valueLocale ?? (isLocalized(field) && walk.locale !== 'all' ? walk.locale : undefined)\n const encoded = encodeValue(walk, field, value, path, locale)\n data[field.name] = encoded\n tagSource(walk, field, data, encoded, path, locale)\n }\n}\n\n/** Data-nesting fields: their content is tagged or encoded field by field. */\nconst CONTAINERS = new Set(['group', 'tab', 'array', 'blocks'])\n\n/** Gives a value without stega text a source tag (see the module comment). */\nfunction tagSource(\n walk: Walk,\n field: FlattenedField,\n parent: Record<string, any>,\n value: unknown,\n path: string,\n locale: string | undefined,\n): void {\n if (value === null || value === undefined || CONTAINERS.has(field.type) || field.type === 'richText') return\n if (typeof value === 'string' && hasStega(value)) return\n const source = location(walk, path, locale)\n if (field.type === 'upload' || field.type === 'relationship') {\n // Populated documents (objects) carry the tag themselves, so it survives mapping them\n if (Array.isArray(value)) {\n parent[field.name] = value.map((item) => (isRecord(item) ? withSource(item, source) : item))\n } else if (isRecord(value)) {\n parent[field.name] = withSource(value, source)\n }\n }\n setFieldSource(parent, field.name, source)\n}\n\nfunction encodeValue(\n walk: Walk,\n field: FlattenedField,\n value: unknown,\n path: string,\n locale: string | undefined,\n): unknown {\n if (value === null || value === undefined) return value\n\n switch (field.type) {\n case 'text':\n case 'textarea': {\n if (typeof value !== 'string' || !walk.shouldEncodePath(path)) return value\n return stegaEncode(value, ref(walk, path, locale), { skip: walk.skip })\n }\n case 'richText': {\n if (walk.shouldEncodePath(path)) {\n encodeLexicalText(value, (text) => stegaEncode(text, ref(walk, path, locale), { skip: walk.skip }))\n }\n return value\n }\n case 'group':\n case 'tab': {\n if (isRecord(value)) encodeFields(walk, field.flattenedFields, value, path, locale)\n return value\n }\n case 'array': {\n if (Array.isArray(value)) {\n value.forEach((row, index) => {\n if (isRecord(row)) encodeFields(walk, field.flattenedFields, row, appendPath(path, index), locale)\n })\n }\n return value\n }\n case 'blocks': {\n if (Array.isArray(value)) {\n value.forEach((row, index) => {\n if (!isRecord(row) || typeof row.blockType !== 'string') return\n const block = findBlock(walk, field.blockReferences ?? field.blocks, row.blockType)\n if (block) encodeFields(walk, block.flattenedFields, row, appendPath(path, index), locale)\n })\n }\n return value\n }\n default:\n // relationship/upload: a populated document is encoded by its own collection's hook.\n // number, date, select, email, json, code, point, checkbox, join: not text content.\n return value\n }\n}\n\nfunction findBlock(walk: Walk, blocks: (FlattenedBlock | string)[], slug: string) {\n for (const block of blocks) {\n if (typeof block === 'string') {\n if (block === slug) return walk.resolveBlock(slug)\n } else if (block.slug === slug) {\n return block\n }\n }\n return undefined\n}\n\nfunction location(walk: Walk, path: string, locale: string | undefined): SourceLocation {\n return { ...walk.target, path, ...(locale ? { locale } : {}) } as SourceLocation\n}\n\nfunction ref(walk: Walk, path: string, locale: string | undefined) {\n return encodeRef(location(walk, path, locale))\n}\n\n/**\n * Encodes the text nodes of a Lexical state. Every node gets the ref of the field itself;\n * node-level refs are a later option (ADR 0001). Only `children` are followed, so populated\n * documents inside upload/relationship nodes and the fields of link and block nodes stay as they are.\n */\nfunction encodeLexicalText(state: unknown, encode: (text: string) => string): void {\n if (!isRecord(state) || !isRecord(state.root)) return\n const visit = (node: unknown) => {\n if (!isRecord(node)) return\n if (node.type === 'text' && typeof node.text === 'string') node.text = encode(node.text)\n if (Array.isArray(node.children)) node.children.forEach(visit)\n }\n visit(state.root)\n}\n"],"names":["appendPath","encodeRef","hasStega","setFieldSource","stegaEncode","withSource","isRecord","value","Array","isArray","isLocalized","field","localized","encodeDocument","fields","data","walk","encodeFields","undefined","basePath","valueLocale","name","virtual","path","locale","locales","code","encodeValue","encoded","tagSource","CONTAINERS","Set","parent","has","type","source","location","map","item","shouldEncodePath","ref","skip","encodeLexicalText","text","flattenedFields","forEach","row","index","blockType","block","findBlock","blockReferences","blocks","slug","resolveBlock","target","state","encode","root","visit","node","children"],"mappings":"AAAA;;;;;;;;;CASC,GACD,SACEA,UAAU,EACVC,SAAS,EACTC,QAAQ,EACRC,cAAc,EACdC,WAAW,EACXC,UAAU,QAEL,wCAAuC;AAsB9C,MAAMC,WAAW,CAACC,QAChB,OAAOA,UAAU,YAAYA,UAAU,QAAQ,CAACC,MAAMC,OAAO,CAACF;AAEhE,MAAMG,cAAc,CAACC,QAA0B,eAAeA,SAASA,MAAMC,SAAS,KAAK;AAE3F,OAAO,SAASC,eAAe,EAAEC,MAAM,EAAEC,IAAI,EAAE,GAAGC,MAA0B;IAC1EC,aAAaD,MAAMF,QAAQC,MAAM,IAAIG;AACvC;AAEA,SAASD,aACPD,IAAU,EACVF,MAAwB,EACxBC,IAAyB,EACzBI,QAAgB,EAChB,yDAAyD,GACzDC,WAA+B;IAE/B,KAAK,MAAMT,SAASG,OAAQ;QAC1B,kEAAkE;QAClE,IAAIH,MAAMU,IAAI,KAAK,MAAM;QACzB,IAAI,aAAaV,SAASA,MAAMW,OAAO,EAAE;QACzC,IAAI,CAAEX,CAAAA,MAAMU,IAAI,IAAIN,IAAG,GAAI;QAE3B,MAAMQ,OAAOvB,WAAWmB,UAAUR,MAAMU,IAAI;QAC5C,MAAMd,QAAQQ,IAAI,CAACJ,MAAMU,IAAI,CAAC;QAE9B,qEAAqE;QACrE,IAAIL,KAAKQ,MAAM,KAAK,SAASJ,gBAAgBF,aAAaR,YAAYC,QAAQ;YAC5E,IAAIL,SAASC,UAAUS,KAAKS,OAAO,EAAE;gBACnC,KAAK,MAAMC,QAAQV,KAAKS,OAAO,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