@widgetic/editor 4.1.18 → 4.1.20
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/dist/components/WidgetPreview.svelte +77 -14
- package/dist/components/editors/CompositionEditorApp.svelte +71 -35
- package/dist/components/editors/CompositionEditorApp.svelte.d.ts +3 -1
- package/dist/components/editors/ContentTabBulkDrop.svelte +268 -0
- package/dist/components/editors/ContentTabBulkDrop.svelte.d.ts +37 -0
- package/dist/components/editors/EmbedAssistantPanel.svelte +303 -0
- package/dist/components/editors/EmbedAssistantPanel.svelte.d.ts +29 -0
- package/dist/components/editors/EmbedEditor.svelte +221 -15
- package/dist/components/editors/EmbedEditor.svelte.d.ts +31 -3
- package/dist/components/editors/PropsEditor.svelte +223 -30
- package/dist/components/editors/PropsEditor.svelte.d.ts +11 -0
- package/dist/components/editors/propsEditorContentStateFlatten.d.ts +12 -0
- package/dist/components/editors/propsEditorContentStateFlatten.js +40 -0
- package/dist/components/ic/BrowserInputController.svelte +184 -21
- package/dist/components/ic/BrowserInputController.svelte.d.ts +2 -0
- package/dist/components/ic/CheckboxInputController.svelte +11 -0
- package/dist/components/ic/ColorPickerInputController.svelte +2 -1
- package/dist/components/ic/DatePickerInputController.svelte +5 -3
- package/dist/components/ic/FontInputController.svelte +9 -4
- package/dist/components/ic/InputControllerHeader.svelte +4 -3
- package/dist/components/ic/KnowledgeDocumentsInputController.svelte +13 -0
- package/dist/components/ic/NumberControls.svelte +140 -17
- package/dist/components/ic/RadioGroupInputController.svelte +11 -0
- package/dist/components/ic/RangeCalendarInputController.svelte +38 -13
- package/dist/components/ic/TextAreaInputController.svelte +593 -321
- package/dist/components/ic/TextAreaPreviewInputController.svelte +46 -58
- package/dist/components/ic/VectorInputController.svelte +0 -1
- package/dist/components/ic/numberControlsValueArea.d.ts +29 -0
- package/dist/components/ic/numberControlsValueArea.js +51 -0
- package/dist/components/ic/optionGroupCardPointerDown.d.ts +9 -0
- package/dist/components/ic/optionGroupCardPointerDown.js +20 -0
- package/dist/components/ic/shared/EditorFileBrowserOverlay.svelte +48 -6
- package/dist/components/ic/shared/EditorFileBrowserOverlay.svelte.d.ts +4 -1
- package/dist/components/ic/shared/EditorSelectedFilePreviewOverlay.svelte +34 -0
- package/dist/components/ic/shared/EditorSelectedFilePreviewOverlay.svelte.d.ts +24 -0
- package/dist/components/ic/shared/fileBrowserOverlayEscape.d.ts +29 -0
- package/dist/components/ic/shared/fileBrowserOverlayEscape.js +21 -0
- package/dist/composition/compositionApi.d.ts +16 -0
- package/dist/composition/compositionApi.js +16 -1
- package/dist/composition/compositionEditorController.d.ts +46 -0
- package/dist/composition/compositionEditorController.js +277 -5
- package/dist/composition/compositionEditorSchemaCache.d.ts +35 -0
- package/dist/composition/compositionEditorSchemaCache.js +82 -0
- package/dist/contentBulkDrop/applyContentPlan.d.ts +18 -0
- package/dist/contentBulkDrop/applyContentPlan.js +55 -0
- package/dist/contentBulkDrop/bulkDropSummary.d.ts +4 -0
- package/dist/contentBulkDrop/bulkDropSummary.js +12 -0
- package/dist/contentBulkDrop/dragIntent.d.ts +7 -0
- package/dist/contentBulkDrop/dragIntent.js +24 -0
- package/dist/contentBulkDrop/filterDropFiles.d.ts +2 -0
- package/dist/contentBulkDrop/filterDropFiles.js +21 -0
- package/dist/contentBulkDrop/formatFileSize.d.ts +2 -0
- package/dist/contentBulkDrop/formatFileSize.js +13 -0
- package/dist/contentBulkDrop/types.d.ts +34 -0
- package/dist/contentBulkDrop/types.js +1 -0
- package/dist/contentBulkDrop/uploadQueue.d.ts +41 -0
- package/dist/contentBulkDrop/uploadQueue.js +114 -0
- package/dist/drafts/compositionDraft.d.ts +65 -0
- package/dist/drafts/compositionDraft.js +136 -0
- package/dist/drafts/compositionEditorViewState.d.ts +9 -0
- package/dist/drafts/compositionEditorViewState.js +33 -0
- package/dist/drafts/draftJournal.d.ts +59 -0
- package/dist/drafts/draftJournal.js +261 -0
- package/dist/embed/__fixtures__/gatewayGeneratedEmbedCodes.json +25 -0
- package/dist/embed/__fixtures__/generatedEmbedCodes.json +320 -0
- package/dist/embed/embedSchema.json +409 -0
- package/dist/embed/parseCompositionEmbedCode.d.ts +21 -0
- package/dist/embed/parseCompositionEmbedCode.js +379 -0
- package/dist/embedAssistant/embedAssistantEvents.d.ts +22 -0
- package/dist/embedAssistant/embedAssistantEvents.js +39 -0
- package/dist/embedAssistant/embedAssistantStream.d.ts +30 -0
- package/dist/embedAssistant/embedAssistantStream.js +125 -0
- package/dist/embedAssistant/highlightTiming.d.ts +1 -0
- package/dist/embedAssistant/highlightTiming.js +6 -0
- package/dist/embedAssistant/types.d.ts +27 -0
- package/dist/embedAssistant/types.js +1 -0
- package/dist/i18n/labels.js +8 -4
- package/dist/i18n/language.d.ts +38 -2
- package/dist/i18n/messages.d.ts +38 -1
- package/dist/i18n/messages.js +40 -1
- package/dist/i18n/translate.d.ts +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -0
- package/dist/utils/FontManager.d.ts +2 -0
- package/dist/utils/FontManager.js +16 -1
- package/dist/utils/isEditableEventTarget.d.ts +5 -0
- package/dist/utils/isEditableEventTarget.js +25 -0
- package/dist/utils/linkUrlValidation.d.ts +17 -0
- package/dist/utils/linkUrlValidation.js +120 -0
- package/dist/utils/mediaFileValue.d.ts +12 -0
- package/dist/utils/mediaFileValue.js +24 -0
- package/dist/utils/placeRichTextCaretAtDocumentEnd.d.ts +11 -0
- package/dist/utils/placeRichTextCaretAtDocumentEnd.js +26 -0
- package/package.json +7 -7
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Whether a drag carries files (not plain text or a URL from the page). */
|
|
2
|
+
export function dragCarriesFiles(dataTransfer) {
|
|
3
|
+
if (!dataTransfer)
|
|
4
|
+
return false;
|
|
5
|
+
return Array.from(dataTransfer.types ?? []).includes('Files');
|
|
6
|
+
}
|
|
7
|
+
export const CONTENT_FIELD_FILE_DROP_SELECTOR = '[data-content-field-file-drop]';
|
|
8
|
+
/** True when the event target is over a per-field file drop zone (browser / knowledge panel). */
|
|
9
|
+
export function isOverContentFieldFileDrop(target) {
|
|
10
|
+
if (!target || typeof target !== 'object')
|
|
11
|
+
return false;
|
|
12
|
+
const element = target;
|
|
13
|
+
if (typeof element.closest !== 'function')
|
|
14
|
+
return false;
|
|
15
|
+
return Boolean(element.closest(CONTENT_FIELD_FILE_DROP_SELECTOR));
|
|
16
|
+
}
|
|
17
|
+
/** Bulk tab drop only when the drag carries files and is not aimed at a field-level drop target. */
|
|
18
|
+
export function shouldStartBulkContentDrop(event) {
|
|
19
|
+
if (!dragCarriesFiles(event.dataTransfer))
|
|
20
|
+
return false;
|
|
21
|
+
if (isOverContentFieldFileDrop(event.target))
|
|
22
|
+
return false;
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Files accepted from a drag-and-drop (folders and non-file entries are ignored). */
|
|
2
|
+
export function filesFromDataTransfer(dataTransfer) {
|
|
3
|
+
if (!dataTransfer)
|
|
4
|
+
return [];
|
|
5
|
+
const collected = [];
|
|
6
|
+
if (dataTransfer.items?.length) {
|
|
7
|
+
for (const item of [...dataTransfer.items]) {
|
|
8
|
+
if (item.kind !== 'file')
|
|
9
|
+
continue;
|
|
10
|
+
const file = item.getAsFile();
|
|
11
|
+
if (file)
|
|
12
|
+
collected.push(file);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
else if (dataTransfer.files?.length) {
|
|
16
|
+
for (const file of [...dataTransfer.files]) {
|
|
17
|
+
collected.push(file);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return collected;
|
|
21
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Human-readable size for upload rows (not translated; units are universal). */
|
|
2
|
+
export function formatFileSize(bytes) {
|
|
3
|
+
if (!Number.isFinite(bytes) || bytes < 0)
|
|
4
|
+
return '0 B';
|
|
5
|
+
if (bytes < 1024)
|
|
6
|
+
return `${Math.round(bytes)} B`;
|
|
7
|
+
if (bytes < 1024 * 1024) {
|
|
8
|
+
const kilobytes = bytes / 1024;
|
|
9
|
+
return `${kilobytes < 10 ? kilobytes.toFixed(1) : Math.round(kilobytes)} KB`;
|
|
10
|
+
}
|
|
11
|
+
const megabytes = bytes / (1024 * 1024);
|
|
12
|
+
return `${megabytes < 10 ? megabytes.toFixed(1) : Math.round(megabytes)} MB`;
|
|
13
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Wire shape returned by POST /compositions/:id/content-items/from-files. */
|
|
2
|
+
export type ContentFromFilesSource = 'paired' | 'single';
|
|
3
|
+
export type LibraryOnlyReason = 'no_matching_field' | 'unsupported_type' | 'too_large' | 'duplicate_slot';
|
|
4
|
+
export interface ContentFromFilesAssignment {
|
|
5
|
+
fieldPath: string;
|
|
6
|
+
fieldLabel: string;
|
|
7
|
+
fileName: string;
|
|
8
|
+
mediaId: string | null;
|
|
9
|
+
mimeType: string;
|
|
10
|
+
}
|
|
11
|
+
export interface ContentFromFilesPlannedItem {
|
|
12
|
+
key: string;
|
|
13
|
+
label: string;
|
|
14
|
+
source: ContentFromFilesSource;
|
|
15
|
+
assignments: ContentFromFilesAssignment[];
|
|
16
|
+
}
|
|
17
|
+
export interface ContentFromFilesLibraryOnly {
|
|
18
|
+
fileName: string;
|
|
19
|
+
mediaId: string | null;
|
|
20
|
+
reason: LibraryOnlyReason;
|
|
21
|
+
}
|
|
22
|
+
export interface ContentFromFilesPlan {
|
|
23
|
+
items: ContentFromFilesPlannedItem[];
|
|
24
|
+
libraryOnly: ContentFromFilesLibraryOnly[];
|
|
25
|
+
summary: {
|
|
26
|
+
added: number;
|
|
27
|
+
libraryOnly: number;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export interface ContentFromFilesApiResult {
|
|
31
|
+
plan: ContentFromFilesPlan;
|
|
32
|
+
applied: boolean;
|
|
33
|
+
content?: Record<string, unknown>;
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { MediaLibraryFile, MediaLibraryProvider } from '@widgetic/file-browser';
|
|
2
|
+
export declare const BULK_DROP_UPLOAD_CONCURRENCY = 3;
|
|
3
|
+
export type UploadQueueErrorReason = 'upload_failed' | 'too_large';
|
|
4
|
+
export type UploadQueueFileState = {
|
|
5
|
+
status: 'queued';
|
|
6
|
+
} | {
|
|
7
|
+
status: 'uploading';
|
|
8
|
+
progress: number;
|
|
9
|
+
} | {
|
|
10
|
+
status: 'done';
|
|
11
|
+
file: MediaLibraryFile;
|
|
12
|
+
} | {
|
|
13
|
+
status: 'error';
|
|
14
|
+
reason: UploadQueueErrorReason;
|
|
15
|
+
canRetry: boolean;
|
|
16
|
+
};
|
|
17
|
+
export interface UploadQueueEntry {
|
|
18
|
+
localId: string;
|
|
19
|
+
source: File;
|
|
20
|
+
state: UploadQueueFileState;
|
|
21
|
+
}
|
|
22
|
+
export interface UploadQueueSnapshot {
|
|
23
|
+
entries: UploadQueueEntry[];
|
|
24
|
+
activeCount: number;
|
|
25
|
+
allFinished: boolean;
|
|
26
|
+
failures: UploadQueueEntry[];
|
|
27
|
+
}
|
|
28
|
+
export declare function createUploadQueue(mediaLibrary: MediaLibraryProvider): {
|
|
29
|
+
subscribe: (listener: (snapshot: UploadQueueSnapshot) => void) => () => void;
|
|
30
|
+
enqueue: (files: File[]) => void;
|
|
31
|
+
retry: (localId: string) => void;
|
|
32
|
+
retryFailures: () => void;
|
|
33
|
+
hasActiveUploads: () => boolean;
|
|
34
|
+
successfulUploads: () => Array<{
|
|
35
|
+
sourceName: string;
|
|
36
|
+
file: MediaLibraryFile;
|
|
37
|
+
}>;
|
|
38
|
+
reset: () => void;
|
|
39
|
+
getSnapshot: () => UploadQueueSnapshot;
|
|
40
|
+
};
|
|
41
|
+
export type BulkDropUploadQueue = ReturnType<typeof createUploadQueue>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/** Same limit as `createWidgeticMediaLibrary` in @widgetic/file-browser. */
|
|
2
|
+
const WIDGETIC_MULTIPART_MAX_BYTES = 10 * 1024 * 1024;
|
|
3
|
+
export const BULK_DROP_UPLOAD_CONCURRENCY = 3;
|
|
4
|
+
export function createUploadQueue(mediaLibrary) {
|
|
5
|
+
const entries = [];
|
|
6
|
+
let listeners = [];
|
|
7
|
+
let aborted = false;
|
|
8
|
+
function notify() {
|
|
9
|
+
const snapshot = getSnapshot();
|
|
10
|
+
for (const listener of listeners)
|
|
11
|
+
listener(snapshot);
|
|
12
|
+
}
|
|
13
|
+
function getSnapshot() {
|
|
14
|
+
const activeCount = entries.filter((entry) => entry.state.status === 'uploading').length;
|
|
15
|
+
const failures = entries.filter((entry) => entry.state.status === 'error');
|
|
16
|
+
const allFinished = entries.length > 0 &&
|
|
17
|
+
entries.every((entry) => entry.state.status === 'done' || entry.state.status === 'error');
|
|
18
|
+
return { entries: [...entries], activeCount, allFinished, failures };
|
|
19
|
+
}
|
|
20
|
+
function subscribe(listener) {
|
|
21
|
+
listeners.push(listener);
|
|
22
|
+
listener(getSnapshot());
|
|
23
|
+
return () => {
|
|
24
|
+
listeners = listeners.filter((candidate) => candidate !== listener);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function enqueue(files) {
|
|
28
|
+
aborted = false;
|
|
29
|
+
for (const source of files) {
|
|
30
|
+
const localId = `${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
31
|
+
if (source.size > WIDGETIC_MULTIPART_MAX_BYTES) {
|
|
32
|
+
entries.push({
|
|
33
|
+
localId,
|
|
34
|
+
source,
|
|
35
|
+
state: { status: 'error', reason: 'too_large', canRetry: false }
|
|
36
|
+
});
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
entries.push({ localId, source, state: { status: 'queued' } });
|
|
40
|
+
}
|
|
41
|
+
notify();
|
|
42
|
+
void pump();
|
|
43
|
+
}
|
|
44
|
+
async function uploadOne(entry) {
|
|
45
|
+
entry.state = { status: 'uploading', progress: 0 };
|
|
46
|
+
notify();
|
|
47
|
+
try {
|
|
48
|
+
const uploaded = await mediaLibrary.upload({
|
|
49
|
+
file: entry.source,
|
|
50
|
+
onProgress: (progress) => {
|
|
51
|
+
entry.state = { status: 'uploading', progress };
|
|
52
|
+
notify();
|
|
53
|
+
},
|
|
54
|
+
signal: new AbortController().signal
|
|
55
|
+
});
|
|
56
|
+
entry.state = { status: 'done', file: uploaded };
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
entry.state = { status: 'error', reason: 'upload_failed', canRetry: true };
|
|
60
|
+
}
|
|
61
|
+
notify();
|
|
62
|
+
}
|
|
63
|
+
async function pump() {
|
|
64
|
+
if (aborted)
|
|
65
|
+
return;
|
|
66
|
+
const uploading = entries.filter((entry) => entry.state.status === 'uploading').length;
|
|
67
|
+
const slots = Math.max(0, BULK_DROP_UPLOAD_CONCURRENCY - uploading);
|
|
68
|
+
const queued = entries.filter((entry) => entry.state.status === 'queued').slice(0, slots);
|
|
69
|
+
await Promise.all(queued.map(async (entry) => {
|
|
70
|
+
await uploadOne(entry);
|
|
71
|
+
await pump();
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
function retry(localId) {
|
|
75
|
+
const entry = entries.find((candidate) => candidate.localId === localId);
|
|
76
|
+
if (!entry || entry.state.status !== 'error' || !entry.state.canRetry)
|
|
77
|
+
return;
|
|
78
|
+
entry.state = { status: 'queued' };
|
|
79
|
+
notify();
|
|
80
|
+
void pump();
|
|
81
|
+
}
|
|
82
|
+
function retryFailures() {
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
if (entry.state.status === 'error' && entry.state.canRetry) {
|
|
85
|
+
entry.state = { status: 'queued' };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
notify();
|
|
89
|
+
void pump();
|
|
90
|
+
}
|
|
91
|
+
function hasActiveUploads() {
|
|
92
|
+
return entries.some((entry) => entry.state.status === 'queued' || entry.state.status === 'uploading');
|
|
93
|
+
}
|
|
94
|
+
function successfulUploads() {
|
|
95
|
+
return entries
|
|
96
|
+
.filter((entry) => entry.state.status === 'done')
|
|
97
|
+
.map((entry) => ({ sourceName: entry.source.name, file: entry.state.file }));
|
|
98
|
+
}
|
|
99
|
+
function reset() {
|
|
100
|
+
aborted = true;
|
|
101
|
+
entries.length = 0;
|
|
102
|
+
notify();
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
subscribe,
|
|
106
|
+
enqueue,
|
|
107
|
+
retry,
|
|
108
|
+
retryFailures,
|
|
109
|
+
hasActiveUploads,
|
|
110
|
+
successfulUploads,
|
|
111
|
+
reset,
|
|
112
|
+
getSnapshot
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composition editor draft payload and restore rule (crash/refresh safety net).
|
|
3
|
+
*/
|
|
4
|
+
import type { JsonRecord } from '../composition/compositionHelpers.js';
|
|
5
|
+
import type { DeviceView } from '../composition/compositionEditorController.js';
|
|
6
|
+
import type { DraftJournal, DraftJournalEntry } from './draftJournal.js';
|
|
7
|
+
export declare const COMPOSITION_DRAFT_NAMESPACE = "composition";
|
|
8
|
+
export declare const STALE_DRAFT_KEY_SUFFIX = ":stale";
|
|
9
|
+
export declare const STALE_DRAFT_MAX_AGE_MS: number;
|
|
10
|
+
export interface CompositionDraftPayload {
|
|
11
|
+
designValues: JsonRecord;
|
|
12
|
+
contentValues: JsonRecord;
|
|
13
|
+
contentItems: unknown[];
|
|
14
|
+
embedWidth: number;
|
|
15
|
+
embedHeight: number;
|
|
16
|
+
}
|
|
17
|
+
export interface CompositionEditorViewStateV1 {
|
|
18
|
+
v: 1;
|
|
19
|
+
activeTab: 'design' | 'content';
|
|
20
|
+
designOpenAccordion: string[];
|
|
21
|
+
contentOpenAccordion: string[];
|
|
22
|
+
openContentItemIds: string[];
|
|
23
|
+
designScrollTop: number;
|
|
24
|
+
contentScrollTop: number;
|
|
25
|
+
deviceView: DeviceView;
|
|
26
|
+
activeContentItemId: string | null;
|
|
27
|
+
}
|
|
28
|
+
export declare const COMPOSITION_VIEW_STATE_PREFIX = "widgetic:editor-view:composition:";
|
|
29
|
+
export interface DraftRestoreResult {
|
|
30
|
+
applied: boolean;
|
|
31
|
+
discardedAsStale: boolean;
|
|
32
|
+
clearedAsNoOp: boolean;
|
|
33
|
+
savedAtMs?: number;
|
|
34
|
+
}
|
|
35
|
+
export declare function payloadsEqual(left: CompositionDraftPayload, right: CompositionDraftPayload): boolean;
|
|
36
|
+
/** What the server copy looks like in the editor after a fresh load (parts autosave can send). */
|
|
37
|
+
export declare function draftPayloadFromEditorState(state: {
|
|
38
|
+
designValues: JsonRecord;
|
|
39
|
+
contentValues: JsonRecord;
|
|
40
|
+
contentItems: unknown[];
|
|
41
|
+
embedWidth: number;
|
|
42
|
+
embedHeight: number;
|
|
43
|
+
}): CompositionDraftPayload;
|
|
44
|
+
/** Revision string for restore matching: content version when the API provides one, else a fingerprint of loaded values. */
|
|
45
|
+
export declare function serverBaseRevision(contentVersion: number | null, serverPayload: CompositionDraftPayload): string;
|
|
46
|
+
/** Moves an unmatched draft under a `:stale` key so it can be inspected for 24 h without applying. */
|
|
47
|
+
export declare function preserveStaleDraft<T>(journal: DraftJournal<T>, compositionId: string, entry: DraftJournalEntry<T>, logger?: {
|
|
48
|
+
debug(message: string, detail?: unknown): void;
|
|
49
|
+
}): void;
|
|
50
|
+
export declare function readStaleDraft<T>(journal: DraftJournal<T>, compositionId: string, nowMs: number): DraftJournalEntry<T> | null;
|
|
51
|
+
export declare function applyDraftRestoreRule(options: {
|
|
52
|
+
entry: DraftJournalEntry<CompositionDraftPayload> | null;
|
|
53
|
+
serverRevision: string;
|
|
54
|
+
serverPayload: CompositionDraftPayload;
|
|
55
|
+
journal: DraftJournal<CompositionDraftPayload>;
|
|
56
|
+
compositionId: string;
|
|
57
|
+
nowMs: number;
|
|
58
|
+
logger?: {
|
|
59
|
+
debug(message: string, detail?: unknown): void;
|
|
60
|
+
};
|
|
61
|
+
}): DraftRestoreResult;
|
|
62
|
+
export declare function validateCompositionViewState(raw: unknown, validContentItemIds: ReadonlySet<string>): CompositionEditorViewStateV1 | null;
|
|
63
|
+
export declare function viewStateStorageKey(compositionId: string): string;
|
|
64
|
+
export declare function serializeViewState(state: CompositionEditorViewStateV1): string;
|
|
65
|
+
export declare function parseViewStateEntry(raw: string | null): CompositionEditorViewStateV1 | null;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
export const COMPOSITION_DRAFT_NAMESPACE = 'composition';
|
|
2
|
+
export const STALE_DRAFT_KEY_SUFFIX = ':stale';
|
|
3
|
+
export const STALE_DRAFT_MAX_AGE_MS = 24 * 60 * 60 * 1000;
|
|
4
|
+
export const COMPOSITION_VIEW_STATE_PREFIX = 'widgetic:editor-view:composition:';
|
|
5
|
+
function stableSerialize(value) {
|
|
6
|
+
return JSON.stringify(value, (_key, inner) => {
|
|
7
|
+
if (inner && typeof inner === 'object' && !Array.isArray(inner)) {
|
|
8
|
+
const record = inner;
|
|
9
|
+
const sorted = {};
|
|
10
|
+
for (const key of Object.keys(record).sort()) {
|
|
11
|
+
sorted[key] = record[key];
|
|
12
|
+
}
|
|
13
|
+
return sorted;
|
|
14
|
+
}
|
|
15
|
+
return inner;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export function payloadsEqual(left, right) {
|
|
19
|
+
return stableSerialize(left) === stableSerialize(right);
|
|
20
|
+
}
|
|
21
|
+
/** What the server copy looks like in the editor after a fresh load (parts autosave can send). */
|
|
22
|
+
export function draftPayloadFromEditorState(state) {
|
|
23
|
+
return {
|
|
24
|
+
designValues: state.designValues,
|
|
25
|
+
contentValues: state.contentValues,
|
|
26
|
+
contentItems: state.contentItems,
|
|
27
|
+
embedWidth: state.embedWidth,
|
|
28
|
+
embedHeight: state.embedHeight
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Revision string for restore matching: content version when the API provides one, else a fingerprint of loaded values. */
|
|
32
|
+
export function serverBaseRevision(contentVersion, serverPayload) {
|
|
33
|
+
if (contentVersion !== null && Number.isFinite(contentVersion)) {
|
|
34
|
+
return `cv:${contentVersion}`;
|
|
35
|
+
}
|
|
36
|
+
return `fp:${stableSerialize(serverPayload)}`;
|
|
37
|
+
}
|
|
38
|
+
function staleStorageKey(compositionId) {
|
|
39
|
+
return `${compositionId}${STALE_DRAFT_KEY_SUFFIX}`;
|
|
40
|
+
}
|
|
41
|
+
/** Moves an unmatched draft under a `:stale` key so it can be inspected for 24 h without applying. */
|
|
42
|
+
export function preserveStaleDraft(journal, compositionId, entry, logger) {
|
|
43
|
+
journal.flushNow(staleStorageKey(compositionId), entry.payload, entry.baseRevision);
|
|
44
|
+
journal.clear(compositionId);
|
|
45
|
+
logger?.debug('[compositionDraft] Draft kept as stale; server revision changed', {
|
|
46
|
+
compositionId,
|
|
47
|
+
baseRevision: entry.baseRevision
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export function readStaleDraft(journal, compositionId, nowMs) {
|
|
51
|
+
const staleKey = staleStorageKey(compositionId);
|
|
52
|
+
const entry = journal.read(staleKey);
|
|
53
|
+
if (!entry)
|
|
54
|
+
return null;
|
|
55
|
+
if (nowMs - entry.savedAtMs > STALE_DRAFT_MAX_AGE_MS) {
|
|
56
|
+
journal.clear(staleKey);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return entry;
|
|
60
|
+
}
|
|
61
|
+
export function applyDraftRestoreRule(options) {
|
|
62
|
+
const { entry, serverRevision, serverPayload, journal, compositionId, nowMs, logger } = options;
|
|
63
|
+
if (!entry)
|
|
64
|
+
return { applied: false, discardedAsStale: false, clearedAsNoOp: false };
|
|
65
|
+
if (entry.baseRevision !== serverRevision) {
|
|
66
|
+
preserveStaleDraft(journal, compositionId, entry, logger);
|
|
67
|
+
return { applied: false, discardedAsStale: true, clearedAsNoOp: false };
|
|
68
|
+
}
|
|
69
|
+
if (payloadsEqual(entry.payload, serverPayload)) {
|
|
70
|
+
journal.clear(compositionId);
|
|
71
|
+
logger?.debug('[compositionDraft] Draft matched server; cleared without applying', { compositionId });
|
|
72
|
+
return { applied: false, discardedAsStale: false, clearedAsNoOp: true };
|
|
73
|
+
}
|
|
74
|
+
logger?.debug('[compositionDraft] Restored unsaved draft', {
|
|
75
|
+
compositionId,
|
|
76
|
+
savedAtMs: entry.savedAtMs
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
applied: true,
|
|
80
|
+
discardedAsStale: false,
|
|
81
|
+
clearedAsNoOp: false,
|
|
82
|
+
savedAtMs: entry.savedAtMs
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export function validateCompositionViewState(raw, validContentItemIds) {
|
|
86
|
+
if (!raw || typeof raw !== 'object')
|
|
87
|
+
return null;
|
|
88
|
+
const candidate = raw;
|
|
89
|
+
if (candidate.v !== 1)
|
|
90
|
+
return null;
|
|
91
|
+
const activeTab = candidate.activeTab === 'design' || candidate.activeTab === 'content' ? candidate.activeTab : 'content';
|
|
92
|
+
const deviceView = candidate.deviceView === 'mobile' ? 'mobile' : 'desktop';
|
|
93
|
+
const filterStrings = (value) => Array.isArray(value) ? value.filter((item) => typeof item === 'string') : [];
|
|
94
|
+
const openContentItemIds = filterStrings(candidate.openContentItemIds).filter((id) => validContentItemIds.has(id));
|
|
95
|
+
let activeContentItemId = null;
|
|
96
|
+
if (typeof candidate.activeContentItemId === 'string' &&
|
|
97
|
+
validContentItemIds.has(candidate.activeContentItemId)) {
|
|
98
|
+
activeContentItemId = candidate.activeContentItemId;
|
|
99
|
+
}
|
|
100
|
+
const designScrollTop = typeof candidate.designScrollTop === 'number' && Number.isFinite(candidate.designScrollTop)
|
|
101
|
+
? Math.max(0, candidate.designScrollTop)
|
|
102
|
+
: 0;
|
|
103
|
+
const contentScrollTop = typeof candidate.contentScrollTop === 'number' && Number.isFinite(candidate.contentScrollTop)
|
|
104
|
+
? Math.max(0, candidate.contentScrollTop)
|
|
105
|
+
: 0;
|
|
106
|
+
return {
|
|
107
|
+
v: 1,
|
|
108
|
+
activeTab,
|
|
109
|
+
designOpenAccordion: filterStrings(candidate.designOpenAccordion),
|
|
110
|
+
contentOpenAccordion: filterStrings(candidate.contentOpenAccordion),
|
|
111
|
+
openContentItemIds,
|
|
112
|
+
designScrollTop,
|
|
113
|
+
contentScrollTop,
|
|
114
|
+
deviceView,
|
|
115
|
+
activeContentItemId
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
export function viewStateStorageKey(compositionId) {
|
|
119
|
+
return `${COMPOSITION_VIEW_STATE_PREFIX}${compositionId}`;
|
|
120
|
+
}
|
|
121
|
+
export function serializeViewState(state) {
|
|
122
|
+
return JSON.stringify(state);
|
|
123
|
+
}
|
|
124
|
+
export function parseViewStateEntry(raw) {
|
|
125
|
+
if (!raw)
|
|
126
|
+
return null;
|
|
127
|
+
try {
|
|
128
|
+
const parsed = JSON.parse(raw);
|
|
129
|
+
if (parsed?.v !== 1)
|
|
130
|
+
return null;
|
|
131
|
+
return parsed;
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type CompositionEditorViewStateV1 } from './compositionDraft.js';
|
|
2
|
+
export interface ViewStateSessionStorage {
|
|
3
|
+
getItem(key: string): string | null;
|
|
4
|
+
setItem(key: string, value: string): void;
|
|
5
|
+
removeItem(key: string): void;
|
|
6
|
+
}
|
|
7
|
+
export declare function readCompositionEditorViewState(compositionId: string, validContentItemIds: ReadonlySet<string>, storage?: ViewStateSessionStorage | null): CompositionEditorViewStateV1 | null;
|
|
8
|
+
export declare function writeCompositionEditorViewState(compositionId: string, state: CompositionEditorViewStateV1, storage?: ViewStateSessionStorage | null): boolean;
|
|
9
|
+
export type { CompositionEditorViewStateV1 };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { parseViewStateEntry, serializeViewState, validateCompositionViewState, viewStateStorageKey } from './compositionDraft.js';
|
|
2
|
+
export function readCompositionEditorViewState(compositionId, validContentItemIds, storage) {
|
|
3
|
+
const session = storage ?? getDefaultSessionStorage();
|
|
4
|
+
if (!session)
|
|
5
|
+
return null;
|
|
6
|
+
try {
|
|
7
|
+
const raw = session.getItem(viewStateStorageKey(compositionId));
|
|
8
|
+
const parsed = parseViewStateEntry(raw);
|
|
9
|
+
if (!parsed)
|
|
10
|
+
return null;
|
|
11
|
+
return validateCompositionViewState(parsed, validContentItemIds);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function writeCompositionEditorViewState(compositionId, state, storage) {
|
|
18
|
+
const session = storage ?? getDefaultSessionStorage();
|
|
19
|
+
if (!session)
|
|
20
|
+
return false;
|
|
21
|
+
try {
|
|
22
|
+
session.setItem(viewStateStorageKey(compositionId), serializeViewState(state));
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function getDefaultSessionStorage() {
|
|
30
|
+
if (typeof sessionStorage === 'undefined')
|
|
31
|
+
return null;
|
|
32
|
+
return sessionStorage;
|
|
33
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** Refresh-safe draft journal (localStorage). Copied per package; see QoL persistence spec. */
|
|
2
|
+
export declare const DRAFT_STORAGE_PREFIX = "widgetic:draft:";
|
|
3
|
+
export declare const DRAFT_FORMAT_VERSION = 1;
|
|
4
|
+
export declare const DEFAULT_DRAFT_MAX_BYTES: number;
|
|
5
|
+
export declare const DEFAULT_DRAFT_MAX_AGE_MS: number;
|
|
6
|
+
export declare const DRAFT_WRITE_THROTTLE_MS = 250;
|
|
7
|
+
export interface DraftJournalEntry<T> {
|
|
8
|
+
v: number;
|
|
9
|
+
savedAtMs: number;
|
|
10
|
+
baseRevision: string | null;
|
|
11
|
+
payload: T;
|
|
12
|
+
}
|
|
13
|
+
export interface DraftJournalStorage {
|
|
14
|
+
getItem(key: string): string | null;
|
|
15
|
+
setItem(key: string, value: string): void;
|
|
16
|
+
removeItem(key: string): void;
|
|
17
|
+
length: number;
|
|
18
|
+
key(index: number): string | null;
|
|
19
|
+
}
|
|
20
|
+
export interface DraftJournalClock {
|
|
21
|
+
now(): number;
|
|
22
|
+
}
|
|
23
|
+
export interface DraftJournalLogger {
|
|
24
|
+
debug(message: string, detail?: unknown): void;
|
|
25
|
+
}
|
|
26
|
+
export interface DraftJournalTimers {
|
|
27
|
+
setTimeout(callback: () => void, milliseconds: number): unknown;
|
|
28
|
+
clearTimeout(handle: unknown): void;
|
|
29
|
+
}
|
|
30
|
+
/** Minimal window/document surface for leave flushing (tests inject fakes). */
|
|
31
|
+
export interface DraftJournalWindow {
|
|
32
|
+
addEventListener(type: string, listener: () => void): void;
|
|
33
|
+
removeEventListener(type: string, listener: () => void): void;
|
|
34
|
+
}
|
|
35
|
+
export interface DraftJournalDocument {
|
|
36
|
+
addEventListener(type: string, listener: () => void): void;
|
|
37
|
+
removeEventListener(type: string, listener: () => void): void;
|
|
38
|
+
readonly visibilityState: DocumentVisibilityState | string;
|
|
39
|
+
}
|
|
40
|
+
export interface DraftJournalOptions {
|
|
41
|
+
storage?: DraftJournalStorage;
|
|
42
|
+
clock?: DraftJournalClock;
|
|
43
|
+
logger?: DraftJournalLogger;
|
|
44
|
+
timers?: DraftJournalTimers;
|
|
45
|
+
window?: DraftJournalWindow | null;
|
|
46
|
+
document?: DraftJournalDocument | null;
|
|
47
|
+
maxBytes?: number;
|
|
48
|
+
maxAgeMs?: number;
|
|
49
|
+
throttleMs?: number;
|
|
50
|
+
}
|
|
51
|
+
export interface DraftJournal<T> {
|
|
52
|
+
read(key: string): DraftJournalEntry<T> | null;
|
|
53
|
+
write(key: string, payload: T, baseRevision?: string | null): boolean;
|
|
54
|
+
flushNow(key: string, payload: T, baseRevision?: string | null): boolean;
|
|
55
|
+
clear(key: string): void;
|
|
56
|
+
purgeExpired(): number;
|
|
57
|
+
installFlushOnLeave(flushNow: () => void): () => void;
|
|
58
|
+
}
|
|
59
|
+
export declare function createDraftJournal<T>(namespace: string, options?: DraftJournalOptions): DraftJournal<T>;
|