@pixerate/editor-svelte 0.8.0 → 0.9.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/dist/index.d.ts +70 -7
- package/dist/index.js +327 -206
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/EditableTextNodeEditor.svelte +4 -1
- package/src/createNavigationGuard.svelte.ts +97 -0
- package/src/index.d.ts +70 -7
- package/src/index.ts +23 -4
- package/src/navigationGuard.ts +82 -0
package/src/index.ts
CHANGED
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
import type { Component } from "svelte";
|
|
2
2
|
import type { Editor, Content, Extensions, FocusPosition } from "@tiptap/core";
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
Template,
|
|
5
|
+
ColorGradient,
|
|
6
|
+
RichTextPresetOptions,
|
|
7
|
+
MarkdownOptions,
|
|
8
|
+
ImageOptions,
|
|
9
|
+
} from "@pixerate/editor";
|
|
4
10
|
|
|
5
11
|
import EditableTextNodeEditorComponent from "./EditableTextNodeEditor.svelte";
|
|
6
12
|
import BubbleMenuComponent from "./BubbleMenu.svelte";
|
|
7
13
|
import TemplateRendererComponent from "./TemplateRenderer.svelte";
|
|
8
14
|
|
|
9
15
|
export * from "./editor.svelte";
|
|
16
|
+
export * from "./navigationGuard";
|
|
17
|
+
export * from "./createNavigationGuard.svelte";
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
DirtyTracker,
|
|
21
|
+
createDirtyTracker,
|
|
22
|
+
defaultIsEqual,
|
|
23
|
+
} from "@pixerate/editor";
|
|
24
|
+
export type { DirtyTrackerOptions } from "@pixerate/editor";
|
|
10
25
|
|
|
11
26
|
export interface EditableTextNodeEditorProps {
|
|
12
27
|
class?: string;
|
|
@@ -26,6 +41,7 @@ export interface EditableTextNodeEditorProps {
|
|
|
26
41
|
autofocus?: FocusPosition | boolean;
|
|
27
42
|
image?: ImageOptions | boolean;
|
|
28
43
|
uploadImage?: (file: File) => Promise<string> | string;
|
|
44
|
+
onUploadError?: (error: Error, file: File) => void;
|
|
29
45
|
}
|
|
30
46
|
|
|
31
47
|
export interface BubbleMenuProps {
|
|
@@ -44,9 +60,12 @@ export interface TemplateRendererProps {
|
|
|
44
60
|
onVariableClick?: (variableName: string) => void;
|
|
45
61
|
}
|
|
46
62
|
|
|
47
|
-
export const EditableTextNodeEditor =
|
|
48
|
-
|
|
49
|
-
export const
|
|
63
|
+
export const EditableTextNodeEditor =
|
|
64
|
+
EditableTextNodeEditorComponent as unknown as Component<EditableTextNodeEditorProps>;
|
|
65
|
+
export const BubbleMenu =
|
|
66
|
+
BubbleMenuComponent as unknown as Component<BubbleMenuProps>;
|
|
67
|
+
export const TemplateRenderer =
|
|
68
|
+
TemplateRendererComponent as unknown as Component<TemplateRendererProps>;
|
|
50
69
|
|
|
51
70
|
export type {
|
|
52
71
|
Template,
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export interface NavigationGuardOptions {
|
|
2
|
+
/** Whether the current form or editor has unsaved changes */
|
|
3
|
+
isDirty: boolean;
|
|
4
|
+
/** Whether guarding is enabled (defaults to true) */
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
/** Callback fired when an internal navigation attempt is intercepted */
|
|
7
|
+
onBlocked?: (url: string) => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface NavigationGuardActionReturn {
|
|
11
|
+
update(newOptions: NavigationGuardOptions): void;
|
|
12
|
+
destroy(): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Svelte action to guard against navigation when there are unsaved changes.
|
|
17
|
+
* Attaches beforeunload to window and intercepts internal anchor clicks on the element.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* <div use:navigationGuard={{ isDirty, onBlocked: (url) => { pendingUrl = url; showConfirm = true; } }}>
|
|
21
|
+
*/
|
|
22
|
+
export function navigationGuard(
|
|
23
|
+
node: HTMLElement,
|
|
24
|
+
options: NavigationGuardOptions,
|
|
25
|
+
): NavigationGuardActionReturn {
|
|
26
|
+
let isDirty = options.isDirty;
|
|
27
|
+
let enabled = options.enabled ?? true;
|
|
28
|
+
let onBlocked = options.onBlocked;
|
|
29
|
+
|
|
30
|
+
function handleBeforeUnload(e: BeforeUnloadEvent) {
|
|
31
|
+
if (!enabled || !isDirty) return;
|
|
32
|
+
e.preventDefault();
|
|
33
|
+
e.returnValue = "";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function handleClick(e: MouseEvent) {
|
|
37
|
+
if (!enabled || !isDirty) return;
|
|
38
|
+
|
|
39
|
+
const target = e.target as HTMLElement | null;
|
|
40
|
+
const anchor = target?.closest?.("a");
|
|
41
|
+
if (!anchor) return;
|
|
42
|
+
if (anchor.target === "_blank") return;
|
|
43
|
+
|
|
44
|
+
// Check if link is internal
|
|
45
|
+
if (typeof window !== "undefined") {
|
|
46
|
+
const isInternal = anchor.host === window.location.host;
|
|
47
|
+
if (!isInternal) return;
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
anchor.pathname === window.location.pathname &&
|
|
51
|
+
anchor.search === window.location.search
|
|
52
|
+
) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
e.preventDefault();
|
|
58
|
+
e.stopPropagation();
|
|
59
|
+
|
|
60
|
+
const href = anchor.href;
|
|
61
|
+
onBlocked?.(href);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (typeof window !== "undefined") {
|
|
65
|
+
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
66
|
+
}
|
|
67
|
+
node.addEventListener("click", handleClick, { capture: true });
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
update(newOptions: NavigationGuardOptions) {
|
|
71
|
+
isDirty = newOptions.isDirty;
|
|
72
|
+
enabled = newOptions.enabled ?? true;
|
|
73
|
+
onBlocked = newOptions.onBlocked;
|
|
74
|
+
},
|
|
75
|
+
destroy() {
|
|
76
|
+
if (typeof window !== "undefined") {
|
|
77
|
+
window.removeEventListener("beforeunload", handleBeforeUnload);
|
|
78
|
+
}
|
|
79
|
+
node.removeEventListener("click", handleClick, { capture: true });
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|