Skip to content

Commit 05a1995

Browse files
committed
fix: transformCopied mdImgUri
close #89
1 parent 801e420 commit 05a1995

File tree

4 files changed

+120
-9
lines changed

4 files changed

+120
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@
177177
"svgmoji": "^3.2.0",
178178
"void-elements": "^3.1.0",
179179
"yjs": "^13.6.14",
180-
"zens": "^0.0.24"
180+
"zens": "^0.0.25"
181181
}
182182
}
Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
import { WysiwygThemeWrapper } from '../../theme'
22
import { prosemirrorNodeToHtml } from 'remirror'
3-
import type { Node } from '@remirror/pm/model'
3+
import { DOMSerializer, type Node } from '@remirror/pm/model'
44
import { EditorProps } from '../Editor'
55
import { createWysiwygDelegate } from '../WysiwygEditor'
6+
// @ts-ignore
7+
import HTML from 'html-parse-stringify'
8+
import { useEffect, useState } from 'react'
9+
import { nanoid } from 'nanoid'
10+
import { Loading3QuartersOutlined } from 'zens/esm/Icons'
611

712
interface PreviewProps {
813
doc: Node | string
914
delegateOptions?: EditorProps['delegateOptions']
1015
}
1116

17+
type HTMLAstNode = {
18+
attrs: Record<string, any>
19+
name: string
20+
type: string
21+
children?: HTMLAstNode[]
22+
}
23+
1224
export const Preview: React.FC<PreviewProps> = (props) => {
1325
const { doc, delegateOptions } = props
26+
const [processedHtml, setProcessedHtml] = useState('')
1427
let targetDoc: PreviewProps['doc'] = doc
1528

1629
if (typeof targetDoc === 'string') {
@@ -19,5 +32,60 @@ export const Preview: React.FC<PreviewProps> = (props) => {
1932

2033
const html = prosemirrorNodeToHtml(targetDoc)
2134

22-
return <WysiwygThemeWrapper dangerouslySetInnerHTML={{ __html: html }} />
35+
useEffect(() => {
36+
const fullAst = HTML.parse(html)
37+
38+
const imageLoadTasks: Promise<void>[] = []
39+
const handleHtmlText = async (ast: HTMLAstNode[]) => {
40+
const handleNode = (node: HTMLAstNode) => {
41+
if (!node) {
42+
return
43+
}
44+
45+
if (node.name === 'img' && node.attrs?.src && delegateOptions?.handleViewImgSrcUrl) {
46+
imageLoadTasks.push(
47+
(async () => {
48+
node.attrs.src = await delegateOptions?.handleViewImgSrcUrl?.(node.attrs.src)
49+
node.attrs.key = nanoid()
50+
})(),
51+
)
52+
}
53+
54+
if (node.children) {
55+
handleHtmlText(node.children)
56+
}
57+
}
58+
59+
for (let i = 0; i < ast.length; i++) {
60+
handleNode(ast[i])
61+
}
62+
}
63+
64+
handleHtmlText(fullAst)
65+
Promise.all(imageLoadTasks)
66+
.then((res) => {
67+
setProcessedHtml(HTML.stringify(fullAst))
68+
})
69+
.catch(() => {
70+
setProcessedHtml(html)
71+
})
72+
}, [html])
73+
74+
if (!processedHtml) {
75+
return (
76+
<div
77+
style={{
78+
width: '100%',
79+
minHeight: '60px',
80+
display: 'flex',
81+
justifyContent: 'center',
82+
alignItems: 'center',
83+
}}
84+
>
85+
<Loading3QuartersOutlined spin size={40} />
86+
</div>
87+
)
88+
}
89+
90+
return <WysiwygThemeWrapper dangerouslySetInnerHTML={{ __html: processedHtml }} />
2391
}

src/editor/extensions/Inline/inline-mark-extensions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const globalImageHrefCache: Map<string, string> = new Map()
197197
},
198198
staticKeys: [],
199199
handlerKeys: [],
200-
customHandlerKeys: []
200+
customHandlerKeys: [],
201201
})
202202
class ImgUri extends MarkExtension<MfImgOptions> {
203203
static disableExtraAttributes = true
@@ -216,7 +216,11 @@ class ImgUri extends MarkExtension<MfImgOptions> {
216216
default: '',
217217
},
218218
},
219-
toDOM: (mark) => ['img', { src: globalImageHrefCache.get(mark.attrs.href) || mark.attrs.href }, 0]
219+
toDOM: (mark) => [
220+
'img',
221+
{ src: mark.attrs.href },
222+
0,
223+
],
220224
}
221225
}
222226

yarn.lock

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@
2020
resolved "https://registry.yarnpkg.com/@anatine/esbuild-decorators/-/esbuild-decorators-0.2.19.tgz#d4137182abfdf0e6de680f69b447492813b62ef4"
2121
integrity sha512-pyj6ULyMacyzpDqlnbS2OCkOqxcVgk8IqiTMRJ5CrsF8Yl1azvlX/AM6xWR8UzHKUYDlWOw5mOpos3+7KKR0Lw==
2222

