|
1 | 1 | import "./styles/index.scss"; |
2 | 2 |
|
3 | | -import type { BlockContext, EditorKit } from "doc-editor-core"; |
| 3 | +import type { BlockContext, CopyContext, EditorKit, PasteContext } from "doc-editor-core"; |
4 | 4 | import type { CommandFn } from "doc-editor-core"; |
5 | | -import { BlockPlugin } from "doc-editor-core"; |
6 | | -import type { RenderElementProps } from "doc-editor-delta"; |
| 5 | +import { BlockPlugin, isHTMLElement } from "doc-editor-core"; |
| 6 | +import type { BlockElement, RenderElementProps } from "doc-editor-delta"; |
7 | 7 | import { Editor, HistoryEditor, Range, Transforms } from "doc-editor-delta"; |
8 | | -import { existKey, getClosestBlockPath, getUniqueId } from "doc-editor-utils"; |
| 8 | +import { existKey, getClosestBlockPath, getId, getUniqueId } from "doc-editor-utils"; |
9 | 9 |
|
| 10 | +import { isMatchTag } from "../clipboard/utils/is"; |
10 | 11 | import { focusSelection } from "../shared/modules/selection"; |
11 | 12 | import { DocImage } from "./components/doc-image"; |
12 | 13 | import { IMAGE_KEY, IMAGE_STATUS } from "./types"; |
@@ -105,6 +106,39 @@ export class ImagePlugin extends BlockPlugin { |
105 | 106 | imageInput.click(); |
106 | 107 | }; |
107 | 108 |
|
| 109 | + public serialize(context: CopyContext): void { |
| 110 | + const element = context.node as BlockElement; |
| 111 | + const img = element[IMAGE_KEY]; |
| 112 | + if (!img) return void 0; |
| 113 | + const node = document.createElement("img"); |
| 114 | + node.src = img.src; |
| 115 | + node.setAttribute("data-type", IMAGE_KEY); |
| 116 | + node.appendChild(context.html); |
| 117 | + context.html = node; |
| 118 | + } |
| 119 | + |
| 120 | + public deserialize(context: PasteContext): void { |
| 121 | + const { html } = context; |
| 122 | + if (!isHTMLElement(html)) return void 0; |
| 123 | + if (isMatchTag(html, "img")) { |
| 124 | + const src = html.getAttribute("src") || ""; |
| 125 | + const width = html.getAttribute("data-width") || 100; |
| 126 | + const height = html.getAttribute("data-height") || 100; |
| 127 | + context.nodes = [ |
| 128 | + { |
| 129 | + [IMAGE_KEY]: { |
| 130 | + src: src, |
| 131 | + status: IMAGE_STATUS.SUCCESS, |
| 132 | + width: Number(width), |
| 133 | + height: Number(height), |
| 134 | + }, |
| 135 | + uuid: getId(), |
| 136 | + children: [{ text: "" }], |
| 137 | + }, |
| 138 | + ]; |
| 139 | + } |
| 140 | + } |
| 141 | + |
108 | 142 | public render(context: BlockContext): JSX.Element { |
109 | 143 | return ( |
110 | 144 | <DocImage editor={this.editor} element={context.element} readonly={this.readonly}></DocImage> |
|
0 commit comments