@bytechain.cn/colamd 1.5.1 → 1.5.2
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/CHANGELOG.md +35 -0
- package/README.md +105 -38
- package/README_CN.md +121 -46
- package/dist/lib/colamd-renderer.js +193804 -0
- package/dist/lib/colamd-renderer.js.map +1 -0
- package/dist/lib/colamd.css +1810 -0
- package/dist/lib/editor/editor.d.ts +27 -0
- package/dist/lib/editor/html-view.d.ts +2 -0
- package/dist/lib/editor/lib.d.ts +49 -0
- package/dist/lib/editor/plugins/index.d.ts +56 -0
- package/dist/lib/editor/plugins/lib-entry.d.ts +10 -0
- package/dist/lib/editor/plugins/math-plugin.d.ts +9 -0
- package/dist/lib/editor/plugins/mermaid-plugin.d.ts +19 -0
- package/dist/lib/editor/plugins/themes/theme-manager.d.ts +30 -0
- package/dist/main/index.js +93 -32
- package/dist/preload/index.js +2 -1
- package/dist/renderer/assets/{arc-_lugyDzx.js → arc-CpIo2PoE.js} +1 -1
- package/dist/renderer/assets/{architectureDiagram-3BPJPVTR-DDfP-F9D.js → architectureDiagram-3BPJPVTR-T-4Cq0Bz.js} +2 -2
- package/dist/renderer/assets/{blockDiagram-GPEHLZMM-DOETUKma.js → blockDiagram-GPEHLZMM-C-SuR6eu.js} +4 -4
- package/dist/renderer/assets/{c4Diagram-AAUBKEIU-kAmLfVtk.js → c4Diagram-AAUBKEIU-PJp81cGm.js} +2 -2
- package/dist/renderer/assets/{channel-CD4vJFwr.js → channel-CtIHaaRI.js} +1 -1
- package/dist/renderer/assets/{chunk-2J33WTMH-DkVrgw4l.js → chunk-2J33WTMH-BrchfQL6.js} +1 -1
- package/dist/renderer/assets/{chunk-4BX2VUAB-JTZ-VKNX.js → chunk-4BX2VUAB-BCc3Ev_H.js} +1 -1
- package/dist/renderer/assets/{chunk-55IACEB6-Di_O5box.js → chunk-55IACEB6-B2M27HQo.js} +1 -1
- package/dist/renderer/assets/{chunk-727SXJPM-BgKvwR8w.js → chunk-727SXJPM-DXDHGECs.js} +5 -5
- package/dist/renderer/assets/{chunk-AQP2D5EJ-DonSXCCQ.js → chunk-AQP2D5EJ-BNb1lOQY.js} +3 -3
- package/dist/renderer/assets/{chunk-FMBD7UC4-CsV8Lmy6.js → chunk-FMBD7UC4-CzgfEH2z.js} +1 -1
- package/dist/renderer/assets/{chunk-ND2GUHAM-C_-_dlq7.js → chunk-ND2GUHAM-BUFT_yOe.js} +1 -1
- package/dist/renderer/assets/{chunk-QZHKN3VN-aCmcDsfu.js → chunk-QZHKN3VN-C-zV7oBi.js} +1 -1
- package/dist/renderer/assets/{classDiagram-v2-Q7XG4LA2-nuHQLjzM.js → classDiagram-4FO5ZUOK-CAyhGSvf.js} +6 -6
- package/dist/renderer/assets/{classDiagram-4FO5ZUOK-nuHQLjzM.js → classDiagram-v2-Q7XG4LA2-CAyhGSvf.js} +6 -6
- package/dist/renderer/assets/{cose-bilkent-S5V4N54A-Di1_SXt9.js → cose-bilkent-S5V4N54A-ldu81kCk.js} +1 -1
- package/dist/renderer/assets/{dagre-BM42HDAG-CqOUIYsL.js → dagre-BM42HDAG-NJgy8CTw.js} +3 -3
- package/dist/renderer/assets/{diagram-2AECGRRQ-F-fP19TC.js → diagram-2AECGRRQ-CuIcMpOn.js} +2 -2
- package/dist/renderer/assets/{diagram-5GNKFQAL-BD5XZc8G.js → diagram-5GNKFQAL-BJwFCroB.js} +3 -3
- package/dist/renderer/assets/{diagram-KO2AKTUF-Qa3ZxA67.js → diagram-KO2AKTUF-C7NgQ8ju.js} +2 -2
- package/dist/renderer/assets/{diagram-LMA3HP47-DqDnw2RA.js → diagram-LMA3HP47-B71v6MOq.js} +2 -2
- package/dist/renderer/assets/{diagram-OG6HWLK6-BH4Elzby.js → diagram-OG6HWLK6-D2ZxkrSe.js} +3 -3
- package/dist/renderer/assets/{erDiagram-TEJ5UH35-CaKZCewa.js → erDiagram-TEJ5UH35-4ToW0p3h.js} +4 -4
- package/dist/renderer/assets/{flowDiagram-I6XJVG4X-C8znUgyL.js → flowDiagram-I6XJVG4X-DBkZLqwn.js} +6 -6
- package/dist/renderer/assets/{ganttDiagram-6RSMTGT7-DnRvbMnU.js → ganttDiagram-6RSMTGT7-G3xD2VXj.js} +2 -2
- package/dist/renderer/assets/{gitGraphDiagram-PVQCEYII-CZuRZzk4.js → gitGraphDiagram-PVQCEYII-BcTNiFfg.js} +3 -3
- package/dist/renderer/assets/{graph-CYiEKt0j.js → graph--Vhl4p4D.js} +1 -1
- package/dist/renderer/assets/{index-BX6JHKv6.js → index-C63bU7CF.js} +400 -54
- package/dist/renderer/assets/{index-CcwAgVRi.css → index-D8nA981z.css} +12 -0
- package/dist/renderer/assets/{infoDiagram-5YYISTIA-BlBUdqMb.js → infoDiagram-5YYISTIA-DjKa7Yv-.js} +1 -1
- package/dist/renderer/assets/{ishikawaDiagram-YF4QCWOH-TeVytCrP.js → ishikawaDiagram-YF4QCWOH-xIWzW1pL.js} +1 -1
- package/dist/renderer/assets/{journeyDiagram-JHISSGLW-BLZa3jnX.js → journeyDiagram-JHISSGLW-DxMBkVOF.js} +4 -4
- package/dist/renderer/assets/{kanban-definition-UN3LZRKU-foVxWrJn.js → kanban-definition-UN3LZRKU-DqJeL6E1.js} +2 -2
- package/dist/renderer/assets/{layout-DOAlFUVb.js → layout-B3giiMoK.js} +2 -2
- package/dist/renderer/assets/{linear-BMBn6kVq.js → linear-Cqh3vVIf.js} +1 -1
- package/dist/renderer/assets/{mindmap-definition-RKZ34NQL-5pGeNCcp.js → mindmap-definition-RKZ34NQL-DUay02ma.js} +3 -3
- package/dist/renderer/assets/{pieDiagram-4H26LBE5-N9aAUxfr.js → pieDiagram-4H26LBE5-CVcfLtky.js} +3 -3
- package/dist/renderer/assets/{quadrantDiagram-W4KKPZXB-BPxbtjga.js → quadrantDiagram-W4KKPZXB-wDHffPOs.js} +2 -2
- package/dist/renderer/assets/{requirementDiagram-4Y6WPE33-CK1LrJyG.js → requirementDiagram-4Y6WPE33-C4pgGDGv.js} +3 -3
- package/dist/renderer/assets/{sankeyDiagram-5OEKKPKP-BNj7Sqii.js → sankeyDiagram-5OEKKPKP-Jsf0rRMo.js} +1 -1
- package/dist/renderer/assets/{sequenceDiagram-3UESZ5HK-B-r7lNhI.js → sequenceDiagram-3UESZ5HK-e6QS7gt8.js} +3 -3
- package/dist/renderer/assets/{stateDiagram-AJRCARHV-wrWE0MRa.js → stateDiagram-AJRCARHV-DgYKZzDq.js} +6 -6
- package/dist/renderer/assets/{stateDiagram-v2-BHNVJYJU-CE-Lqo2d.js → stateDiagram-v2-BHNVJYJU-CUdmd_50.js} +4 -4
- package/dist/renderer/assets/{timeline-definition-PNZ67QCA-BkeMGH4J.js → timeline-definition-PNZ67QCA-S12e1Mp_.js} +2 -2
- package/dist/renderer/assets/{vennDiagram-CIIHVFJN-BTxVzto3.js → vennDiagram-CIIHVFJN-CNDBG6A2.js} +1 -1
- package/dist/renderer/assets/{wardleyDiagram-YWT4CUSO-D2r5dVGW.js → wardleyDiagram-YWT4CUSO-CLlGQcof.js} +2 -2
- package/dist/renderer/assets/{xychartDiagram-2RQKCTM6-BHQdINVc.js → xychartDiagram-2RQKCTM6-CUtsHUQd.js} +2 -2
- package/dist/renderer/index.html +6 -2
- package/package.json +25 -1
- package/.github/workflows/release.yml +0 -66
- package/.trae/specs/optimize-theme-loading/checklist.md +0 -20
- package/.trae/specs/optimize-theme-loading/spec.md +0 -103
- package/.trae/specs/optimize-theme-loading/tasks.md +0 -40
- package/CLAUDE.md +0 -143
- package/android/app/build/.npmkeep +0 -0
- package/android/app/build.gradle +0 -76
- package/android/app/capacitor.build.gradle +0 -24
- package/android/app/proguard-rules.pro +0 -21
- package/android/app/release.keystore +0 -0
- package/android/app/src/androidTest/java/com/getcapacitor/myapp/ExampleInstrumentedTest.java +0 -26
- package/android/app/src/main/AndroidManifest.xml +0 -64
- package/android/app/src/main/java/cn/bytechain/colamd/MainActivity.java +0 -180
- package/android/app/src/main/res/drawable/ic_launcher_background.xml +0 -170
- package/android/app/src/main/res/drawable/splash.png +0 -0
- package/android/app/src/main/res/drawable-land-hdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-land-mdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-land-xhdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-land-xxhdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-land-xxxhdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-port-hdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-port-mdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-port-xhdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-port-xxhdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-port-xxxhdpi/splash.png +0 -0
- package/android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml +0 -34
- package/android/app/src/main/res/layout/activity_main.xml +0 -12
- package/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml +0 -5
- package/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml +0 -5
- package/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
- package/android/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png +0 -0
- package/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
- package/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
- package/android/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png +0 -0
- package/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
- package/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
- package/android/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png +0 -0
- package/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
- package/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
- package/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png +0 -0
- package/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
- package/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png +0 -0
- package/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
- package/android/app/src/main/res/values/ic_launcher_background.xml +0 -4
- package/android/app/src/main/res/values/strings.xml +0 -7
- package/android/app/src/main/res/values/styles.xml +0 -22
- package/android/app/src/main/res/xml/file_paths.xml +0 -5
- package/android/app/src/main/res/xml/network_security_config.xml +0 -8
- package/android/app/src/test/java/com/getcapacitor/myapp/ExampleUnitTest.java +0 -18
- package/android/build.gradle +0 -29
- package/android/capacitor.settings.gradle +0 -21
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +0 -7
- package/android/gradle.properties +0 -22
- package/android/gradlew +0 -248
- package/android/gradlew.bat +0 -92
- package/android/settings.gradle +0 -5
- package/android/variables.gradle +0 -16
- package/capacitor.config.js +0 -29
- package/capacitor.config.ts +0 -30
- package/demo.md +0 -193
- package/docs/RELEASE_NOTES_v1.5.1.md +0 -382
- package/docs/academic-demo.md +0 -567
- package/docs/agent-diff-view.md +0 -48
- package/docs/demo.html +0 -748
- package/docs/demo.md +0 -546
- package/docs/demo.pdf +0 -0
- package/docs/theme-paradigm.md +0 -797
- package/electron-builder.yml +0 -64
- package/electron.vite.config.js +0 -31
- package/electron.vite.config.ts +0 -30
- package/ios/App/App/AppDelegate.swift +0 -49
- package/ios/App/App/Assets.xcassets/AppIcon.appiconset/AppIcon-512@2x.png +0 -0
- package/ios/App/App/Assets.xcassets/AppIcon.appiconset/Contents.json +0 -14
- package/ios/App/App/Assets.xcassets/Contents.json +0 -6
- package/ios/App/App/Assets.xcassets/Splash.imageset/Contents.json +0 -23
- package/ios/App/App/Assets.xcassets/Splash.imageset/splash-2732x2732-1.png +0 -0
- package/ios/App/App/Assets.xcassets/Splash.imageset/splash-2732x2732-2.png +0 -0
- package/ios/App/App/Assets.xcassets/Splash.imageset/splash-2732x2732.png +0 -0
- package/ios/App/App/Base.lproj/LaunchScreen.storyboard +0 -32
- package/ios/App/App/Base.lproj/Main.storyboard +0 -19
- package/ios/App/App/Info.plist +0 -49
- package/ios/App/App.xcodeproj/project.pbxproj +0 -408
- package/ios/App/App.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
- package/ios/App/Podfile +0 -28
- package/resources/entitlements.mac.plist +0 -12
- package/resources/icon.icns +0 -0
- package/resources/icon.png +0 -0
- package/resources/icon.svg +0 -23
- package/resources/templates/slides/icon.png +0 -0
- package/resources/templates/slides/slides-template.md +0 -74
- package/resources/templates/slides/template-forest-ink.html +0 -540
- package/resources/templates/slides/template.html +0 -535
- package/scripts/afterPack.js +0 -13
- package/scripts/generate-icons.js +0 -102
- package/src/main/index.ts +0 -919
- package/src/preload/index.d.ts +0 -51
- package/src/preload/index.js +0 -70
- package/src/preload/index.ts +0 -110
- package/src/renderer/capacitor-api.ts +0 -713
- package/src/renderer/editor/editor.ts +0 -287
- package/src/renderer/editor/html-view.ts +0 -15
- package/src/renderer/editor/plugins/index.ts +0 -86
- package/src/renderer/editor/plugins/math-plugin.ts +0 -305
- package/src/renderer/editor/plugins/mermaid-plugin-custom.css +0 -46
- package/src/renderer/editor/plugins/mermaid-plugin-dark.css +0 -428
- package/src/renderer/editor/plugins/mermaid-plugin-elegant.css +0 -443
- package/src/renderer/editor/plugins/mermaid-plugin-newsprint.css +0 -208
- package/src/renderer/editor/plugins/mermaid-plugin.css +0 -111
- package/src/renderer/editor/plugins/mermaid-plugin.ts +0 -680
- package/src/renderer/editor/plugins/themes/base/dark.css +0 -23
- package/src/renderer/editor/plugins/themes/base/elegant.css +0 -32
- package/src/renderer/editor/plugins/themes/base/light.css +0 -20
- package/src/renderer/editor/plugins/themes/base/newsprint.css +0 -27
- package/src/renderer/editor/plugins/themes/base.css +0 -509
- package/src/renderer/editor/plugins/themes/components/mermaid/academic.css +0 -43
- package/src/renderer/editor/plugins/themes/components/mermaid/dark.css +0 -20
- package/src/renderer/editor/plugins/themes/components/mermaid/elegant.css +0 -24
- package/src/renderer/editor/plugins/themes/components/mermaid/light.css +0 -21
- package/src/renderer/editor/plugins/themes/components/mermaid/newsprint.css +0 -26
- package/src/renderer/editor/plugins/themes/components/mermaid/variables.css +0 -592
- package/src/renderer/editor/plugins/themes/foundation.css +0 -143
- package/src/renderer/editor/plugins/themes/theme-manager.ts +0 -133
- package/src/renderer/env.d.ts +0 -10
- package/src/renderer/index.html +0 -76
- package/src/renderer/main.ts +0 -775
- package/src/renderer/mobile.css +0 -428
- package/tsconfig.json +0 -14
- package/tsconfig.main.json +0 -12
- package/tsconfig.main.tsbuildinfo +0 -1
- package/tsconfig.preload.json +0 -12
- package/tsconfig.preload.tsbuildinfo +0 -1
- package/tsconfig.renderer.json +0 -13
- package/tsconfig.renderer.tsbuildinfo +0 -1
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import { Editor, rootCtx, defaultValueCtx, editorViewCtx, serializerCtx, remarkPluginsCtx, nodeViewCtx, schemaCtx } from '@milkdown/kit/core'
|
|
2
|
-
import { DOMSerializer } from '@milkdown/kit/prose/model'
|
|
3
|
-
import { EditorView } from '@milkdown/kit/prose/view'
|
|
4
|
-
import remarkBreaks from 'remark-breaks'
|
|
5
|
-
import { commonmark } from '@milkdown/kit/preset/commonmark'
|
|
6
|
-
import { gfm } from '@milkdown/kit/preset/gfm'
|
|
7
|
-
import { history } from '@milkdown/kit/plugin/history'
|
|
8
|
-
import { listener, listenerCtx } from '@milkdown/kit/plugin/listener'
|
|
9
|
-
import { clipboard } from '@milkdown/kit/plugin/clipboard'
|
|
10
|
-
import { replaceAll } from '@milkdown/kit/utils'
|
|
11
|
-
import { htmlView } from './html-view'
|
|
12
|
-
import { getAllPluginModules, getAllPlugins } from './plugins'
|
|
13
|
-
|
|
14
|
-
import '@milkdown/kit/prose/view/style/prosemirror.css'
|
|
15
|
-
|
|
16
|
-
let editorInstance: Editor | null = null
|
|
17
|
-
|
|
18
|
-
const inlineStyles: Record<string, string> = {
|
|
19
|
-
'h1': 'font-size:1.8em;font-weight:700;margin:1em 0 .5em;padding-bottom:.3em;border-bottom:1px solid #eee;',
|
|
20
|
-
'h2': 'font-size:1.4em;font-weight:600;margin:1em 0 .5em;padding-bottom:.25em;border-bottom:1px solid #eee;',
|
|
21
|
-
'h3': 'font-size:1.2em;font-weight:600;margin:.8em 0 .4em;',
|
|
22
|
-
'h4': 'font-weight:600;margin:.8em 0 .4em;',
|
|
23
|
-
'h5': 'font-weight:600;margin:.8em 0 .4em;',
|
|
24
|
-
'h6': 'font-weight:600;margin:.8em 0 .4em;',
|
|
25
|
-
'p': 'margin:.5em 0;line-height:1.75;',
|
|
26
|
-
'strong': 'font-weight:600;',
|
|
27
|
-
'a': 'color:#0969da;text-decoration:none;',
|
|
28
|
-
'code': 'background:rgba(175,184,193,0.2);padding:2px 6px;border-radius:3px;font-size:.875em;font-family:Menlo,Monaco,monospace;',
|
|
29
|
-
'pre': 'background:#f6f8fa;padding:16px;border-radius:6px;overflow-x:auto;margin:1em 0;',
|
|
30
|
-
'blockquote': 'border-left:4px solid #ddd;padding-left:16px;margin:1em 0;color:#666;',
|
|
31
|
-
'ul': 'padding-left:24px;margin:.5em 0;',
|
|
32
|
-
'ol': 'padding-left:24px;margin:.5em 0;',
|
|
33
|
-
'li': 'margin:.25em 0;',
|
|
34
|
-
'table': 'border-collapse:collapse;width:100%;margin:1em 0;',
|
|
35
|
-
'th': 'border:1px solid #ddd;padding:8px 12px;text-align:left;font-weight:600;background:#f6f8fa;',
|
|
36
|
-
'td': 'border:1px solid #ddd;padding:8px 12px;text-align:left;',
|
|
37
|
-
'hr': 'border:none;border-top:2px solid #ddd;margin:2em 0;',
|
|
38
|
-
'img': 'max-width:100%;',
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function enhanceClipboard(e: ClipboardEvent): void {
|
|
42
|
-
const html = e.clipboardData?.getData('text/html')
|
|
43
|
-
if (!html) return
|
|
44
|
-
|
|
45
|
-
const doc = new DOMParser().parseFromString(html, 'text/html')
|
|
46
|
-
|
|
47
|
-
for (const [tag, style] of Object.entries(inlineStyles)) {
|
|
48
|
-
doc.querySelectorAll(tag).forEach((el) => {
|
|
49
|
-
;(el as HTMLElement).setAttribute('style', style)
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
doc.querySelectorAll('pre code').forEach((el) => {
|
|
54
|
-
;(el as HTMLElement).setAttribute('style', 'background:none;padding:0;font-size:.875em;line-height:1.6;font-family:Menlo,Monaco,monospace;')
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
for (const p of getAllPlugins()) {
|
|
58
|
-
if (!p.enabled || !p.clipboardStyles) continue
|
|
59
|
-
for (const [selector, style] of Object.entries(p.clipboardStyles)) {
|
|
60
|
-
doc.querySelectorAll(selector).forEach((el) => {
|
|
61
|
-
;(el as HTMLElement).setAttribute('style', style)
|
|
62
|
-
})
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
e.clipboardData?.setData('text/html', doc.body.innerHTML)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const defaultContent = `# Welcome to ColaMD\n\nStart typing here...\n`
|
|
70
|
-
|
|
71
|
-
export async function createEditor(
|
|
72
|
-
rootId: string,
|
|
73
|
-
onChange?: (markdown: string) => void
|
|
74
|
-
): Promise<Editor> {
|
|
75
|
-
const root = document.getElementById(rootId)
|
|
76
|
-
if (!root) throw new Error(`Element #${rootId} not found`)
|
|
77
|
-
|
|
78
|
-
const pluginModules = getAllPluginModules()
|
|
79
|
-
|
|
80
|
-
let builder = Editor.make()
|
|
81
|
-
.config((ctx) => {
|
|
82
|
-
ctx.set(rootCtx, root)
|
|
83
|
-
ctx.set(defaultValueCtx, defaultContent)
|
|
84
|
-
ctx.set(remarkPluginsCtx, [
|
|
85
|
-
...pluginModules.map((m) => m.info.remarkPlugin),
|
|
86
|
-
{ plugin: remarkBreaks, options: undefined },
|
|
87
|
-
] as any)
|
|
88
|
-
if (onChange) {
|
|
89
|
-
ctx.get(listenerCtx).markdownUpdated((_ctx, markdown) => {
|
|
90
|
-
onChange(markdown)
|
|
91
|
-
})
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
.use(commonmark)
|
|
95
|
-
.use(gfm)
|
|
96
|
-
.use(history)
|
|
97
|
-
.use(listener)
|
|
98
|
-
.use(clipboard)
|
|
99
|
-
.use(htmlView)
|
|
100
|
-
|
|
101
|
-
for (const mod of pluginModules) {
|
|
102
|
-
for (const p of mod.milkdownPlugins) {
|
|
103
|
-
builder = builder.use(p)
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
editorInstance = await builder.create()
|
|
109
|
-
} catch (e: any) {
|
|
110
|
-
const offendingModule = pluginModules.find((m) =>
|
|
111
|
-
m.info.nodeTypes?.some((nt) => e?.message?.includes(nt))
|
|
112
|
-
)
|
|
113
|
-
if (offendingModule) {
|
|
114
|
-
console.warn('[editor] Milkdown context error, retrying without plugin:', offendingModule.info.id, e.message)
|
|
115
|
-
const filteredModules = pluginModules.filter((m) => m !== offendingModule)
|
|
116
|
-
builder = Editor.make()
|
|
117
|
-
.config((ctx) => {
|
|
118
|
-
ctx.set(rootCtx, root)
|
|
119
|
-
ctx.set(defaultValueCtx, defaultContent)
|
|
120
|
-
ctx.set(remarkPluginsCtx, [
|
|
121
|
-
...filteredModules.map((m) => m.info.remarkPlugin),
|
|
122
|
-
{ plugin: remarkBreaks, options: undefined },
|
|
123
|
-
] as any)
|
|
124
|
-
if (onChange) ctx.get(listenerCtx).markdownUpdated((_ctx, markdown) => { onChange(markdown) })
|
|
125
|
-
})
|
|
126
|
-
.use(commonmark)
|
|
127
|
-
.use(gfm)
|
|
128
|
-
.use(history)
|
|
129
|
-
.use(listener)
|
|
130
|
-
.use(clipboard)
|
|
131
|
-
.use(htmlView)
|
|
132
|
-
for (const mod of filteredModules) {
|
|
133
|
-
for (const p of mod.milkdownPlugins) {
|
|
134
|
-
builder = builder.use(p)
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
editorInstance = await builder.create()
|
|
138
|
-
} else {
|
|
139
|
-
throw e
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
editorInstance.action((ctx) => {
|
|
144
|
-
const nvs = ctx.get(nodeViewCtx)
|
|
145
|
-
const schema = ctx.get(schemaCtx)
|
|
146
|
-
const fixed = nvs.map((nv: any, i: number) => {
|
|
147
|
-
if (nv[0] != null) return nv
|
|
148
|
-
const viewFn = nv[1]
|
|
149
|
-
const fallbackNames = pluginModules.flatMap((m) => m.info.nodeTypes || [])
|
|
150
|
-
const name = fallbackNames[i - 1]
|
|
151
|
-
if (name && schema.nodes[name]) {
|
|
152
|
-
return [name, viewFn]
|
|
153
|
-
}
|
|
154
|
-
return nv
|
|
155
|
-
})
|
|
156
|
-
ctx.set(nodeViewCtx, fixed)
|
|
157
|
-
|
|
158
|
-
const oldView = ctx.get(editorViewCtx)
|
|
159
|
-
const rootEl = ctx.get(rootCtx) as HTMLElement
|
|
160
|
-
const nodeViews = Object.fromEntries(fixed)
|
|
161
|
-
|
|
162
|
-
const newView = new EditorView(rootEl, {
|
|
163
|
-
state: oldView.state,
|
|
164
|
-
nodeViews,
|
|
165
|
-
dispatchTransaction: oldView.props.dispatchTransaction!,
|
|
166
|
-
})
|
|
167
|
-
oldView.destroy()
|
|
168
|
-
rootEl.appendChild(newView.dom)
|
|
169
|
-
ctx.set(editorViewCtx, newView)
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
root.addEventListener('copy', enhanceClipboard)
|
|
173
|
-
root.addEventListener('cut', enhanceClipboard)
|
|
174
|
-
|
|
175
|
-
/*
|
|
176
|
-
* REMOVED: Custom IME composition handlers for mobile CJK input.
|
|
177
|
-
* ProseMirror handles IME composition natively via its DOM observer and input rules.
|
|
178
|
-
* These handlers dispatched synthetic input events that interfered with ProseMirror's
|
|
179
|
-
* internal state tracking, causing duplicated characters and broken undo history.
|
|
180
|
-
*
|
|
181
|
-
* Original code:
|
|
182
|
-
* if (/android|iphone|ipad/i.test(navigator.userAgent)) {
|
|
183
|
-
* root.addEventListener('compositionstart', () => { ... })
|
|
184
|
-
* root.addEventListener('compositionend', () => { ... })
|
|
185
|
-
* }
|
|
186
|
-
*/
|
|
187
|
-
|
|
188
|
-
root.addEventListener('click', (e) => {
|
|
189
|
-
if (!(e.metaKey || e.ctrlKey)) return
|
|
190
|
-
const link = (e.target as HTMLElement).closest('a')
|
|
191
|
-
if (!link) return
|
|
192
|
-
const href = link.getAttribute('href')
|
|
193
|
-
if (href) {
|
|
194
|
-
e.preventDefault()
|
|
195
|
-
window.electronAPI?.openExternal(href)
|
|
196
|
-
window.capacitorAPI?.openExternal(href)
|
|
197
|
-
}
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
return editorInstance
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export function getMarkdown(): string {
|
|
204
|
-
if (!editorInstance) return ''
|
|
205
|
-
let markdown = ''
|
|
206
|
-
editorInstance.action((ctx) => {
|
|
207
|
-
const serializer = ctx.get(serializerCtx)
|
|
208
|
-
const view = ctx.get(editorViewCtx)
|
|
209
|
-
markdown = serializer(view.state.doc)
|
|
210
|
-
})
|
|
211
|
-
return markdown
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Get HTML by cloning the live rendered DOM.
|
|
216
|
-
* Unlike {@link getHTML} which uses DOMSerializer (creating fresh nodes with
|
|
217
|
-
* "Rendering..." placeholders), this captures the actual rendered SVGs
|
|
218
|
-
* from mermaid, KaTeX math, etc. Used for PDF/HTML export.
|
|
219
|
-
*/
|
|
220
|
-
export function getLiveHTML(): string {
|
|
221
|
-
const editorDom = document.querySelector('#editor .ProseMirror')
|
|
222
|
-
if (!editorDom) return ''
|
|
223
|
-
const clone = editorDom.cloneNode(true) as HTMLElement
|
|
224
|
-
|
|
225
|
-
const plugins = getAllPlugins().filter((p) => p.enabled)
|
|
226
|
-
|
|
227
|
-
// Inline computed background colors from plugin-declared selectors
|
|
228
|
-
for (const p of plugins) {
|
|
229
|
-
if (!p.bgCaptureSelectors) continue
|
|
230
|
-
for (const selector of p.bgCaptureSelectors) {
|
|
231
|
-
const originals = editorDom.querySelectorAll(selector)
|
|
232
|
-
const clones = clone.querySelectorAll(selector)
|
|
233
|
-
originals.forEach((orig, i) => {
|
|
234
|
-
const val = getComputedStyle(orig).backgroundColor
|
|
235
|
-
if (val && clones[i]) (clones[i] as HTMLElement).style.backgroundColor = val
|
|
236
|
-
})
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// Remove raw-source textareas declared by plugins
|
|
241
|
-
const rawSelectors = plugins.flatMap((p) => p.rawSelectors || [])
|
|
242
|
-
if (rawSelectors.length) {
|
|
243
|
-
clone.querySelectorAll(rawSelectors.join(',')).forEach((el) => el.remove())
|
|
244
|
-
}
|
|
245
|
-
// Hide loading/error placeholders declared by plugins
|
|
246
|
-
const hideSelectors = plugins.flatMap((p) => p.hideSelectors || [])
|
|
247
|
-
if (hideSelectors.length) {
|
|
248
|
-
clone.querySelectorAll(hideSelectors.join(',')).forEach((el) => (el as HTMLElement).style.display = 'none')
|
|
249
|
-
}
|
|
250
|
-
return `<div id="write">${clone.innerHTML}</div>`
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
export function getHTML(): string {
|
|
254
|
-
if (!editorInstance) return ''
|
|
255
|
-
let html = ''
|
|
256
|
-
editorInstance.action((ctx) => {
|
|
257
|
-
const view = ctx.get(editorViewCtx)
|
|
258
|
-
const div = document.createElement('div')
|
|
259
|
-
const fragment = DOMSerializer.fromSchema(view.state.schema).serializeFragment(view.state.doc.content)
|
|
260
|
-
div.appendChild(fragment)
|
|
261
|
-
html = div.innerHTML
|
|
262
|
-
})
|
|
263
|
-
return html
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
export function setMarkdown(content: string): void {
|
|
267
|
-
if (!editorInstance) return
|
|
268
|
-
editorInstance.action(replaceAll(content))
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
export function togglePluginMode(nodeTypes: string[], mode: 'rendered' | 'raw'): void {
|
|
272
|
-
if (!editorInstance) return
|
|
273
|
-
editorInstance.action((ctx) => {
|
|
274
|
-
const view = ctx.get(editorViewCtx)
|
|
275
|
-
const { state, dispatch } = view
|
|
276
|
-
const tr = state.tr
|
|
277
|
-
const nodeTypeSet = new Set(nodeTypes)
|
|
278
|
-
|
|
279
|
-
state.doc.descendants((node, pos) => {
|
|
280
|
-
if (nodeTypeSet.has(node.type.name)) {
|
|
281
|
-
tr.setNodeMarkup(pos, undefined, { ...node.attrs, mode })
|
|
282
|
-
}
|
|
283
|
-
})
|
|
284
|
-
|
|
285
|
-
dispatch(tr)
|
|
286
|
-
})
|
|
287
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { $view } from '@milkdown/kit/utils'
|
|
2
|
-
import { htmlSchema } from '@milkdown/kit/preset/commonmark'
|
|
3
|
-
import type { NodeViewConstructor } from '@milkdown/kit/prose/view'
|
|
4
|
-
|
|
5
|
-
export const htmlView = $view(htmlSchema.node, (): NodeViewConstructor => {
|
|
6
|
-
return (node) => {
|
|
7
|
-
const dom = document.createElement('span')
|
|
8
|
-
dom.classList.add('milkdown-html-inline')
|
|
9
|
-
dom.innerHTML = node.attrs.value as string
|
|
10
|
-
return {
|
|
11
|
-
dom,
|
|
12
|
-
stopEvent: () => true
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
})
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import type { MilkdownPlugin } from '@milkdown/ctx'
|
|
2
|
-
import type { Plugin } from 'unified'
|
|
3
|
-
|
|
4
|
-
export interface ExportCapability {
|
|
5
|
-
label: string
|
|
6
|
-
defaultName: string
|
|
7
|
-
filter: { name: string; extensions: string[] }
|
|
8
|
-
execute: (element: HTMLElement) => Promise<string | null>
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface RendererPlugin {
|
|
12
|
-
id: string
|
|
13
|
-
name: string
|
|
14
|
-
enabled: boolean
|
|
15
|
-
remarkPlugin?: { plugin: Plugin<any[], any>; options?: any }
|
|
16
|
-
rehypePlugin?: Plugin<any[], any>
|
|
17
|
-
onMount?: (...args: any[]) => any
|
|
18
|
-
onBeforeMount?: (...args: any[]) => any
|
|
19
|
-
onInit?: () => void
|
|
20
|
-
onThemeChange?: (theme: string, customCSS?: string) => void
|
|
21
|
-
css?: string
|
|
22
|
-
nodeTypes?: string[]
|
|
23
|
-
exportCapabilities?: ExportCapability[]
|
|
24
|
-
renderStatus?: (container: HTMLElement) => Promise<{ ok: number; fail: number; total: number }>
|
|
25
|
-
ensureRendered?: () => Promise<void>
|
|
26
|
-
/** Selector→inline-style pairs applied during clipboard copy/cut */
|
|
27
|
-
clipboardStyles?: Record<string, string>
|
|
28
|
-
/** CSS rules injected into HTML export (use var(--...) for theme colors) */
|
|
29
|
-
exportStyles?: string
|
|
30
|
-
/** Selectors for raw-source textareas/inputs (blurred on save, removed on export) */
|
|
31
|
-
rawSelectors?: string[]
|
|
32
|
-
/** Selectors for loading/error placeholder elements (hidden during export) */
|
|
33
|
-
hideSelectors?: string[]
|
|
34
|
-
/** Selectors for elements needing computed background-color captured during clone */
|
|
35
|
-
bgCaptureSelectors?: string[]
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface PluginModule {
|
|
39
|
-
info: RendererPlugin
|
|
40
|
-
milkdownPlugins: MilkdownPlugin[]
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const modules: PluginModule[] = []
|
|
44
|
-
|
|
45
|
-
export function registerPluginModule(m: PluginModule): void {
|
|
46
|
-
modules.push(m)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function getAllPluginModules(): PluginModule[] {
|
|
50
|
-
return modules
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function getAllPlugins(): RendererPlugin[] {
|
|
54
|
-
return modules.map((m) => m.info)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function findPluginBySelector(selector: string): RendererPlugin | undefined {
|
|
58
|
-
const plugins = getAllPlugins()
|
|
59
|
-
if (selector.startsWith('node:')) {
|
|
60
|
-
const name = selector.slice(5)
|
|
61
|
-
return plugins.find((p) => p.nodeTypes?.includes(name))
|
|
62
|
-
}
|
|
63
|
-
return plugins.find((p) => p.id === selector)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function findExportCapabilities(className: string): ExportCapability[] {
|
|
67
|
-
for (const m of modules) {
|
|
68
|
-
if (m.info.enabled && m.info.exportCapabilities && m.info.nodeTypes?.some(nt => className.includes(nt.replace(/_/g, '-')))) {
|
|
69
|
-
return m.info.exportCapabilities
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return []
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function togglePlugin(id: string, enabled: boolean): void {
|
|
76
|
-
const mod = modules.find((m) => m.info.id === id)
|
|
77
|
-
if (mod) mod.info.enabled = enabled
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Build a CSS selector matching all export-capable plugin block elements */
|
|
81
|
-
export function getContextMenuSelector(): string {
|
|
82
|
-
return getAllPlugins()
|
|
83
|
-
.filter((p) => p.exportCapabilities && p.exportCapabilities.length > 0)
|
|
84
|
-
.flatMap((p) => (p.nodeTypes || []).map((nt) => '.' + nt.replace(/_/g, '-')))
|
|
85
|
-
.join(', ')
|
|
86
|
-
}
|
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import { $nodeSchema, $view } from '@milkdown/utils'
|
|
2
|
-
import type { NodeViewConstructor } from '@milkdown/prose/view'
|
|
3
|
-
import type { RendererPlugin } from './index'
|
|
4
|
-
import { registerPluginModule } from './index'
|
|
5
|
-
import katex from 'katex'
|
|
6
|
-
import remarkMath from 'remark-math'
|
|
7
|
-
import 'katex/dist/katex.min.css'
|
|
8
|
-
|
|
9
|
-
;(window as any).katex = katex
|
|
10
|
-
|
|
11
|
-
const mathInlineSchema = $nodeSchema('math_inline', () => ({
|
|
12
|
-
group: 'inline',
|
|
13
|
-
inline: true,
|
|
14
|
-
atom: true,
|
|
15
|
-
priority: 60,
|
|
16
|
-
attrs: {
|
|
17
|
-
text: { default: '', validate: 'string' },
|
|
18
|
-
mode: { default: 'rendered' as 'rendered' | 'raw' },
|
|
19
|
-
},
|
|
20
|
-
parseDOM: [
|
|
21
|
-
{ tag: 'span.math-inline', getAttrs: (dom) => ({ text: (dom as HTMLElement).dataset.math || '' }) },
|
|
22
|
-
],
|
|
23
|
-
toDOM: (node) => {
|
|
24
|
-
const span = document.createElement('span')
|
|
25
|
-
span.className = 'math-inline'
|
|
26
|
-
span.dataset.math = node.attrs.text as string
|
|
27
|
-
const text = (node.attrs.text as string) || ''
|
|
28
|
-
if (node.attrs.mode === 'raw') {
|
|
29
|
-
span.innerHTML = '<code class="math-raw" data-math="' + text + '">$' + text + '$</code>'
|
|
30
|
-
return { dom: span }
|
|
31
|
-
}
|
|
32
|
-
const katexLib = (window as any).katex
|
|
33
|
-
if (katexLib && text) {
|
|
34
|
-
try {
|
|
35
|
-
span.innerHTML = katexLib.renderToString(text, { throwOnError: false, displayMode: false })
|
|
36
|
-
} catch {
|
|
37
|
-
span.textContent = '$' + text + '$'
|
|
38
|
-
}
|
|
39
|
-
} else {
|
|
40
|
-
span.textContent = '$' + text + '$'
|
|
41
|
-
}
|
|
42
|
-
return { dom: span }
|
|
43
|
-
},
|
|
44
|
-
parseMarkdown: {
|
|
45
|
-
match: (node) => node.type === 'inlineMath',
|
|
46
|
-
runner: (state, node, type) => {
|
|
47
|
-
state.addNode(type, { text: (node.value as string) || '' })
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
toMarkdown: {
|
|
51
|
-
match: (node) => node.type.name === 'math_inline',
|
|
52
|
-
runner: (state, node) => {
|
|
53
|
-
state.addNode('inlineMath', undefined, node.attrs.text as string)
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
}))
|
|
57
|
-
|
|
58
|
-
function renderMathInline(dom: HTMLElement, text: string, mode: string): void {
|
|
59
|
-
dom.dataset.math = text
|
|
60
|
-
dom.innerHTML = ''
|
|
61
|
-
if (mode === 'raw') {
|
|
62
|
-
const input = document.createElement('input')
|
|
63
|
-
input.type = 'text'
|
|
64
|
-
input.className = 'math-inline-raw'
|
|
65
|
-
input.value = '$' + text + '$'
|
|
66
|
-
input.dataset.math = text
|
|
67
|
-
dom.appendChild(input)
|
|
68
|
-
} else {
|
|
69
|
-
const katexLib = (window as any).katex
|
|
70
|
-
if (katexLib) {
|
|
71
|
-
try {
|
|
72
|
-
dom.innerHTML = katexLib.renderToString(text, { throwOnError: false, displayMode: false })
|
|
73
|
-
} catch {
|
|
74
|
-
dom.textContent = '$' + text + '$'
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
dom.textContent = '$' + text + '$'
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const mathInlineView = $view(mathInlineSchema, (_ctx): NodeViewConstructor => {
|
|
83
|
-
return (node, view, getPos) => {
|
|
84
|
-
const span = document.createElement('span')
|
|
85
|
-
span.className = 'math-inline'
|
|
86
|
-
renderMathInline(span, node.attrs.text as string, node.attrs.mode as string)
|
|
87
|
-
|
|
88
|
-
span.addEventListener('focusout', (e) => {
|
|
89
|
-
const target = e.target as HTMLElement
|
|
90
|
-
if (!target.classList.contains('math-inline-raw')) return
|
|
91
|
-
const rawValue = (target as HTMLInputElement).value
|
|
92
|
-
const m = rawValue.match(/^\$(.*)\$$/)
|
|
93
|
-
const newText = m ? m[1].trim() : rawValue
|
|
94
|
-
const pos = getPos()
|
|
95
|
-
if (pos != null && node.attrs.text !== newText) {
|
|
96
|
-
view.dispatch(view.state.tr.setNodeMarkup(pos, undefined, { ...node.attrs, text: newText }))
|
|
97
|
-
}
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
dom: span,
|
|
102
|
-
update: (updatedNode) => {
|
|
103
|
-
if (updatedNode.type !== node.type) return false
|
|
104
|
-
if (updatedNode.sameMarkup(node)) return true
|
|
105
|
-
node = updatedNode
|
|
106
|
-
renderMathInline(span, node.attrs.text as string, node.attrs.mode as string)
|
|
107
|
-
return true
|
|
108
|
-
},
|
|
109
|
-
stopEvent: (e) => {
|
|
110
|
-
return (e.target as HTMLElement).matches('input, textarea')
|
|
111
|
-
},
|
|
112
|
-
ignoreMutation: () => true,
|
|
113
|
-
destroy: () => { span.remove() },
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
const mathBlockSchema = $nodeSchema('math_block', () => ({
|
|
119
|
-
group: 'block',
|
|
120
|
-
atom: true,
|
|
121
|
-
priority: 60,
|
|
122
|
-
attrs: {
|
|
123
|
-
text: { default: '', validate: 'string' },
|
|
124
|
-
mode: { default: 'rendered' as 'rendered' | 'raw' },
|
|
125
|
-
},
|
|
126
|
-
parseDOM: [
|
|
127
|
-
{ tag: 'div.math-block', getAttrs: (dom) => ({ text: (dom as HTMLElement).dataset.math || '' }) },
|
|
128
|
-
],
|
|
129
|
-
toDOM: (node) => {
|
|
130
|
-
const div = document.createElement('div')
|
|
131
|
-
div.className = 'math-block'
|
|
132
|
-
div.dataset.math = node.attrs.text as string
|
|
133
|
-
const text = (node.attrs.text as string) || ''
|
|
134
|
-
if (node.attrs.mode === 'raw') {
|
|
135
|
-
div.innerHTML = '<pre class="math-raw" data-math="' + text + '">$$\n' + text + '\n$$</pre>'
|
|
136
|
-
return { dom: div }
|
|
137
|
-
}
|
|
138
|
-
const katexLib = (window as any).katex
|
|
139
|
-
if (katexLib && text) {
|
|
140
|
-
try {
|
|
141
|
-
div.innerHTML = katexLib.renderToString(text, { throwOnError: false, displayMode: true })
|
|
142
|
-
} catch {
|
|
143
|
-
div.textContent = '$$\n' + text + '\n$$'
|
|
144
|
-
}
|
|
145
|
-
} else {
|
|
146
|
-
div.textContent = '$$\n' + text + '\n$$'
|
|
147
|
-
}
|
|
148
|
-
return { dom: div }
|
|
149
|
-
},
|
|
150
|
-
parseMarkdown: {
|
|
151
|
-
match: (node) => node.type === 'math',
|
|
152
|
-
runner: (state, node, type) => {
|
|
153
|
-
state.addNode(type, { text: (node.value as string) || '' })
|
|
154
|
-
},
|
|
155
|
-
},
|
|
156
|
-
toMarkdown: {
|
|
157
|
-
match: (node) => node.type.name === 'math_block',
|
|
158
|
-
runner: (state, node) => {
|
|
159
|
-
state.addNode('math', undefined, node.attrs.text as string)
|
|
160
|
-
},
|
|
161
|
-
},
|
|
162
|
-
}))
|
|
163
|
-
|
|
164
|
-
function renderMathBlock(dom: HTMLElement, text: string, mode: string): void {
|
|
165
|
-
dom.dataset.math = text
|
|
166
|
-
dom.innerHTML = ''
|
|
167
|
-
if (mode === 'raw') {
|
|
168
|
-
const textarea = document.createElement('textarea')
|
|
169
|
-
textarea.className = 'math-block-raw'
|
|
170
|
-
textarea.dataset.math = text
|
|
171
|
-
textarea.value = '$$\n' + text + '\n$$'
|
|
172
|
-
textarea.rows = Math.min(10, (text.match(/\n/g) || []).length + 2)
|
|
173
|
-
dom.appendChild(textarea)
|
|
174
|
-
} else {
|
|
175
|
-
const katexLib = (window as any).katex
|
|
176
|
-
if (katexLib) {
|
|
177
|
-
try {
|
|
178
|
-
dom.innerHTML = katexLib.renderToString(text, { throwOnError: false, displayMode: true })
|
|
179
|
-
} catch {
|
|
180
|
-
dom.textContent = '$$\n' + text + '\n$$'
|
|
181
|
-
}
|
|
182
|
-
} else {
|
|
183
|
-
dom.textContent = '$$\n' + text + '\n$$'
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const mathBlockView = $view(mathBlockSchema, (_ctx): NodeViewConstructor => {
|
|
189
|
-
return (node, view, getPos) => {
|
|
190
|
-
const div = document.createElement('div')
|
|
191
|
-
div.className = 'math-block'
|
|
192
|
-
renderMathBlock(div, node.attrs.text as string, node.attrs.mode as string)
|
|
193
|
-
|
|
194
|
-
div.addEventListener('focusout', (e) => {
|
|
195
|
-
const target = e.target as HTMLElement
|
|
196
|
-
if (!target.classList.contains('math-block-raw')) return
|
|
197
|
-
const rawValue = (e.target as HTMLTextAreaElement).value
|
|
198
|
-
const newText = rawValue.replace(/^\$\$\s*\n?/, '').replace(/\n?\s*\$\$$/, '').trim()
|
|
199
|
-
const pos = getPos()
|
|
200
|
-
if (pos != null && node.attrs.text !== newText) {
|
|
201
|
-
view.dispatch(view.state.tr.setNodeMarkup(pos, undefined, { ...node.attrs, text: newText }))
|
|
202
|
-
}
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
return {
|
|
206
|
-
dom: div,
|
|
207
|
-
update: (updatedNode) => {
|
|
208
|
-
if (updatedNode.type !== node.type) return false
|
|
209
|
-
if (updatedNode.sameMarkup(node)) return true
|
|
210
|
-
node = updatedNode
|
|
211
|
-
renderMathBlock(div, node.attrs.text as string, node.attrs.mode as string)
|
|
212
|
-
return true
|
|
213
|
-
},
|
|
214
|
-
stopEvent: (e) => {
|
|
215
|
-
return (e.target as HTMLElement).matches('input, textarea')
|
|
216
|
-
},
|
|
217
|
-
ignoreMutation: () => true,
|
|
218
|
-
destroy: () => { div.remove() },
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
function getKatexCSS(): string {
|
|
224
|
-
const sheets: string[] = []
|
|
225
|
-
for (const sheet of document.styleSheets) {
|
|
226
|
-
try {
|
|
227
|
-
if (sheet.href && sheet.href.includes('katex')) {
|
|
228
|
-
const rules = []
|
|
229
|
-
for (const rule of sheet.cssRules) {
|
|
230
|
-
rules.push(rule.cssText)
|
|
231
|
-
}
|
|
232
|
-
if (rules.length) sheets.push(rules.join('\n'))
|
|
233
|
-
}
|
|
234
|
-
} catch { /* cross-origin sheets throw */ }
|
|
235
|
-
}
|
|
236
|
-
return sheets.join('\n')
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
function mathHtmlToPngDataUrl(element: HTMLElement, scale = 2): Promise<string> {
|
|
240
|
-
const w = element.offsetWidth + 32
|
|
241
|
-
const h = element.offsetHeight + 32
|
|
242
|
-
const canvas = document.createElement('canvas')
|
|
243
|
-
canvas.width = w * scale
|
|
244
|
-
canvas.height = h * scale
|
|
245
|
-
const ctx = canvas.getContext('2d')!
|
|
246
|
-
ctx.scale(scale, scale)
|
|
247
|
-
ctx.fillStyle = '#ffffff'
|
|
248
|
-
ctx.fillRect(0, 0, w, h)
|
|
249
|
-
const katexCSS = getKatexCSS()
|
|
250
|
-
const xml = new XMLSerializer().serializeToString(element)
|
|
251
|
-
const imgSrc = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(
|
|
252
|
-
`<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}">` +
|
|
253
|
-
`<style>${katexCSS}</style>` +
|
|
254
|
-
`<foreignObject width="100%" height="100%" style="background:#fff;padding:16px">` +
|
|
255
|
-
xml +
|
|
256
|
-
`</foreignObject></svg>`
|
|
257
|
-
)))
|
|
258
|
-
return new Promise((resolve, reject) => {
|
|
259
|
-
const img = document.createElement('img')
|
|
260
|
-
img.onload = () => {
|
|
261
|
-
ctx.drawImage(img, 16, 16)
|
|
262
|
-
resolve(canvas.toDataURL('image/png'))
|
|
263
|
-
}
|
|
264
|
-
img.onerror = () => reject(new Error('Failed to render equation to image'))
|
|
265
|
-
img.src = imgSrc
|
|
266
|
-
})
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export const mathPlugin: RendererPlugin = {
|
|
270
|
-
id: 'math',
|
|
271
|
-
name: 'Math Rendering',
|
|
272
|
-
enabled: true,
|
|
273
|
-
remarkPlugin: { plugin: remarkMath, options: undefined },
|
|
274
|
-
nodeTypes: ['math_inline', 'math_block'],
|
|
275
|
-
clipboardStyles: {
|
|
276
|
-
'.math-inline': 'display:inline;padding:2px 4px;border-radius:3px;background:rgba(175,184,193,0.2);',
|
|
277
|
-
'.math-block': 'display:block;padding:16px;margin:1em 0;border-radius:6px;background:#f6f8fa;text-align:center;overflow-x:auto;',
|
|
278
|
-
},
|
|
279
|
-
exportStyles: `.math-inline{display:inline;padding:2px 4px;border-radius:3px;background:var(--code-bg)}
|
|
280
|
-
.math-block{display:block;padding:16px;margin:1em 0;border-radius:6px;background:var(--code-block-bg);text-align:center;overflow-x:auto}`,
|
|
281
|
-
rawSelectors: ['.math-inline-raw', '.math-block-raw'],
|
|
282
|
-
bgCaptureSelectors: ['.math-inline', '.math-block'],
|
|
283
|
-
// TODO: KaTeX CSS 在 SVG foreignObject 中丢失,导致导出 PNG 二次内容/空白
|
|
284
|
-
// 后续需解决样式注入问题后重新启用
|
|
285
|
-
// exportCapabilities: [
|
|
286
|
-
// {
|
|
287
|
-
// label: 'Save Equation as PNG',
|
|
288
|
-
// defaultName: 'equation.png',
|
|
289
|
-
// filter: { name: 'PNG Image', extensions: ['png'] },
|
|
290
|
-
// execute: async (element: HTMLElement): Promise<string | null> => {
|
|
291
|
-
// if (element.classList.contains('math-block')) {
|
|
292
|
-
// return mathHtmlToPngDataUrl(element)
|
|
293
|
-
// }
|
|
294
|
-
// return null
|
|
295
|
-
// },
|
|
296
|
-
// },
|
|
297
|
-
// ],
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
registerPluginModule({
|
|
301
|
-
info: mathPlugin,
|
|
302
|
-
milkdownPlugins: [mathInlineSchema, mathBlockSchema, mathInlineView, mathBlockView],
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
export { mathInlineSchema, mathBlockSchema, mathInlineView, mathBlockView }
|