@smartdoca/markdown 0.4.2 → 0.4.4

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/README.md CHANGED
@@ -1,43 +1,72 @@
1
- # exmd-collaborative-editor
1
+ # @smartdoca/markdown
2
2
 
3
- 基于 React、CodeMirror 6 和 Yjs 的 Markdown 编辑器,面向 Doca 的宿主托管协同场景。
3
+ [中文](README.zh-CN.md)
4
4
 
5
- ## 使用
5
+ Embeddable collaborative Markdown editor for React. It renders GFM, code, math, Mermaid, and a split preview. The host owns identity, files, permissions, and the durable save path.
6
+
7
+ Licensed under [MIT](LICENSE).
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ npm install @smartdoca/markdown react react-dom yjs
13
+ ```
6
14
 
7
15
  ```tsx
8
- import { CollaborativeMarkdownEditor } from 'exmd-collaborative-editor'
9
- import 'exmd-collaborative-editor/style.css'
10
-
11
- export function MarkdownDocument({ session, canEdit }) {
12
- return (
13
- <CollaborativeMarkdownEditor
14
- collaboration={session}
15
- mode={canEdit ? 'edit' : 'readonly'}
16
- />
17
- )
16
+ import { CollaborativeMarkdownEditor } from "@smartdoca/markdown";
17
+ import "@smartdoca/markdown/style.css";
18
+
19
+ export function Markdown({ markdown }: { markdown: string }) {
20
+ return <CollaborativeMarkdownEditor initialValue={markdown} mode="edit" height="100%" />;
18
21
  }
19
22
  ```
20
23
 
21
- `mode` 是唯一权限入口。宿主可用 `components` 替换 UI 区域,用 `selectionActions` 和 `toolbarActions` 增加业务动作,用 `messages` 覆盖稳定国际化键。
24
+ A host-managed session replaces `roomId` and `websocketUrl`. Without `collaboration`, both `roomId` and `websocketUrl` are required.
22
25
 
23
- ## 协同边界
26
+ ## Props
24
27
 
25
- 宿主创建并拥有 `CollaborationSession`、Y.Doc、Awareness、连接、认证、ACK、outbox、checkpoint 和重连。编辑器不会在宿主模式下创建额外的 Y.Doc、WebSocket 或 IndexedDB。
28
+ `CollaborativeMarkdownEditor` accepts `CollaborativeMarkdownEditorProps`.
26
29
 
27
- Markdown 正文固定存放在 `Y.Text("markdown")`。远端 update 不进入本地 update 回调;只读会话不发布本地正文、光标或选区。评论锚点使用 Yjs relative position,正文和 ACL 仍由 Doca 保存。
30
+ | Prop | Type | Role |
31
+ |---|---|---|
32
+ | `collaboration` | `CollaborationSession` | Stable host session. When set, the package does not open its own WebSocket or IndexedDB transport. |
33
+ | `roomId` | `string` | Demo transport room. Required only without `collaboration`. |
34
+ | `websocketUrl` | `string` | Demo transport URL. Required only without `collaboration`. |
35
+ | `initialValue` | `string` | Markdown used when the session starts. |
36
+ | `user` | `CollaborationUser` | Display identity for the built-in transport. |
37
+ | `mode` | `EditorMode` | `readonly` stops editing. |
38
+ | `resources` | `EditorResources` | Host asset callbacks. Keep the object reference stable for the session. |
39
+ | `onImagePreview` | `(event: MarkdownImagePreviewEvent) => void` | Takes over image/diagram preview with the current item and document gallery. |
40
+ | `uploadImage` | `(file) => Promise<string>` | Image upload. Defaults to a data URL. |
41
+ | `onChange` | `(markdown) => void` | Local markdown text. |
42
+ | `onConnectionChange` | `(state) => void` | Connection state. |
43
+ | `selectionActions` | `MarkdownSelectionAction[]` | Actions near a valid selection, or in the toolbar as fallback. |
44
+ | `selectionToolbar` | `boolean` | Floating actions. Defaults to `true`. `false` keeps actions in the top toolbar. |
45
+ | `toolbarActions` | `MarkdownSelectionAction[]` | Actions always shown in the top toolbar. |
46
+ | `components` | `CollaborativeMarkdownEditorComponents` | Replaces header, loading, or footer without replacing the Yjs lifecycle. |
47
+ | `defaultViewMode` | `ViewMode` | Initial editor, preview, or split view. |
48
+ | `syncScroll` | `boolean` | Scrolls the preview with the editor. Defaults to `true`. |
49
+ | `previewDebounceMs` | `number` | Preview debounce. Automatic timing is 150ms for large documents, otherwise 0. |
50
+ | `locale` | `string` | `zh` or `en`. Omitted stays Chinese. Unknown codes show English. Switching updates chrome only. |
51
+ | `messages` | `EditorMessages` | Replaces individual catalog entries. |
52
+ | `title`, `className`, `height` | | Chrome and layout. `height` defaults to `100vh`. |
53
+ | `persistence`, `persistenceKey` | | Built-in IndexedDB for the demo transport. Leave them off for a host session. |
28
54
 
29
- 资源上传与短期 URL 解析由 `resources` 注入。模型只保存稳定资源标识,不保存 Cookie、签名 URL 或部署域名。
55
+ ## Collaboration
30
56
 
31
- ## 文件交换
57
+ Give the host `collaboration` session to the editor and keep that object for the life of the document.
32
58
 
33
- Markdown 导入导出严格使用 UTF-8,不主动下载、不访问网络、不修改当前协作文档。PDF 导入只提取可验证的文本内容;文件交换结果由宿主决定是否创建新文档。
59
+ - Local text updates are observed from the session. Remote updates are applied to the same `Y.Doc`. Do not echo remote text into a second save request.
60
+ - `mode="readonly"` does not publish edits.
61
+ - Selection actions and comment ranges use `resolveMarkdownTextAnchor`. Anchors are text positions, not screen coordinates.
62
+ - `locale` and `messages` change interface copy only.
34
63
 
35
- ## 开发
64
+ Other entry points on the package root include `MarkdownPreview`, `importMarkdownFile`, `exportMarkdownFile`, `importPdfFile`, and `exportPdfFile`.
36
65
 
37
- ```bash
38
- yarn typecheck
39
- yarn build:lib
40
- yarn test:contract
41
- yarn test:files
42
- yarn test:i18n
43
- ```
66
+ Import `katex/dist/katex.min.css` in the host when displaying math. PDF runtimes load only when `importPdfFile` or `exportPdfFile` is explicitly called.
67
+
68
+ ## Image preview
69
+
70
+ Click an image or Mermaid diagram to open the built-in gallery. Drag to pan, use the wheel or buttons to zoom, and switch images with the previous/next buttons, arrow keys, or a horizontal touch swipe at the initial zoom. Escape closes the gallery. Preview also works in readonly mode and never changes Markdown or collaboration state.
71
+
72
+ Both `CollaborativeMarkdownEditor` and `MarkdownPreview` accept `onImagePreview`. Supplying it delegates the complete preview to the host. Custom `components.Preview` wrappers must forward this prop. The exported `MarkdownImagePreviewEvent` includes `image`, `images` in document order, and `index`; each `MarkdownPreviewImage` retains its original `path`, display metadata, and current resolved `src`. Keep resource authorization and URL resolution in the host. See [the event contract](docs/IMAGE_PREVIEW.md).
@@ -0,0 +1,72 @@
1
+ # @smartdoca/markdown
2
+
3
+ [English](README.md)
4
+
5
+ 可嵌入的 React 协同 Markdown 编辑器。支持 GFM、代码、数学公式、Mermaid 和分屏预览。宿主负责身份、文件、权限和持久保存。
6
+
7
+ 许可证为 [MIT](LICENSE)。
8
+
9
+ ## 安装
10
+
11
+ ```sh
12
+ npm install @smartdoca/markdown react react-dom yjs
13
+ ```
14
+
15
+ ```tsx
16
+ import { CollaborativeMarkdownEditor } from "@smartdoca/markdown";
17
+ import "@smartdoca/markdown/style.css";
18
+
19
+ export function Markdown({ markdown }: { markdown: string }) {
20
+ return <CollaborativeMarkdownEditor initialValue={markdown} mode="edit" height="100%" />;
21
+ }
22
+ ```
23
+
24
+ 宿主托管会话时不需要 `roomId` 和 `websocketUrl`。没有 `collaboration` 时,这两项都必填。
25
+
26
+ ## Props
27
+
28
+ `CollaborativeMarkdownEditor` 接收 `CollaborativeMarkdownEditorProps`。
29
+
30
+ | Prop | 类型 | 作用 |
31
+ |---|---|---|
32
+ | `collaboration` | `CollaborationSession` | 稳定的宿主会话。传入后,包不再打开自己的 WebSocket 或 IndexedDB。 |
33
+ | `roomId` | `string` | 演示传输的房间。仅在没有 `collaboration` 时必填。 |
34
+ | `websocketUrl` | `string` | 演示传输地址。仅在没有 `collaboration` 时必填。 |
35
+ | `initialValue` | `string` | 会话开始时的 Markdown。 |
36
+ | `user` | `CollaborationUser` | 内建传输使用的显示身份。 |
37
+ | `mode` | `EditorMode` | `readonly` 停止编辑。 |
38
+ | `resources` | `EditorResources` | 宿主资源回调。会话期间保持对象引用不变。 |
39
+ | `onImagePreview` | `(event: MarkdownImagePreviewEvent) => void` | 接管图片/图表预览,获得当前项及同文档图集。 |
40
+ | `uploadImage` | `(file) => Promise<string>` | 图片上传。默认转为 data URL。 |
41
+ | `onChange` | `(markdown) => void` | 本地 Markdown 文本。 |
42
+ | `onConnectionChange` | `(state) => void` | 连接状态。 |
43
+ | `selectionActions` | `MarkdownSelectionAction[]` | 有效选区旁的操作,没有浮层时退回工具栏。 |
44
+ | `selectionToolbar` | `boolean` | 浮动操作。默认 `true`。`false` 时操作留在顶部工具栏。 |
45
+ | `toolbarActions` | `MarkdownSelectionAction[]` | 始终显示在顶部工具栏的操作。 |
46
+ | `components` | `CollaborativeMarkdownEditorComponents` | 替换页眉、加载或页脚,不替换 Yjs 生命周期。 |
47
+ | `defaultViewMode` | `ViewMode` | 初始的编辑、预览或分屏。 |
48
+ | `syncScroll` | `boolean` | 预览跟随编辑器滚动。默认 `true`。 |
49
+ | `previewDebounceMs` | `number` | 预览防抖。大文档自动为 150ms,否则为 0。 |
50
+ | `locale` | `string` | `zh` 或 `en`。省略为中文。未知代码显示英文。切换只更新界面。 |
51
+ | `messages` | `EditorMessages` | 替换单个文案。 |
52
+ | `title`、`className`、`height` | | 界面和布局。`height` 默认 `100vh`。 |
53
+ | `persistence`、`persistenceKey` | | 演示传输的 IndexedDB。宿主会话不要开启。 |
54
+
55
+ ## 协同
56
+
57
+ 把宿主的 `collaboration` 会话交给编辑器,并在文档存活期间保持这个对象。
58
+
59
+ - 本地文本从会话中观察。远端更新应用到同一个 `Y.Doc`。不要把远端文本再写成一次保存。
60
+ - `mode="readonly"` 不发布编辑。
61
+ - 选区操作和评论范围使用 `resolveMarkdownTextAnchor`。锚点是文本位置,不是屏幕坐标。
62
+ - `locale` 和 `messages` 只改变界面文案。
63
+
64
+ 包根路径还导出 `MarkdownPreview`、`importMarkdownFile`、`exportMarkdownFile`、`importPdfFile` 和 `exportPdfFile`。
65
+
66
+ 显示数学公式时,宿主需导入 `katex/dist/katex.min.css`。PDF 运行时仅在显式调用 `importPdfFile` 或 `exportPdfFile` 时加载。
67
+
68
+ ## 图片预览
69
+
70
+ 点击图片或 Mermaid 图表打开内置图集。拖动可平移,滚轮或按钮可缩放,上一张/下一张按钮、左右方向键或初始缩放下的触控横滑可切图。Escape 关闭。只读模式也支持预览;所有预览操作只改变界面状态,不修改 Markdown 或协同文档。
71
+
72
+ `CollaborativeMarkdownEditor` 和 `MarkdownPreview` 都接受 `onImagePreview`;传入后由宿主完全接管预览。自定义 `components.Preview` 包装组件需要转发该属性。包根导出 `MarkdownImagePreviewEvent`(`image`、按文档顺序排列的 `images`、`index`)和 `MarkdownPreviewImage` 类型。图片保留原始 `path`、显示元数据及当前解析的 `src`;资源权限和 URL 解析始终由宿主负责。详细说明见[事件契约](docs/IMAGE_PREVIEW.md)。