@bytechain.cn/colamd 1.5.1-beta.2 → 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 +194 -162
- package/README.md +148 -5
- package/README_CN.md +163 -13
- 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 +107 -37
- package/dist/preload/index.js +3 -2
- package/dist/renderer/assets/{arc-CPdeInCG.js → arc-CpIo2PoE.js} +1 -1
- package/dist/renderer/assets/{architectureDiagram-3BPJPVTR-BAbnaR9G.js → architectureDiagram-3BPJPVTR-T-4Cq0Bz.js} +2 -2
- package/dist/renderer/assets/{blockDiagram-GPEHLZMM-CYSWjnJg.js → blockDiagram-GPEHLZMM-C-SuR6eu.js} +4 -4
- package/dist/renderer/assets/{c4Diagram-AAUBKEIU-Rb1tstnr.js → c4Diagram-AAUBKEIU-PJp81cGm.js} +2 -2
- package/dist/renderer/assets/{channel-DpG2A6fE.js → channel-CtIHaaRI.js} +1 -1
- package/dist/renderer/assets/{chunk-2J33WTMH-DFc0Jxy_.js → chunk-2J33WTMH-BrchfQL6.js} +1 -1
- package/dist/renderer/assets/{chunk-4BX2VUAB-BhRxDTNn.js → chunk-4BX2VUAB-BCc3Ev_H.js} +1 -1
- package/dist/renderer/assets/{chunk-55IACEB6-DEgMVBk8.js → chunk-55IACEB6-B2M27HQo.js} +1 -1
- package/dist/renderer/assets/{chunk-727SXJPM-bjBIfiz8.js → chunk-727SXJPM-DXDHGECs.js} +5 -5
- package/dist/renderer/assets/{chunk-AQP2D5EJ-DwQMzTzD.js → chunk-AQP2D5EJ-BNb1lOQY.js} +3 -3
- package/dist/renderer/assets/{chunk-FMBD7UC4-CkphwJzs.js → chunk-FMBD7UC4-CzgfEH2z.js} +1 -1
- package/dist/renderer/assets/{chunk-ND2GUHAM-oU09z4y4.js → chunk-ND2GUHAM-BUFT_yOe.js} +1 -1
- package/dist/renderer/assets/{chunk-QZHKN3VN-rCbVuPBn.js → chunk-QZHKN3VN-C-zV7oBi.js} +1 -1
- package/dist/renderer/assets/{classDiagram-v2-Q7XG4LA2-DGS2faoM.js → classDiagram-4FO5ZUOK-CAyhGSvf.js} +6 -6
- package/dist/renderer/assets/{classDiagram-4FO5ZUOK-DGS2faoM.js → classDiagram-v2-Q7XG4LA2-CAyhGSvf.js} +6 -6
- package/dist/renderer/assets/{cose-bilkent-S5V4N54A-iqY6-EwA.js → cose-bilkent-S5V4N54A-ldu81kCk.js} +1 -1
- package/dist/renderer/assets/{dagre-BM42HDAG-5t3X5sDa.js → dagre-BM42HDAG-NJgy8CTw.js} +3 -3
- package/dist/renderer/assets/{diagram-2AECGRRQ-DzHiYDPh.js → diagram-2AECGRRQ-CuIcMpOn.js} +2 -2
- package/dist/renderer/assets/{diagram-5GNKFQAL-BiNv6Keq.js → diagram-5GNKFQAL-BJwFCroB.js} +3 -3
- package/dist/renderer/assets/{diagram-KO2AKTUF-ClzeDG6f.js → diagram-KO2AKTUF-C7NgQ8ju.js} +2 -2
- package/dist/renderer/assets/{diagram-LMA3HP47-CGkw7wII.js → diagram-LMA3HP47-B71v6MOq.js} +2 -2
- package/dist/renderer/assets/{diagram-OG6HWLK6-Dl-Hyk1_.js → diagram-OG6HWLK6-D2ZxkrSe.js} +3 -3
- package/dist/renderer/assets/{erDiagram-TEJ5UH35-BxUN79Qb.js → erDiagram-TEJ5UH35-4ToW0p3h.js} +4 -4
- package/dist/renderer/assets/{flowDiagram-I6XJVG4X-CzFk-KNI.js → flowDiagram-I6XJVG4X-DBkZLqwn.js} +6 -6
- package/dist/renderer/assets/{ganttDiagram-6RSMTGT7-C2xl6Igx.js → ganttDiagram-6RSMTGT7-G3xD2VXj.js} +2 -2
- package/dist/renderer/assets/{gitGraphDiagram-PVQCEYII-_fn7XCa7.js → gitGraphDiagram-PVQCEYII-BcTNiFfg.js} +3 -3
- package/dist/renderer/assets/{graph-CDoHYrHm.js → graph--Vhl4p4D.js} +1 -1
- package/dist/renderer/assets/{index-DAlXyxzt.js → index-C63bU7CF.js} +693 -162
- package/dist/renderer/assets/{index-CeFpoCKV.css → index-D8nA981z.css} +17 -6
- package/dist/renderer/assets/{infoDiagram-5YYISTIA-DL6XIxLz.js → infoDiagram-5YYISTIA-DjKa7Yv-.js} +1 -1
- package/dist/renderer/assets/{ishikawaDiagram-YF4QCWOH-BUZLjRo-.js → ishikawaDiagram-YF4QCWOH-xIWzW1pL.js} +1 -1
- package/dist/renderer/assets/{journeyDiagram-JHISSGLW-C4rH_mQM.js → journeyDiagram-JHISSGLW-DxMBkVOF.js} +4 -4
- package/dist/renderer/assets/{kanban-definition-UN3LZRKU-DRbrBcWV.js → kanban-definition-UN3LZRKU-DqJeL6E1.js} +2 -2
- package/dist/renderer/assets/{layout-DZl4n4qu.js → layout-B3giiMoK.js} +2 -2
- package/dist/renderer/assets/{linear-B0Krxg21.js → linear-Cqh3vVIf.js} +1 -1
- package/dist/renderer/assets/{mindmap-definition-RKZ34NQL-DdmPsWrn.js → mindmap-definition-RKZ34NQL-DUay02ma.js} +3 -3
- package/dist/renderer/assets/{pieDiagram-4H26LBE5-BPZLqwG0.js → pieDiagram-4H26LBE5-CVcfLtky.js} +3 -3
- package/dist/renderer/assets/{quadrantDiagram-W4KKPZXB-Dr-oWRk9.js → quadrantDiagram-W4KKPZXB-wDHffPOs.js} +2 -2
- package/dist/renderer/assets/{requirementDiagram-4Y6WPE33-B6QZd0lo.js → requirementDiagram-4Y6WPE33-C4pgGDGv.js} +3 -3
- package/dist/renderer/assets/{sankeyDiagram-5OEKKPKP-Cyl9ojEt.js → sankeyDiagram-5OEKKPKP-Jsf0rRMo.js} +1 -1
- package/dist/renderer/assets/{sequenceDiagram-3UESZ5HK-D48Yr9T6.js → sequenceDiagram-3UESZ5HK-e6QS7gt8.js} +3 -3
- package/dist/renderer/assets/{stateDiagram-AJRCARHV-qyb8ETsa.js → stateDiagram-AJRCARHV-DgYKZzDq.js} +6 -6
- package/dist/renderer/assets/{stateDiagram-v2-BHNVJYJU-DmDOyyrJ.js → stateDiagram-v2-BHNVJYJU-CUdmd_50.js} +4 -4
- package/dist/renderer/assets/{timeline-definition-PNZ67QCA-C-KQxTi1.js → timeline-definition-PNZ67QCA-S12e1Mp_.js} +2 -2
- package/dist/renderer/assets/{vennDiagram-CIIHVFJN-BdaZlnH-.js → vennDiagram-CIIHVFJN-CNDBG6A2.js} +1 -1
- package/dist/renderer/assets/{wardleyDiagram-YWT4CUSO-B2hBE-EE.js → wardleyDiagram-YWT4CUSO-CLlGQcof.js} +2 -2
- package/dist/renderer/assets/{xychartDiagram-2RQKCTM6-CSvswDTY.js → xychartDiagram-2RQKCTM6-CUtsHUQd.js} +2 -2
- package/dist/renderer/index.html +6 -2
- package/package.json +25 -1
- package/themes/README.md +18 -1
- package/themes/academic-paper-pt.css +1321 -0
- package/themes/academic-paper.css +192 -97
- 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/bytechain.cn-colamd-1.5.1-beta.2.tgz +0 -0
- package/capacitor.config.js +0 -29
- package/capacitor.config.ts +0 -30
- package/demo.md +0 -193
- package/docs/academic-demo.md +0 -566
- 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 -658
- 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 -905
- 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 -68
- package/src/renderer/editor/plugins/math-plugin.ts +0 -297
- 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 -670
- 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 -92
- package/src/renderer/env.d.ts +0 -10
- package/src/renderer/index.html +0 -76
- package/src/renderer/main.ts +0 -678
- package/src/renderer/mobile.css +0 -429
- 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 } 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
|
-
doc.querySelectorAll('.math-inline').forEach((el) => {
|
|
58
|
-
;(el as HTMLElement).setAttribute('style', 'display:inline;padding:2px 4px;border-radius:3px;background:rgba(175,184,193,0.2);')
|
|
59
|
-
})
|
|
60
|
-
doc.querySelectorAll('.math-block').forEach((el) => {
|
|
61
|
-
;(el as HTMLElement).setAttribute('style', 'display:block;padding:16px;margin:1em 0;border-radius:6px;background:#f6f8fa;text-align:center;overflow-x:auto;')
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
doc.querySelectorAll('.mermaid-block').forEach((el) => {
|
|
65
|
-
;(el as HTMLElement).setAttribute('style', 'display:block;padding:16px;margin:1em 0;border-radius:6px;background:#f6f8fa;border:1px solid #d0d7de;')
|
|
66
|
-
})
|
|
67
|
-
doc.querySelectorAll('.mermaid-preview svg').forEach((el) => {
|
|
68
|
-
;(el as HTMLElement).setAttribute('style', 'max-width:100%;height:auto;')
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
e.clipboardData?.setData('text/html', doc.body.innerHTML)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const defaultContent = `# Welcome to ColaMD\n\nStart typing here...\n`
|
|
75
|
-
|
|
76
|
-
export async function createEditor(
|
|
77
|
-
rootId: string,
|
|
78
|
-
onChange?: (markdown: string) => void
|
|
79
|
-
): Promise<Editor> {
|
|
80
|
-
const root = document.getElementById(rootId)
|
|
81
|
-
if (!root) throw new Error(`Element #${rootId} not found`)
|
|
82
|
-
|
|
83
|
-
const pluginModules = getAllPluginModules()
|
|
84
|
-
|
|
85
|
-
let builder = Editor.make()
|
|
86
|
-
.config((ctx) => {
|
|
87
|
-
ctx.set(rootCtx, root)
|
|
88
|
-
ctx.set(defaultValueCtx, defaultContent)
|
|
89
|
-
ctx.set(remarkPluginsCtx, [
|
|
90
|
-
...pluginModules.map((m) => m.info.remarkPlugin),
|
|
91
|
-
{ plugin: remarkBreaks, options: undefined },
|
|
92
|
-
] as any)
|
|
93
|
-
if (onChange) {
|
|
94
|
-
ctx.get(listenerCtx).markdownUpdated((_ctx, markdown) => {
|
|
95
|
-
onChange(markdown)
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
})
|
|
99
|
-
.use(commonmark)
|
|
100
|
-
.use(gfm)
|
|
101
|
-
.use(history)
|
|
102
|
-
.use(listener)
|
|
103
|
-
.use(clipboard)
|
|
104
|
-
.use(htmlView)
|
|
105
|
-
|
|
106
|
-
for (const mod of pluginModules) {
|
|
107
|
-
for (const p of mod.milkdownPlugins) {
|
|
108
|
-
builder = builder.use(p)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
editorInstance = await builder.create()
|
|
114
|
-
} catch (e: any) {
|
|
115
|
-
if (e?.message?.includes('math_inline') || e?.message?.includes('math_block')) {
|
|
116
|
-
console.warn('[editor] Milkdown context error, retrying without math plugins:', e.message)
|
|
117
|
-
const filteredModules = pluginModules.filter((m) => m.info.id !== 'math')
|
|
118
|
-
builder = Editor.make()
|
|
119
|
-
.config((ctx) => {
|
|
120
|
-
ctx.set(rootCtx, root)
|
|
121
|
-
ctx.set(defaultValueCtx, defaultContent)
|
|
122
|
-
ctx.set(remarkPluginsCtx, [
|
|
123
|
-
...filteredModules.map((m) => m.info.remarkPlugin),
|
|
124
|
-
{ plugin: remarkBreaks, options: undefined },
|
|
125
|
-
] as any)
|
|
126
|
-
if (onChange) ctx.get(listenerCtx).markdownUpdated((_ctx, markdown) => { onChange(markdown) })
|
|
127
|
-
})
|
|
128
|
-
.use(commonmark)
|
|
129
|
-
.use(gfm)
|
|
130
|
-
.use(history)
|
|
131
|
-
.use(listener)
|
|
132
|
-
.use(clipboard)
|
|
133
|
-
.use(htmlView)
|
|
134
|
-
for (const mod of filteredModules) {
|
|
135
|
-
for (const p of mod.milkdownPlugins) {
|
|
136
|
-
builder = builder.use(p)
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
editorInstance = await builder.create()
|
|
140
|
-
} else {
|
|
141
|
-
throw e
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
editorInstance.action((ctx) => {
|
|
146
|
-
const nvs = ctx.get(nodeViewCtx)
|
|
147
|
-
const schema = ctx.get(schemaCtx)
|
|
148
|
-
const fixed = nvs.map((nv: any, i: number) => {
|
|
149
|
-
if (nv[0] != null) return nv
|
|
150
|
-
const viewFn = nv[1]
|
|
151
|
-
const fallbackNames = ['math_inline', 'math_block', 'mermaid_block']
|
|
152
|
-
const name = fallbackNames[i - 1]
|
|
153
|
-
if (name && schema.nodes[name]) {
|
|
154
|
-
return [name, viewFn]
|
|
155
|
-
}
|
|
156
|
-
return nv
|
|
157
|
-
})
|
|
158
|
-
ctx.set(nodeViewCtx, fixed)
|
|
159
|
-
|
|
160
|
-
const oldView = ctx.get(editorViewCtx)
|
|
161
|
-
const rootEl = ctx.get(rootCtx) as HTMLElement
|
|
162
|
-
const nodeViews = Object.fromEntries(fixed)
|
|
163
|
-
|
|
164
|
-
const newView = new EditorView(rootEl, {
|
|
165
|
-
state: oldView.state,
|
|
166
|
-
nodeViews,
|
|
167
|
-
dispatchTransaction: oldView.props.dispatchTransaction!,
|
|
168
|
-
})
|
|
169
|
-
oldView.destroy()
|
|
170
|
-
rootEl.appendChild(newView.dom)
|
|
171
|
-
ctx.set(editorViewCtx, newView)
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
root.addEventListener('copy', enhanceClipboard)
|
|
175
|
-
root.addEventListener('cut', enhanceClipboard)
|
|
176
|
-
|
|
177
|
-
/*
|
|
178
|
-
* REMOVED: Custom IME composition handlers for mobile CJK input.
|
|
179
|
-
* ProseMirror handles IME composition natively via its DOM observer and input rules.
|
|
180
|
-
* These handlers dispatched synthetic input events that interfered with ProseMirror's
|
|
181
|
-
* internal state tracking, causing duplicated characters and broken undo history.
|
|
182
|
-
*
|
|
183
|
-
* Original code:
|
|
184
|
-
* if (/android|iphone|ipad/i.test(navigator.userAgent)) {
|
|
185
|
-
* root.addEventListener('compositionstart', () => { ... })
|
|
186
|
-
* root.addEventListener('compositionend', () => { ... })
|
|
187
|
-
* }
|
|
188
|
-
*/
|
|
189
|
-
|
|
190
|
-
root.addEventListener('click', (e) => {
|
|
191
|
-
if (!(e.metaKey || e.ctrlKey)) return
|
|
192
|
-
const link = (e.target as HTMLElement).closest('a')
|
|
193
|
-
if (!link) return
|
|
194
|
-
const href = link.getAttribute('href')
|
|
195
|
-
if (href) {
|
|
196
|
-
e.preventDefault()
|
|
197
|
-
window.electronAPI?.openExternal(href)
|
|
198
|
-
window.capacitorAPI?.openExternal(href)
|
|
199
|
-
}
|
|
200
|
-
})
|
|
201
|
-
|
|
202
|
-
return editorInstance
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export function getMarkdown(): string {
|
|
206
|
-
if (!editorInstance) return ''
|
|
207
|
-
let markdown = ''
|
|
208
|
-
editorInstance.action((ctx) => {
|
|
209
|
-
const serializer = ctx.get(serializerCtx)
|
|
210
|
-
const view = ctx.get(editorViewCtx)
|
|
211
|
-
markdown = serializer(view.state.doc)
|
|
212
|
-
})
|
|
213
|
-
return markdown
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Get HTML by cloning the live rendered DOM.
|
|
218
|
-
* Unlike {@link getHTML} which uses DOMSerializer (creating fresh nodes with
|
|
219
|
-
* "Rendering..." placeholders), this captures the actual rendered SVGs
|
|
220
|
-
* from mermaid, KaTeX math, etc. Used for PDF/HTML export.
|
|
221
|
-
*/
|
|
222
|
-
export function getLiveHTML(): string {
|
|
223
|
-
const editorDom = document.querySelector('#editor .ProseMirror')
|
|
224
|
-
if (!editorDom) return ''
|
|
225
|
-
const clone = editorDom.cloneNode(true) as HTMLElement
|
|
226
|
-
|
|
227
|
-
// Inline computed background colors so export preserves exact editor appearance.
|
|
228
|
-
// CSS variables from the export template may not match the editor's cascade.
|
|
229
|
-
const styleTargets: Array<{ selector: string; prop: string }> = [
|
|
230
|
-
{ selector: '.math-inline', prop: 'backgroundColor' },
|
|
231
|
-
{ selector: '.math-block', prop: 'backgroundColor' },
|
|
232
|
-
{ selector: '.mermaid-block', prop: 'backgroundColor' },
|
|
233
|
-
]
|
|
234
|
-
for (const { selector, prop } of styleTargets) {
|
|
235
|
-
const originals = editorDom.querySelectorAll(selector)
|
|
236
|
-
const clones = clone.querySelectorAll(selector)
|
|
237
|
-
originals.forEach((orig, i) => {
|
|
238
|
-
const computed = getComputedStyle(orig)
|
|
239
|
-
const val = computed.getPropertyValue(prop === 'backgroundColor' ? 'background-color' : prop)
|
|
240
|
-
if (val && clones[i]) (clones[i] as HTMLElement).style.setProperty(prop, val)
|
|
241
|
-
})
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Remove source-mode textareas
|
|
245
|
-
clone.querySelectorAll('textarea.mermaid-source, .math-inline-raw, .math-block-raw')
|
|
246
|
-
.forEach(el => el.remove())
|
|
247
|
-
// Hide loading/error placeholders
|
|
248
|
-
clone.querySelectorAll('.mermaid-loading, .mermaid-error')
|
|
249
|
-
.forEach(el => (el as HTMLElement).style.display = 'none')
|
|
250
|
-
return clone.innerHTML
|
|
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,68 +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
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface PluginModule {
|
|
29
|
-
info: RendererPlugin
|
|
30
|
-
milkdownPlugins: MilkdownPlugin[]
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const modules: PluginModule[] = []
|
|
34
|
-
|
|
35
|
-
export function registerPluginModule(m: PluginModule): void {
|
|
36
|
-
modules.push(m)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function getAllPluginModules(): PluginModule[] {
|
|
40
|
-
return modules
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function getAllPlugins(): RendererPlugin[] {
|
|
44
|
-
return modules.map((m) => m.info)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function findPluginBySelector(selector: string): RendererPlugin | undefined {
|
|
48
|
-
const plugins = getAllPlugins()
|
|
49
|
-
if (selector.startsWith('node:')) {
|
|
50
|
-
const name = selector.slice(5)
|
|
51
|
-
return plugins.find((p) => p.nodeTypes?.includes(name))
|
|
52
|
-
}
|
|
53
|
-
return plugins.find((p) => p.id === selector)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function findExportCapabilities(className: string): ExportCapability[] {
|
|
57
|
-
for (const m of modules) {
|
|
58
|
-
if (m.info.enabled && m.info.exportCapabilities && m.info.nodeTypes?.some(nt => className.includes(nt.replace(/_/g, '-')))) {
|
|
59
|
-
return m.info.exportCapabilities
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return []
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function togglePlugin(id: string, enabled: boolean): void {
|
|
66
|
-
const mod = modules.find((m) => m.info.id === id)
|
|
67
|
-
if (mod) mod.info.enabled = enabled
|
|
68
|
-
}
|
|
@@ -1,297 +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
|
-
// TODO: KaTeX CSS 在 SVG foreignObject 中丢失,导致导出 PNG 二次内容/空白
|
|
276
|
-
// 后续需解决样式注入问题后重新启用
|
|
277
|
-
// exportCapabilities: [
|
|
278
|
-
// {
|
|
279
|
-
// label: 'Save Equation as PNG',
|
|
280
|
-
// defaultName: 'equation.png',
|
|
281
|
-
// filter: { name: 'PNG Image', extensions: ['png'] },
|
|
282
|
-
// execute: async (element: HTMLElement): Promise<string | null> => {
|
|
283
|
-
// if (element.classList.contains('math-block')) {
|
|
284
|
-
// return mathHtmlToPngDataUrl(element)
|
|
285
|
-
// }
|
|
286
|
-
// return null
|
|
287
|
-
// },
|
|
288
|
-
// },
|
|
289
|
-
// ],
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
registerPluginModule({
|
|
293
|
-
info: mathPlugin,
|
|
294
|
-
milkdownPlugins: [mathInlineSchema, mathBlockSchema, mathInlineView, mathBlockView],
|
|
295
|
-
})
|
|
296
|
-
|
|
297
|
-
export { mathInlineSchema, mathBlockSchema, mathInlineView, mathBlockView }
|