23+
"@ant-design/colors@^7.0.0":
24+
version "7.0.2"
25+
resolved "https://registry.yarnpkg.com/@ant-design/colors/-/colors-7.0.2.tgz#c5c753a467ce8d86ba7ca4736d2c01f599bb5492"
26+
integrity sha512-7KJkhTiPiLHSu+LmMJnehfJ6242OCxSlR3xHVBecYxnMW8MS/878NXct1GqYARyL59fyeFdKRxXTfvR9SnDgJg==
27+
dependencies:
28+
"@ctrl/tinycolor" "^3.6.1"
29+
30+
"@ant-design/icons-svg@^4.4.0":
31+
version "4.4.2"
32+
resolved "https://registry.yarnpkg.com/@ant-design/icons-svg/-/icons-svg-4.4.2.tgz#ed2be7fb4d82ac7e1d45a54a5b06d6cecf8be6f6"
33+
integrity sha512-vHbT+zJEVzllwP+CM+ul7reTEfBR0vgxFe7+lREAsAA7YGsYpboiq2sQNeQeRvh09GfQgs/GyFEvZpJ9cLXpXA==
34+
35+
"@ant-design/icons@^5.3.7":
36+
version "5.3.7"
37+
resolved "https://registry.yarnpkg.com/@ant-design/icons/-/icons-5.3.7.tgz#d9f3654bf7934ee5faba43f91b5a187f5309ec68"
38+
integrity sha512-bCPXTAg66f5bdccM4TT21SQBDO1Ek2gho9h3nO9DAKXJP4sq+5VBjrQMSxMVXSB3HyEz+cUbHQ5+6ogxCOpaew==
39+
dependencies:
40+
"@ant-design/colors" "^7.0.0"
41+
"@ant-design/icons-svg" "^4.4.0"
42+
"@babel/runtime" "^7.11.2"
43+
classnames "^2.2.6"
44+
rc-util "^5.31.1"
45+
2346
"@ariakit/[email protected]":
2447
version "0.4.5"
2548
resolved "https://registry.yarnpkg.com/@ariakit/core/-/core-0.4.5.tgz#ba42573474c1f54109cf96650e8614d7312a432d"
@@ -612,6 +635,11 @@
612635
dependencies:
613636
"@jridgewell/trace-mapping" "0.3.9"
614637

638+
"@ctrl/tinycolor@^3.6.1":
639+
version "3.6.1"
640+
resolved "https://registry.yarnpkg.com/@ctrl/tinycolor/-/tinycolor-3.6.1.tgz#b6c75a56a1947cc916ea058772d666a2c8932f31"
641+
integrity sha512-SITSV6aIXsuVNV3f3O0f2n/cgyEDWoSqtZMYiAmcsYHydcKrOz3gUxB/iXd/Qf08+IZX4KpgNbvUdMBmWz+kcA==
642+
615643
"@drl990114/codemirror-themes@^0.0.1":
616644
version "0.0.1"
617645
resolved "https://registry.yarnpkg.com/@drl990114/codemirror-themes/-/codemirror-themes-0.0.1.tgz#da604383518cd4213207aaf8831422312a3fd2ae"
@@ -8392,6 +8420,14 @@ rc-util@^5.21.0, rc-util@^5.24.4, rc-util@^5.34.1:
83928420
"@babel/runtime" "^7.18.3"
83938421
react-is "^18.2.0"
83948422

8423+
rc-util@^5.31.1:
8424+
version "5.43.0"
8425+
resolved "https://registry.yarnpkg.com/rc-util/-/rc-util-5.43.0.tgz#bba91fbef2c3e30ea2c236893746f3e9b05ecc4c"
8426+
integrity sha512-AzC7KKOXFqAdIBqdGWepL9Xn7cm3vnAmjlHqUnoQaTMZYhM4VlXGLkkHHxj/BZ7Td0+SOPKB4RGPboBVKT9htw==
8427+
dependencies:
8428+
"@babel/runtime" "^7.18.3"
8429+
react-is "^18.2.0"
8430+
83958431
react-color@^2.19.3:
83968432
version "2.19.3"
83978433
resolved "https://registry.yarnpkg.com/react-color/-/react-color-2.19.3.tgz#ec6c6b4568312a3c6a18420ab0472e146aa5683d"
@@ -9153,6 +9189,7 @@ string-hash@^1.1.1:
91539189
integrity sha512-kJUvRUFK49aub+a7T1nNE66EJbZBMnBgoC1UbCZ5n6bsZKBRga4KgBRTMn/pFkeCZSYtNeSyMxPDM0AXWELk2A==
91549190

91559191
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
9192+
name string-width-cjs
91569193
version "4.2.3"
91579194
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
91589195
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -10101,6 +10138,7 @@ why-is-node-running@^2.2.2:
1010110138
stackback "0.0.2"
1010210139

1010310140
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
10141+
name wrap-ansi-cjs
1010410142
version "7.0.0"
1010510143
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
1010610144
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -10257,11 +10295,12 @@ yocto-queue@^1.0.0:
1025710295
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
1025810296
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==
1025910297

10260-
zens@^0.0.24:
10261-
version "0.0.24"
10262-
resolved "https://registry.yarnpkg.com/zens/-/zens-0.0.24.tgz#c46a119549eda1edb03d7a71abd3d7cb1cb7c2cf"
10263-
integrity sha512-Gy4LGu0X0hV9nWaiuCQ6ZRoDRUYCM3wMAdHXG4UpI5xVDYee6QdT4OrhpldvQ8oxWncxCbxBOryJE6JQ7gqeTg==
10298+
zens@^0.0.25:
10299+
version "0.0.25"
10300+
resolved "https://registry.yarnpkg.com/zens/-/zens-0.0.25.tgz#958134c776a4e52399747316124b7677e863af15"
10301+
integrity sha512-7cjRP6Xp0rNNVTtIxspetI9yFU6GUDTSVEoGGvs3e94Z77ocGSfMbkbfoX624Ub7XGn5tg5UJsot8t6h4N4A+w==
1026410302
dependencies:
10303+
"@ant-design/icons" "^5.3.7"
1026510304
"@ariakit/react" "^0.4.5"
1026610305
"@babel/runtime" "^7"
1026710306
"@emotion/is-prop-valid" "^1.2.2"

0 commit comments

Comments
 (0)