@toantranmei/synced-block 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 toantranmei
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,77 @@
1
- # Temporary Holding Version
1
+ # @toantranmei/synced-block
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Notion-style synced blocks for [`@toantranmei/editor`](https://www.npmjs.com/package/@toantranmei/editor): the
4
+ same content on many pages, edited anywhere, updated everywhere.
5
+
6
+ - A `syncedBlock` node stores only `{ sourceId }`; its content is a separate Yjs document that your app provides
7
+ - Shown with a nested `<MeiEditor>` in collaboration mode, inside a frame that appears on hover / focus
8
+ ("Synced block" label + "Edit original" link)
9
+ - Slash item **Synced block** moves the current block(s) into a new source (an empty line gives an empty one)
10
+ - Read-only when the page or the source isn't editable; "not found / no access" state
11
+ - Server-side HTML through `renderHTML`, with the source content you pass in
12
+ - Vietnamese, English and Simplified Chinese, styled with the editor's tokens (`component.synced.*`)
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ npm i @toantranmei/synced-block yjs
18
+ ```
19
+
20
+ Peers: `@toantranmei/editor` `^0.3.0`, `yjs` `^13.6` (one copy in the app), `vue` `^3.5`, Tiptap `3.32.0`, `@lucide/vue`.
21
+
22
+ ## Usage
23
+
24
+ ```ts
25
+ import { defaultBlocks, mention } from '@toantranmei/editor'
26
+ import { syncedBlock } from '@toantranmei/synced-block'
27
+ import '@toantranmei/synced-block/style.css'
28
+
29
+ const base = [...defaultBlocks, mention]
30
+ const blocks = [
31
+ ...base,
32
+ syncedBlock.configure({
33
+ // One cached Y.Doc per source (doc id = sourceId), connected to your sync provider.
34
+ doc: async sourceId => ({ doc: await docs.open(sourceId), editable: await canEdit(sourceId) }), // null: missing / no access
35
+ create: () => api.createResource({ type: 'synced_block', parent: pageId }).then(r => r.id),
36
+ originHref: sourceId => `/notes/${ownerPageOf(sourceId)}`,
37
+ blocks: base, // the nested editor's blocks (synced blocks can't nest)
38
+ }),
39
+ ]
40
+ ```
41
+
42
+ Copying a synced block and pasting it on another page shows the same source there (the node only holds
43
+ `sourceId`); `editor.commands.insertSyncedBlock(sourceId)` does the same from your UI.
44
+ `createSyncedBlock()` is the slash item's command.
45
+
46
+ ### Options
47
+
48
+ | Option | Description |
49
+ | --- | --- |
50
+ | `doc(sourceId)` | `{ doc: Y.Doc, editable: boolean, field?: string }` (or a promise), `null` when missing / no access |
51
+ | `create()` | Creates a source, resolves to its id. Without it the slash item is hidden |
52
+ | `originHref(sourceId)` | "Edit original" link (hidden when not given or `null`) |
53
+ | `blocks` | Blocks of the nested editor (default `defaultBlocks`) |
54
+ | `onError` | `create` / `doc` failures (default `console.error`) |
55
+ | `messages` | `{ en: { label: 'Synced' } }` |
56
+
57
+ ### Server-side HTML
58
+
59
+ ```ts
60
+ import { renderHTML } from '@toantranmei/editor/render'
61
+ import { syncedBlockRender } from '@toantranmei/synced-block/render' // DOM-free
62
+
63
+ renderHTML(doc, { blocks: [syncedBlockRender], data: { syncedBlocks: { [sourceId]: sourceJson } } })
64
+ ```
65
+
66
+ Sources without content in `data.syncedBlocks` render nothing. `documentToJSON(ydoc)` from
67
+ `@toantranmei/editor/collaboration` gives a source's JSON on a Node server.
68
+
69
+ ### Styling
70
+
71
+ Tokens under `component.synced` (`border`, `borderWidth`, `radius`, `padding`, `label*`, `hoverBorder`,
72
+ `emptyColor`, `gap`). Classes: `.me-synced` (`.is-selected`, `.is-readonly`, `.is-missing`), `.me-synced-bar`,
73
+ `.me-synced-content`.
74
+
75
+ ## License
76
+
77
+ MIT
package/dist/index.js ADDED
@@ -0,0 +1,234 @@
1
+ import { n as e, r as t, t as n } from "./render-CFO6ghxH.js";
2
+ import { Repeat2 as r } from "@lucide/vue";
3
+ import { MeiEditor as i, blockNodeView as a, currentRange as o, defaultBlocks as s, defineBlock as c, useEditable as l } from "@toantranmei/editor";
4
+ import { Node as u, mergeAttributes as d } from "@tiptap/core";
5
+ import { Fragment as f, computed as p, createBlock as m, createCommentVNode as h, createElementBlock as g, createElementVNode as _, createTextVNode as v, createVNode as y, defineComponent as b, normalizeClass as x, openBlock as S, shallowRef as C, toDisplayString as w, unref as T, watch as E, withCtx as D } from "vue";
6
+ import { NodeViewWrapper as O, nodeViewProps as k } from "@tiptap/vue-3";
7
+ // src/messages.ts
8
+ var A = {
9
+ vi: {
10
+ label: "Khối đồng bộ",
11
+ original: "Mở bản gốc",
12
+ loading: "Đang tải khối đồng bộ…",
13
+ unavailable: "Không tìm thấy khối đồng bộ hoặc bạn không có quyền truy cập",
14
+ readOnly: "Chỉ xem"
15
+ },
16
+ en: {
17
+ label: "Synced block",
18
+ original: "Edit original",
19
+ loading: "Loading synced block…",
20
+ unavailable: "Synced block not found or no access",
21
+ readOnly: "View only"
22
+ },
23
+ zh: {
24
+ label: "同步块",
25
+ original: "编辑原始块",
26
+ loading: "正在加载同步块…",
27
+ unavailable: "同步块不存在或无访问权限",
28
+ readOnly: "仅查看"
29
+ }
30
+ };
31
+ function j(e, t) {
32
+ let n = e && e in A ? e : "vi";
33
+ return {
34
+ ...A[n],
35
+ ...t?.[n]
36
+ };
37
+ }
38
+ // src/SyncedBlockView.vue?vue&type=script&setup=true&lang.ts
39
+ var M = {
40
+ class: "me-synced-bar",
41
+ contenteditable: "false"
42
+ }, N = { class: "me-synced-label" }, P = ["href"], F = { class: "me-synced-content" }, I = {
43
+ key: 0,
44
+ class: "me-hint me-synced-empty"
45
+ }, L = {
46
+ key: 1,
47
+ class: "me-hint me-synced-empty"
48
+ }, R = /* @__PURE__ */ b({
49
+ __name: "SyncedBlockView",
50
+ props: k,
51
+ setup(e) {
52
+ let t = e, a = p(() => t.extension.options), o = p(() => t.node.attrs.sourceId || null), c = p(() => t.editor.storage.meiConfig?.locale ?? "vi"), u = p(() => j(c.value, a.value.messages)), d = l(t.editor), b = C({ status: "loading" });
53
+ E(o, async (e) => {
54
+ let t = a.value.doc;
55
+ if (!e || !t) b.value = { status: "missing" };
56
+ else {
57
+ b.value = { status: "loading" };
58
+ try {
59
+ let n = await t(e);
60
+ if (o.value !== e) return;
61
+ b.value = n ? {
62
+ status: "ok",
63
+ source: n
64
+ } : { status: "missing" };
65
+ } catch (t) {
66
+ if (o.value !== e) return;
67
+ b.value = { status: "missing" }, B(a.value, t);
68
+ }
69
+ }
70
+ }, { immediate: !0 });
71
+ let k = (a.value.blocks ?? s).filter((e) => !e.nodes.includes(n)), A = p(() => b.value.status === "ok" ? b.value.source : null), R = p(() => d.value && !!A.value?.editable), z = p(() => o.value ? a.value.originHref?.(o.value) ?? null : null);
72
+ return (e, t) => (S(), m(T(O), {
73
+ class: x(["me-synced", {
74
+ "is-selected": e.selected,
75
+ "is-readonly": A.value && !R.value,
76
+ "is-missing": b.value.status === "missing"
77
+ }]),
78
+ "data-type": "synced-block",
79
+ "data-source-id": o.value ?? void 0
80
+ }, {
81
+ default: D(() => [_("div", M, [_("span", N, [
82
+ y(T(r)),
83
+ v(" " + w(u.value.label), 1),
84
+ A.value && !R.value ? (S(), g(f, { key: 0 }, [v(" · " + w(u.value.readOnly), 1)], 64)) : h("", !0)
85
+ ]), z.value ? (S(), g("a", {
86
+ key: 0,
87
+ class: "me-synced-origin",
88
+ href: z.value,
89
+ target: "_blank",
90
+ rel: "noopener"
91
+ }, w(u.value.original), 9, P)) : h("", !0)]), _("div", F, [b.value.status === "loading" ? (S(), g("p", I, w(u.value.loading), 1)) : b.value.status === "missing" ? (S(), g("p", L, w(u.value.unavailable), 1)) : A.value ? (S(), m(T(i), {
92
+ key: o.value ?? "",
93
+ collaboration: {
94
+ document: A.value.doc,
95
+ field: A.value.field
96
+ },
97
+ editable: R.value,
98
+ blocks: T(k),
99
+ locale: c.value,
100
+ outline: !1
101
+ }, null, 8, [
102
+ "collaboration",
103
+ "editable",
104
+ "blocks",
105
+ "locale"
106
+ ])) : h("", !0)])]),
107
+ _: 1
108
+ }, 8, ["class", "data-source-id"]));
109
+ }
110
+ }), z = {
111
+ doc: null,
112
+ create: null,
113
+ originHref: null,
114
+ blocks: null,
115
+ onError: null,
116
+ messages: null
117
+ }, B = (e, t) => e.onError ? e.onError(t) : console.error("[synced-block]", t);
118
+ function V(e, t) {
119
+ let n = e.utils.createMappablePosition(t), r = ({ transaction: t }) => {
120
+ n = e.utils.getUpdatedPosition(n, t).position;
121
+ };
122
+ return e.on("transaction", r), {
123
+ get: () => n.position,
124
+ stop: () => e.off("transaction", r)
125
+ };
126
+ }
127
+ var H = (e) => e.length === 1 && e[0].type === "paragraph" && !e[0].content?.length, U = u.create({
128
+ name: n,
129
+ group: "block",
130
+ atom: !0,
131
+ draggable: !0,
132
+ selectable: !0,
133
+ isolating: !0,
134
+ addOptions: () => ({ ...z }),
135
+ addAttributes() {
136
+ return { sourceId: {
137
+ default: null,
138
+ parseHTML: (e) => e.getAttribute("data-source-id"),
139
+ renderHTML: (e) => e.sourceId ? { "data-source-id": e.sourceId } : {}
140
+ } };
141
+ },
142
+ parseHTML() {
143
+ return [{ tag: "div[data-type=\"synced-block\"]" }];
144
+ },
145
+ renderHTML({ HTMLAttributes: e }) {
146
+ return ["div", d({
147
+ "data-type": "synced-block",
148
+ class: "me-synced"
149
+ }, e)];
150
+ },
151
+ addNodeView() {
152
+ let e = a(R);
153
+ return (t) => {
154
+ let n = e(t), r = n.stopEvent?.bind(n);
155
+ return n.stopEvent = (e) => e.target?.closest?.(".me-synced-content") ? !0 : r ? r(e) : !1, n.ignoreMutation = () => !0, n;
156
+ };
157
+ },
158
+ addCommands() {
159
+ return {
160
+ insertSyncedBlock: (e) => ({ commands: t }) => t.insertContent({
161
+ type: this.name,
162
+ attrs: { sourceId: e }
163
+ }),
164
+ createSyncedBlock: () => ({ editor: e }) => {
165
+ let { create: t, doc: r } = this.options;
166
+ if (!t || !r) return !1;
167
+ let i = o(e.state);
168
+ if (!i) return !1;
169
+ let a = e.state.doc.slice(i.from, i.to).content.toJSON() ?? [], s = {
170
+ type: "doc",
171
+ content: a.length && !H(a) ? a : [{ type: "paragraph" }]
172
+ }, c = V(e, i.from), l = V(e, i.to), u = () => {
173
+ c.stop(), l.stop();
174
+ };
175
+ return (async () => {
176
+ let i = await t(), a = await r(i);
177
+ if (!a) throw Error(`synced-block: no document for new source ${i}`);
178
+ let { fillDocument: o } = await import("@toantranmei/editor/collaboration");
179
+ if (o(a.doc, s, e.schema, a.field), u(), e.isDestroyed) return;
180
+ let d = e.schema.nodes[n].create({ sourceId: i }), f = c.get(), p = l.get(), m = e.state.tr;
181
+ f < p && p <= m.doc.content.size ? m.replaceWith(f, p, d) : m.insert(Math.min(f, m.doc.content.size), d), e.view.dispatch(m.scrollIntoView());
182
+ })().catch((e) => {
183
+ u(), B(this.options, e);
184
+ }), !0;
185
+ }
186
+ };
187
+ }
188
+ }), W = c({
189
+ name: "syncedBlock",
190
+ defaults: {
191
+ doc: null,
192
+ create: null,
193
+ originHref: null,
194
+ blocks: null,
195
+ onError: null,
196
+ messages: null
197
+ },
198
+ nodes: [n],
199
+ extensions: (e) => [U.configure(e)],
200
+ items: (e) => e.create ? [{
201
+ id: "syncedBlock",
202
+ icon: r,
203
+ group: "advanced",
204
+ keywords: [
205
+ "synced",
206
+ "sync",
207
+ "synced block",
208
+ "mirror",
209
+ "dong bo",
210
+ "khoi dong bo",
211
+ "同步",
212
+ "同步块"
213
+ ],
214
+ insert: (e) => e.createSyncedBlock()
215
+ }] : [],
216
+ render: () => t(),
217
+ messages: {
218
+ vi: { blocks: { syncedBlock: {
219
+ title: "Khối đồng bộ",
220
+ description: "Nội dung giống nhau ở nhiều trang, sửa một nơi cập nhật mọi nơi."
221
+ } } },
222
+ en: { blocks: { syncedBlock: {
223
+ title: "Synced block",
224
+ description: "Same content on many pages: edit once, updates everywhere."
225
+ } } },
226
+ zh: { blocks: { syncedBlock: {
227
+ title: "同步块",
228
+ description: "多个页面共享同一内容,一处编辑处处更新。"
229
+ } } }
230
+ }
231
+ });
232
+ export { n as SYNCED_NODE, U as SyncedBlockNode, A as messages, W as syncedBlock, e as syncedBlockRender, t as syncedBlockRenderers, j as syncedMessages };
233
+
234
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["selected","_openBlock","_createBlock","_unref","_normalizeClass","_withCtx","_createElementVNode","_createVNode","_createTextVNode","_toDisplayString","_createElementBlock","_Fragment","_createCommentVNode","_sfc_main","SyncedBlockView","syncedBlockRender","syncedBlockRenderers","SYNCED_NODE","messages","syncedMessages","key","locale","overrides","_hoisted_1","_hoisted_2","_hoisted_3","_hoisted_4","_hoisted_5","_hoisted_6","SyncedBlockView_default","defineComponent","nodeViewProps","props","__props","options","computed","sourceId","m","outerEditable","useEditable","state","shallowRef","resolve","id","source","error","reportError","blocks","defaultBlocks","b","editable","href","openBlock","createBlock","unref","NodeViewWrapper","normalizeClass","_ctx","withCtx","createElementVNode","createVNode","Repeat2","createTextVNode","toDisplayString","createElementBlock","Fragment","createCommentVNode","MeiEditor","defaults","track","position","editor","pos","onTransaction","transaction","isEmptyLine","SyncedBlockNode","Node","el","attrs","mergeAttributes","\"class\"","HTMLAttributes","render","blockNodeView","view","stop","event","commands","create","doc","range","currentRange","content","from","to","fillDocument","node","a","tr","syncedBlock","defineBlock","o","c"],"sources":["../src/messages.ts","../src/SyncedBlockView.vue","../src/SyncedBlockView.vue","../src/node.ts","../src/definition.ts"],"sourcesContent":["import type { LocaleName } from '@toantranmei/editor'\n\nexport interface SyncedMessages {\n /** Frame label. */\n label: string\n /** Link to the source's page. */\n original: string\n loading: string\n /** `doc` returned null or failed. */\n unavailable: string\n readOnly: string\n}\n\nexport const messages: Record<LocaleName, SyncedMessages> = {\n vi: { label: 'Khối đồng bộ', original: 'Mở bản gốc', loading: 'Đang tải khối đồng bộ…', unavailable: 'Không tìm thấy khối đồng bộ hoặc bạn không có quyền truy cập', readOnly: 'Chỉ xem' },\n en: { label: 'Synced block', original: 'Edit original', loading: 'Loading synced block…', unavailable: 'Synced block not found or no access', readOnly: 'View only' },\n zh: { label: '同步块', original: '编辑原始块', loading: '正在加载同步块…', unavailable: '同步块不存在或无访问权限', readOnly: '仅查看' },\n}\n\nexport function syncedMessages(locale: string | undefined, overrides?: Partial<Record<LocaleName, Partial<SyncedMessages>>> | null): SyncedMessages {\n const key = (locale && locale in messages ? locale : 'vi') as LocaleName\n return { ...messages[key], ...overrides?.[key] }\n}\n","<script setup lang=\"ts\">\n/**\n * A synced block: a nested `<MeiEditor>` on the source's Y.Doc (collaboration mode), so\n * every page showing the source edits the same content. The frame shows on hover / focus.\n */\nimport { Repeat2 } from '@lucide/vue'\nimport { NodeViewWrapper, nodeViewProps } from '@tiptap/vue-3'\nimport { defaultBlocks, MeiEditor, useEditable } from '@toantranmei/editor'\nimport { computed, shallowRef, watch } from 'vue'\nimport { syncedMessages } from './messages'\nimport { reportError } from './node'\nimport { SYNCED_NODE } from './render'\nimport type { SyncedBlockOptions, SyncedSource } from './types'\n\nconst props = defineProps(nodeViewProps)\n\nconst options = computed(() => props.extension.options as SyncedBlockOptions)\nconst sourceId = computed(() => (props.node.attrs.sourceId as string | null) || null)\nconst locale = computed(() => props.editor.storage.meiConfig?.locale ?? 'vi')\nconst m = computed(() => syncedMessages(locale.value, options.value.messages))\nconst outerEditable = useEditable(props.editor)\n\ntype State = { status: 'loading' } | { status: 'missing' } | { status: 'ok', source: SyncedSource }\nconst state = shallowRef<State>({ status: 'loading' })\n\nwatch(sourceId, async (id) => {\n const resolve = options.value.doc\n if (!id || !resolve) {\n state.value = { status: 'missing' }\n return\n }\n state.value = { status: 'loading' }\n try {\n const source = await resolve(id)\n if (sourceId.value !== id) return\n state.value = source ? { status: 'ok', source } : { status: 'missing' }\n }\n catch (error) {\n if (sourceId.value !== id) return\n state.value = { status: 'missing' }\n reportError(options.value, error)\n }\n}, { immediate: true })\n\n// Read once, like <MeiEditor :blocks>. Synced blocks can't nest (a source could show itself).\nconst blocks = (options.value.blocks ?? defaultBlocks).filter(b => !b.nodes.includes(SYNCED_NODE))\nconst source = computed(() => (state.value.status === 'ok' ? state.value.source : null))\nconst editable = computed(() => outerEditable.value && !!source.value?.editable)\nconst href = computed(() => (sourceId.value ? options.value.originHref?.(sourceId.value) ?? null : null))\n</script>\n\n<template>\n <NodeViewWrapper\n class=\"me-synced\"\n :class=\"{ 'is-selected': selected, 'is-readonly': source && !editable, 'is-missing': state.status === 'missing' }\"\n data-type=\"synced-block\"\n :data-source-id=\"sourceId ?? undefined\"\n >\n <div class=\"me-synced-bar\" contenteditable=\"false\">\n <span class=\"me-synced-label\"><Repeat2 /> {{ m.label }}<template v-if=\"source && !editable\"> · {{ m.readOnly }}</template></span>\n <a v-if=\"href\" class=\"me-synced-origin\" :href=\"href\" target=\"_blank\" rel=\"noopener\">{{ m.original }}</a>\n </div>\n <div class=\"me-synced-content\">\n <p v-if=\"state.status === 'loading'\" class=\"me-hint me-synced-empty\">\n {{ m.loading }}\n </p>\n <p v-else-if=\"state.status === 'missing'\" class=\"me-hint me-synced-empty\">\n {{ m.unavailable }}\n </p>\n <MeiEditor\n v-else-if=\"source\"\n :key=\"sourceId ?? ''\"\n :collaboration=\"{ document: source.doc, field: source.field }\"\n :editable=\"editable\"\n :blocks=\"blocks\"\n :locale=\"locale\"\n :outline=\"false\"\n />\n </div>\n </NodeViewWrapper>\n</template>\n","<script setup lang=\"ts\">\n/**\n * A synced block: a nested `<MeiEditor>` on the source's Y.Doc (collaboration mode), so\n * every page showing the source edits the same content. The frame shows on hover / focus.\n */\nimport { Repeat2 } from '@lucide/vue'\nimport { NodeViewWrapper, nodeViewProps } from '@tiptap/vue-3'\nimport { defaultBlocks, MeiEditor, useEditable } from '@toantranmei/editor'\nimport { computed, shallowRef, watch } from 'vue'\nimport { syncedMessages } from './messages'\nimport { reportError } from './node'\nimport { SYNCED_NODE } from './render'\nimport type { SyncedBlockOptions, SyncedSource } from './types'\n\nconst props = defineProps(nodeViewProps)\n\nconst options = computed(() => props.extension.options as SyncedBlockOptions)\nconst sourceId = computed(() => (props.node.attrs.sourceId as string | null) || null)\nconst locale = computed(() => props.editor.storage.meiConfig?.locale ?? 'vi')\nconst m = computed(() => syncedMessages(locale.value, options.value.messages))\nconst outerEditable = useEditable(props.editor)\n\ntype State = { status: 'loading' } | { status: 'missing' } | { status: 'ok', source: SyncedSource }\nconst state = shallowRef<State>({ status: 'loading' })\n\nwatch(sourceId, async (id) => {\n const resolve = options.value.doc\n if (!id || !resolve) {\n state.value = { status: 'missing' }\n return\n }\n state.value = { status: 'loading' }\n try {\n const source = await resolve(id)\n if (sourceId.value !== id) return\n state.value = source ? { status: 'ok', source } : { status: 'missing' }\n }\n catch (error) {\n if (sourceId.value !== id) return\n state.value = { status: 'missing' }\n reportError(options.value, error)\n }\n}, { immediate: true })\n\n// Read once, like <MeiEditor :blocks>. Synced blocks can't nest (a source could show itself).\nconst blocks = (options.value.blocks ?? defaultBlocks).filter(b => !b.nodes.includes(SYNCED_NODE))\nconst source = computed(() => (state.value.status === 'ok' ? state.value.source : null))\nconst editable = computed(() => outerEditable.value && !!source.value?.editable)\nconst href = computed(() => (sourceId.value ? options.value.originHref?.(sourceId.value) ?? null : null))\n</script>\n\n<template>\n <NodeViewWrapper\n class=\"me-synced\"\n :class=\"{ 'is-selected': selected, 'is-readonly': source && !editable, 'is-missing': state.status === 'missing' }\"\n data-type=\"synced-block\"\n :data-source-id=\"sourceId ?? undefined\"\n >\n <div class=\"me-synced-bar\" contenteditable=\"false\">\n <span class=\"me-synced-label\"><Repeat2 /> {{ m.label }}<template v-if=\"source && !editable\"> · {{ m.readOnly }}</template></span>\n <a v-if=\"href\" class=\"me-synced-origin\" :href=\"href\" target=\"_blank\" rel=\"noopener\">{{ m.original }}</a>\n </div>\n <div class=\"me-synced-content\">\n <p v-if=\"state.status === 'loading'\" class=\"me-hint me-synced-empty\">\n {{ m.loading }}\n </p>\n <p v-else-if=\"state.status === 'missing'\" class=\"me-hint me-synced-empty\">\n {{ m.unavailable }}\n </p>\n <MeiEditor\n v-else-if=\"source\"\n :key=\"sourceId ?? ''\"\n :collaboration=\"{ document: source.doc, field: source.field }\"\n :editable=\"editable\"\n :blocks=\"blocks\"\n :locale=\"locale\"\n :outline=\"false\"\n />\n </div>\n </NodeViewWrapper>\n</template>\n","import { type Editor, mergeAttributes, Node } from '@tiptap/core'\nimport { blockNodeView, currentRange, type JSONContent } from '@toantranmei/editor'\nimport { SYNCED_NODE } from './render'\nimport type { SyncedBlockOptions } from './types'\nimport SyncedBlockView from './SyncedBlockView.vue'\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n syncedBlock: {\n /** Inserts a synced block showing an existing source (e.g. \"Paste and sync\"). */\n insertSyncedBlock: (sourceId: string) => ReturnType\n /**\n * \"Synced block\": creates a source (`create`), moves the selected blocks into it (an\n * empty line gives an empty source) and puts a synced block in their place.\n */\n createSyncedBlock: () => ReturnType\n }\n }\n}\n\nconst defaults: SyncedBlockOptions = { doc: null, create: null, originHref: null, blocks: null, onError: null, messages: null }\n\nexport const reportError = (options: SyncedBlockOptions, error: unknown) => (options.onError ? options.onError(error) : console.error('[synced-block]', error))\n\n/** A position that follows the document (Yjs-aware through the editor's mappable positions). */\nfunction track(editor: Editor, pos: number) {\n let position = editor.utils.createMappablePosition(pos)\n const onTransaction = ({ transaction }: { transaction: Parameters<Editor['utils']['getUpdatedPosition']>[1] }) => {\n position = editor.utils.getUpdatedPosition(position, transaction).position\n }\n editor.on('transaction', onTransaction)\n return { get: () => position.position, stop: () => editor.off('transaction', onTransaction) }\n}\n\nconst isEmptyLine = (blocks: JSONContent[]) => blocks.length === 1 && blocks[0]!.type === 'paragraph' && !blocks[0]!.content?.length\n\n/**\n * Synced block: shows (and edits) a separate Yjs document, so the same content appears on\n * many pages. The node stores only `sourceId`.\n */\nexport const SyncedBlockNode = Node.create<SyncedBlockOptions>({\n name: SYNCED_NODE,\n group: 'block',\n atom: true,\n draggable: true,\n selectable: true,\n isolating: true,\n\n addOptions: () => ({ ...defaults }),\n\n addAttributes() {\n return {\n sourceId: {\n default: null,\n parseHTML: (el: HTMLElement) => el.getAttribute('data-source-id'),\n renderHTML: (attrs: Record<string, any>) => (attrs.sourceId ? { 'data-source-id': attrs.sourceId } : {}),\n },\n }\n },\n\n parseHTML() {\n return [{ tag: 'div[data-type=\"synced-block\"]' }]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['div', mergeAttributes({ 'data-type': 'synced-block', 'class': 'me-synced' }, HTMLAttributes)]\n },\n\n addNodeView() {\n const render = blockNodeView(SyncedBlockView)\n return (props) => {\n const view = render(props)\n // The nested editor handles its own events, DOM changes and selection.\n const stop = view.stopEvent?.bind(view)\n view.stopEvent = (event: Event) => {\n const target = event.target as Element | null\n if (target?.closest?.('.me-synced-content')) return true\n return stop ? stop(event) : false\n }\n view.ignoreMutation = () => true\n return view\n }\n },\n\n addCommands() {\n return {\n insertSyncedBlock: sourceId => ({ commands }) => commands.insertContent({ type: this.name, attrs: { sourceId } }),\n\n createSyncedBlock: () => ({ editor }) => {\n const { create, doc } = this.options\n if (!create || !doc) return false\n const range = currentRange(editor.state)\n if (!range) return false\n const blocks = (editor.state.doc.slice(range.from, range.to).content.toJSON() ?? []) as JSONContent[]\n const content: JSONContent = { type: 'doc', content: blocks.length && !isEmptyLine(blocks) ? blocks : [{ type: 'paragraph' }] }\n const from = track(editor, range.from)\n const to = track(editor, range.to)\n const stop = () => {\n from.stop()\n to.stop()\n }\n ;(async () => {\n const sourceId = await create()\n const source = await doc(sourceId)\n if (!source) throw new Error(`synced-block: no document for new source ${sourceId}`)\n const { fillDocument } = await import('@toantranmei/editor/collaboration')\n fillDocument(source.doc, content, editor.schema, source.field)\n stop()\n if (editor.isDestroyed) return\n const node = editor.schema.nodes[SYNCED_NODE]!.create({ sourceId })\n const a = from.get()\n const b = to.get()\n const tr = editor.state.tr\n if (a < b && b <= tr.doc.content.size) tr.replaceWith(a, b, node)\n else tr.insert(Math.min(a, tr.doc.content.size), node)\n editor.view.dispatch(tr.scrollIntoView())\n })().catch((error) => {\n stop()\n reportError(this.options, error)\n })\n return true\n },\n }\n },\n})\n","import { Repeat2 } from '@lucide/vue'\nimport { defineBlock } from '@toantranmei/editor'\nimport { SyncedBlockNode } from './node'\nimport { SYNCED_NODE, syncedBlockRenderers } from './render'\nimport type { SyncedBlockOptions } from './types'\n\n/**\n * Synced blocks for `<MeiEditor :blocks>`: `syncedBlock.configure({ doc, create, originHref?, blocks? })`.\n * Slash item \"Synced block\" (needs `create`) moves the selected blocks into a new source.\n */\nexport const syncedBlock = defineBlock<SyncedBlockOptions>({\n name: 'syncedBlock',\n defaults: { doc: null, create: null, originHref: null, blocks: null, onError: null, messages: null },\n nodes: [SYNCED_NODE],\n extensions: o => [SyncedBlockNode.configure(o)],\n items: o => (o.create\n ? [{\n id: 'syncedBlock',\n icon: Repeat2,\n group: 'advanced',\n keywords: ['synced', 'sync', 'synced block', 'mirror', 'dong bo', 'khoi dong bo', '同步', '同步块'],\n insert: c => c.createSyncedBlock(),\n }]\n : []),\n render: () => syncedBlockRenderers(),\n messages: {\n vi: { blocks: { syncedBlock: { title: 'Khối đồng bộ', description: 'Nội dung giống nhau ở nhiều trang, sửa một nơi cập nhật mọi nơi.' } } },\n en: { blocks: { syncedBlock: { title: 'Synced block', description: 'Same content on many pages: edit once, updates everywhere.' } } },\n zh: { blocks: { syncedBlock: { title: '同步块', description: '多个页面共享同一内容,一处编辑处处更新。' } } },\n },\n})\n"],"mappings":";;;;;;;AAaA,IAAakB,IAA+C;CAC1D,IAAI;EAAE,OAAO;EAAgB,UAAU;EAAc,SAAS;EAA0B,aAAa;EAAgE,UAAU;CAAU;CACzL,IAAI;EAAE,OAAO;EAAgB,UAAU;EAAiB,SAAS;EAAyB,aAAa;EAAuC,UAAU;CAAY;CACpK,IAAI;EAAE,OAAO;EAAO,UAAU;EAAS,SAAS;EAAY,aAAa;EAAgB,UAAU;CAAM;AAC3G;AAEA,SAAgBC,EAAe,GAA4B,GAAyF;CAClJ,IAAMC,IAAOC,KAAUA,KAAUH,IAAWG,IAAS;CACrD,OAAO;EAAE,GAAGH,EAASE;EAAM,GAAGE,IAAYF;CAAK;AACjD;;;;;;;;;;;;;;;ECRA,IAAMY,IAAQC,GAERC,IAAUC,QAAeH,EAAM,UAAU,OAA6B,GACtEI,IAAWD,QAAgBH,EAAM,KAAK,MAAM,YAA8B,IAAI,GAC9EX,IAASc,QAAeH,EAAM,OAAO,QAAQ,WAAW,UAAU,IAAI,GACtEK,IAAIF,QAAehB,EAAeE,EAAO,OAAOa,EAAQ,MAAM,QAAQ,CAAC,GACvEI,IAAgBC,EAAYP,EAAM,MAAM,GAGxCQ,IAAQC,EAAkB,EAAE,QAAQ,UAAU,CAAC;EAErD,EAAML,GAAU,OAAO,MAAO;GAC5B,IAAMM,IAAUR,EAAQ,MAAM;GAC9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cC,EAAgC;CACzD,MAAM;CACN,UAAU;EAAE,KAAK;EAAM,QAAQ;EAAM,YAAY;EAAM,QAAQ;EAAM,SAAS;EAAM,UAAU;CAAK;CACnG,OAAO,CAACpF,CAAW;CACnB,aAAY,MAAK,CAAC2D,EAAgB,UAAU0B,CAAC,CAAC;CAC9C,QAAO,MAAMA,EAAE,SACX,CAAC;EACC,IAAI;EACJ,MAAMzC;EACN,OAAO;EACP,UAAU;GAAC;GAAU;GAAQ;GAAgB;GAAU;GAAW;GAAgB;GAAM;EAAK;EAC7F,SAAQ,MAAK0C,EAAE,kBAAkB;CACnC,CAAC,IACD,CAAC;CACL,cAAcvF,EAAqB;CACnC,UAAU;EACR,IAAI,EAAE,QAAQ,EAAE,aAAa;GAAE,OAAO;GAAgB,aAAa;EAAmE,EAAE,EAAE;EAC1I,IAAI,EAAE,QAAQ,EAAE,aAAa;GAAE,OAAO;GAAgB,aAAa;EAA6D,EAAE,EAAE;EACpI,IAAI,EAAE,QAAQ,EAAE,aAAa;GAAE,OAAO;GAAO,aAAa;EAAuB,EAAE,EAAE;CACvF;AACF,CAAC"}
@@ -0,0 +1,17 @@
1
+ // src/render.ts
2
+ var e = "syncedBlock";
3
+ function t() {
4
+ return { nodes: { [e]: (e, t) => {
5
+ let n = typeof e.attrs?.sourceId == "string" ? e.attrs.sourceId : null, r = t.options.data?.syncedBlocks, i = n ? r?.[n] : null;
6
+ if (!i) return "";
7
+ let a = i.type === "doc" ? t.children(i) : t.render(i);
8
+ return `<div class="me-synced" data-type="synced-block"${t.attr("data-source-id", n)}${t.blockAttrs(e)}><div class="me-synced-content">${a}</div></div>`;
9
+ } } };
10
+ }
11
+ var n = {
12
+ name: "syncedBlock",
13
+ render: t()
14
+ };
15
+ export { n, t as r, e as t };
16
+
17
+ //# sourceMappingURL=render-CFO6ghxH.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render-CFO6ghxH.js","names":["SYNCED_NODE","syncedBlockRenderers","sourceId","node","all","ctx","content","inner","syncedBlockRender"],"sources":["../src/render.ts"],"sourcesContent":["import type { JSONContent } from '@tiptap/core'\nimport type { BlockRenderers, RenderBlock } from '@toantranmei/editor/render'\n\nexport const SYNCED_NODE = 'syncedBlock'\n\n/**\n * Static HTML for synced blocks (`renderHTML` from `@toantranmei/editor/render`). The content\n * lives in another document: pass it as `data.syncedBlocks[sourceId]` (editor JSON, e.g.\n * `documentToJSON(doc)` or the server's ProseMirror JSON). Missing content renders nothing.\n */\nexport function syncedBlockRenderers(): BlockRenderers {\n return {\n nodes: {\n [SYNCED_NODE]: (node, ctx) => {\n const sourceId = typeof node.attrs?.sourceId === 'string' ? node.attrs.sourceId : null\n const all = ctx.options.data?.syncedBlocks as Record<string, JSONContent | null | undefined> | undefined\n const content = sourceId ? all?.[sourceId] : null\n if (!content) return ''\n const inner = content.type === 'doc' ? ctx.children(content) : ctx.render(content)\n return `<div class=\"me-synced\" data-type=\"synced-block\"${ctx.attr('data-source-id', sourceId)}${ctx.blockAttrs(node)}><div class=\"me-synced-content\">${inner}</div></div>`\n },\n },\n }\n}\n\n/** Render-only stand-in for `syncedBlock`, for servers that don't load Vue / Tiptap. */\nexport const syncedBlockRender: RenderBlock = { name: 'syncedBlock', render: syncedBlockRenderers() }\n"],"mappings":";AAGA,IAAaA,IAAc;AAO3B,SAAgBC,IAAuC;CACrD,OAAO,EACL,OAAO,GACJD,KAAe,GAAM,MAAQ;EAC5B,IAAME,IAAW,OAAOC,EAAK,OAAO,YAAa,WAAWA,EAAK,MAAM,WAAW,MAC5EC,IAAMC,EAAI,QAAQ,MAAM,cACxBC,IAAUJ,IAAWE,IAAMF,KAAY;EAC7C,IAAI,CAACI,GAAS,OAAO;EACrB,IAAMC,IAAQD,EAAQ,SAAS,QAAQD,EAAI,SAASC,CAAO,IAAID,EAAI,OAAOC,CAAO;EACjF,OAAO,kDAAkDD,EAAI,KAAK,kBAAkBH,CAAQ,IAAIG,EAAI,WAAWF,CAAI,EAAE,kCAAkCI,EAAM;CAC/J,EACF,EACF;AACF;AAGA,IAAaC,IAAiC;CAAE,MAAM;CAAe,QAAQP,EAAqB;AAAE"}
package/dist/render.js ADDED
@@ -0,0 +1 @@
1
+ export { t as SYNCED_NODE, n as syncedBlockRender, r as syncedBlockRenderers } from "./render-CFO6ghxH.js";
package/dist/style.css ADDED
@@ -0,0 +1,2 @@
1
+ @layer mei-editor{.me-synced{margin:var(--me-block-box-spacing) 0;padding:var(--me-synced-padding);border:var(--me-synced-border-width) solid transparent;border-radius:var(--me-synced-radius);transition:border-color var(--me-duration-fast);position:relative}.me-synced:hover,.me-synced:focus-within,.me-synced.is-selected{border-color:var(--me-synced-border)}.me-synced:focus-within{border-color:var(--me-synced-hover-border)}.me-synced-bar{--me-icon-size:var(--me-synced-label-font-size);top:var(--me-synced-label-offset);left:var(--me-space-5);z-index:2;align-items:center;gap:var(--me-synced-gap);padding:var(--me-synced-label-padding);background:var(--me-synced-label-bg);color:var(--me-synced-label-color);font-size:var(--me-synced-label-font-size);line-height:var(--me-font-line-height);opacity:0;pointer-events:none;transition:opacity var(--me-duration-fast);display:flex;position:absolute}.me-synced:hover .me-synced-bar,.me-synced:focus-within .me-synced-bar,.me-synced.is-selected .me-synced-bar{opacity:1;pointer-events:auto}.me-synced-label{align-items:center;gap:var(--me-space-2);display:inline-flex}.me-synced-origin{color:inherit;text-underline-offset:var(--me-space-1);text-decoration:underline}.me-synced-empty{padding:var(--me-space-4) var(--me-block-padding-x);color:var(--me-synced-empty-color)}.me-synced-content>.me-root{background:0 0}.me-synced-content .me-content>:last-child{margin-bottom:0}.me-synced-content .me-editor-tail{display:none}}
2
+ /*$vite$:1*/
@@ -0,0 +1,93 @@
1
+ declare const __VLS_export: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
2
+ editor: {
3
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["editor"]>;
4
+ required: true;
5
+ };
6
+ node: {
7
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["node"]>;
8
+ required: true;
9
+ };
10
+ decorations: {
11
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["decorations"]>;
12
+ required: true;
13
+ };
14
+ selected: {
15
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["selected"]>;
16
+ required: true;
17
+ };
18
+ extension: {
19
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["extension"]>;
20
+ required: true;
21
+ };
22
+ getPos: {
23
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["getPos"]>;
24
+ required: true;
25
+ };
26
+ updateAttributes: {
27
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["updateAttributes"]>;
28
+ required: true;
29
+ };
30
+ deleteNode: {
31
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["deleteNode"]>;
32
+ required: true;
33
+ };
34
+ view: {
35
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["view"]>;
36
+ required: true;
37
+ };
38
+ innerDecorations: {
39
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["innerDecorations"]>;
40
+ required: true;
41
+ };
42
+ HTMLAttributes: {
43
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["HTMLAttributes"]>;
44
+ required: true;
45
+ };
46
+ }>, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
47
+ editor: {
48
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["editor"]>;
49
+ required: true;
50
+ };
51
+ node: {
52
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["node"]>;
53
+ required: true;
54
+ };
55
+ decorations: {
56
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["decorations"]>;
57
+ required: true;
58
+ };
59
+ selected: {
60
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["selected"]>;
61
+ required: true;
62
+ };
63
+ extension: {
64
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["extension"]>;
65
+ required: true;
66
+ };
67
+ getPos: {
68
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["getPos"]>;
69
+ required: true;
70
+ };
71
+ updateAttributes: {
72
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["updateAttributes"]>;
73
+ required: true;
74
+ };
75
+ deleteNode: {
76
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["deleteNode"]>;
77
+ required: true;
78
+ };
79
+ view: {
80
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["view"]>;
81
+ required: true;
82
+ };
83
+ innerDecorations: {
84
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["innerDecorations"]>;
85
+ required: true;
86
+ };
87
+ HTMLAttributes: {
88
+ type: import("vue").PropType<import("@tiptap/core").NodeViewProps["HTMLAttributes"]>;
89
+ required: true;
90
+ };
91
+ }>> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
92
+ declare const _default: typeof __VLS_export;
93
+ export default _default;
@@ -0,0 +1,6 @@
1
+ import type { SyncedBlockOptions } from './types';
2
+ /**
3
+ * Synced blocks for `<MeiEditor :blocks>`: `syncedBlock.configure({ doc, create, originHref?, blocks? })`.
4
+ * Slash item "Synced block" (needs `create`) moves the selected blocks into a new source.
5
+ */
6
+ export declare const syncedBlock: import("@toantranmei/editor").BlockDefinition<SyncedBlockOptions>;
@@ -0,0 +1,6 @@
1
+ import './styles/synced.css';
2
+ export { syncedBlock } from './definition';
3
+ export { SyncedBlockNode } from './node';
4
+ export { SYNCED_NODE, syncedBlockRender, syncedBlockRenderers } from './render';
5
+ export { messages, syncedMessages, type SyncedMessages } from './messages';
6
+ export type { SyncedBlockOptions, SyncedSource } from './types';
@@ -0,0 +1,13 @@
1
+ import type { LocaleName } from '@toantranmei/editor';
2
+ export interface SyncedMessages {
3
+ /** Frame label. */
4
+ label: string;
5
+ /** Link to the source's page. */
6
+ original: string;
7
+ loading: string;
8
+ /** `doc` returned null or failed. */
9
+ unavailable: string;
10
+ readOnly: string;
11
+ }
12
+ export declare const messages: Record<LocaleName, SyncedMessages>;
13
+ export declare function syncedMessages(locale: string | undefined, overrides?: Partial<Record<LocaleName, Partial<SyncedMessages>>> | null): SyncedMessages;
@@ -0,0 +1,21 @@
1
+ import { Node } from '@tiptap/core';
2
+ import type { SyncedBlockOptions } from './types';
3
+ declare module '@tiptap/core' {
4
+ interface Commands<ReturnType> {
5
+ syncedBlock: {
6
+ /** Inserts a synced block showing an existing source (e.g. "Paste and sync"). */
7
+ insertSyncedBlock: (sourceId: string) => ReturnType;
8
+ /**
9
+ * "Synced block": creates a source (`create`), moves the selected blocks into it (an
10
+ * empty line gives an empty source) and puts a synced block in their place.
11
+ */
12
+ createSyncedBlock: () => ReturnType;
13
+ };
14
+ }
15
+ }
16
+ export declare const reportError: (options: SyncedBlockOptions, error: unknown) => void;
17
+ /**
18
+ * Synced block: shows (and edits) a separate Yjs document, so the same content appears on
19
+ * many pages. The node stores only `sourceId`.
20
+ */
21
+ export declare const SyncedBlockNode: Node<SyncedBlockOptions, any>;
@@ -0,0 +1 @@
1
+ export { SYNCED_NODE, syncedBlockRender, syncedBlockRenderers } from './render';
@@ -0,0 +1,10 @@
1
+ import type { BlockRenderers, RenderBlock } from '@toantranmei/editor/render';
2
+ export declare const SYNCED_NODE = "syncedBlock";
3
+ /**
4
+ * Static HTML for synced blocks (`renderHTML` from `@toantranmei/editor/render`). The content
5
+ * lives in another document: pass it as `data.syncedBlocks[sourceId]` (editor JSON, e.g.
6
+ * `documentToJSON(doc)` or the server's ProseMirror JSON). Missing content renders nothing.
7
+ */
8
+ export declare function syncedBlockRenderers(): BlockRenderers;
9
+ /** Render-only stand-in for `syncedBlock`, for servers that don't load Vue / Tiptap. */
10
+ export declare const syncedBlockRender: RenderBlock;
@@ -0,0 +1,26 @@
1
+ import type { BlockDefinition, LocaleName } from '@toantranmei/editor';
2
+ import type * as Y from 'yjs';
3
+ import type { SyncedMessages } from './messages';
4
+ /** The shared document behind a synced block, from your app (cache it: one Y.Doc per source). */
5
+ export interface SyncedSource {
6
+ doc: Y.Doc;
7
+ /** Whether this user may edit the source (read-only otherwise). */
8
+ editable: boolean;
9
+ /** `Y.XmlFragment` name in `doc`. Default `'default'`. */
10
+ field?: string;
11
+ }
12
+ export interface SyncedBlockOptions {
13
+ /** Shared document for a source id (contract 3.4: doc id = `sourceId`). `null`: missing / no access. */
14
+ doc: ((sourceId: string) => SyncedSource | null | Promise<SyncedSource | null>) | null;
15
+ /** Creates a new source (resource type `synced_block`, parent = this page) and returns its id. */
16
+ create: (() => Promise<string>) | null;
17
+ /** Link to the page that owns the source ("Edit original"). */
18
+ originHref: ((sourceId: string) => string | null) | null;
19
+ /**
20
+ * Blocks of the nested editor. Use the page editor's list (synced blocks themselves are
21
+ * left out, so a source can't embed itself). Default: the editor's `defaultBlocks`.
22
+ */
23
+ blocks: BlockDefinition[] | null;
24
+ onError: ((error: unknown) => void) | null;
25
+ messages: Partial<Record<LocaleName, Partial<SyncedMessages>>> | null;
26
+ }
package/package.json CHANGED
@@ -1,6 +1,87 @@
1
1
  {
2
2
  "name": "@toantranmei/synced-block",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Notion-style synced blocks for @toantranmei/editor: one Yjs document shown and edited on many pages.",
5
+ "keywords": [
6
+ "vue",
7
+ "tiptap",
8
+ "notion",
9
+ "synced block",
10
+ "yjs",
11
+ "editor",
12
+ "collaboration"
13
+ ],
14
+ "license": "MIT",
15
+ "author": "toantranmei",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/toantranmei/editor.git",
19
+ "directory": "packages/synced-block"
20
+ },
21
+ "homepage": "https://github.com/toantranmei/editor#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/toantranmei/editor/issues"
24
+ },
25
+ "type": "module",
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "main": "./dist/index.js",
32
+ "module": "./dist/index.js",
33
+ "types": "./dist/types/index.d.ts",
34
+ "exports": {
35
+ ".": {
36
+ "types": "./dist/types/index.d.ts",
37
+ "import": "./dist/index.js"
38
+ },
39
+ "./render": {
40
+ "types": "./dist/types/render-entry.d.ts",
41
+ "import": "./dist/render.js"
42
+ },
43
+ "./style.css": "./dist/style.css"
44
+ },
45
+ "sideEffects": [
46
+ "**/*.css"
47
+ ],
48
+ "peerDependencies": {
49
+ "@lucide/vue": "^1.61.0",
50
+ "@tiptap/core": "3.32.0",
51
+ "@tiptap/pm": "3.32.0",
52
+ "@tiptap/vue-3": "3.32.0",
53
+ "@toantranmei/editor": "^0.3.0",
54
+ "vue": "^3.5.0",
55
+ "yjs": "^13.6.0"
56
+ },
57
+ "devDependencies": {
58
+ "@lucide/vue": "^1.61.0",
59
+ "@tiptap/core": "3.32.0",
60
+ "@tiptap/pm": "3.32.0",
61
+ "@tiptap/vue-3": "3.32.0",
62
+ "@types/node": "^26.6.5",
63
+ "@vitejs/plugin-vue": "^6.0.10",
64
+ "@vue/test-utils": "^2.4.6",
65
+ "happy-dom": "21.0.4",
66
+ "typescript": "~5.9.3",
67
+ "vite": "^8.3.5",
68
+ "vitest": "5.0.5",
69
+ "vue": "^3.5.0",
70
+ "vue-tsc": "^3.0.0",
71
+ "yjs": "^13.6.0",
72
+ "@toantranmei/editor": "0.3.0"
73
+ },
74
+ "publishConfig": {
75
+ "access": "public"
76
+ },
77
+ "engines": {
78
+ "node": ">=20"
79
+ },
80
+ "scripts": {
81
+ "build": "node ../editor/scripts/check-styles.mjs src && vite build && vue-tsc -p tsconfig.build.json",
82
+ "dev": "vite build --watch",
83
+ "typecheck": "vue-tsc --noEmit -p tsconfig.json",
84
+ "check:styles": "node ../editor/scripts/check-styles.mjs src",
85
+ "test": "vitest run"
86
+ }
6
87
  }