@zhenryx/react-editor 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +215 -169
- package/dist/index.cjs +142 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -5
- package/dist/index.d.mts +40 -5
- package/dist/index.mjs +142 -19
- package/dist/index.mjs.map +1 -1
- package/dist/style.css +3 -2
- package/package.json +62 -62
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* 统一「插入代码」的数据结构与序列化(2026-11-20)
|
|
4
|
+
*
|
|
5
|
+
* 工具栏只负责把当前选区交给外部弹窗;外部确定后回传 payload,
|
|
6
|
+
* 这里按当前编辑器模式生成 Markdown 或 contentEditable HTML。
|
|
7
|
+
*/
|
|
8
|
+
type CodeInsertMode = 'block' | 'file';
|
|
9
|
+
interface CodeInsertPayload {
|
|
10
|
+
/** 代码库片段 id;有值时按引用插入,不内联整段代码 */
|
|
11
|
+
refId?: number;
|
|
12
|
+
code: string;
|
|
13
|
+
language: string;
|
|
14
|
+
display: CodeInsertMode;
|
|
15
|
+
/** 新代码的文件名(可选) */
|
|
16
|
+
fileName?: string;
|
|
17
|
+
}
|
|
18
|
+
interface CodeInsertRequest {
|
|
19
|
+
mode: 'rich' | 'markdown';
|
|
20
|
+
/** 触发按钮时选中的文本,用于弹窗预填 */
|
|
21
|
+
selectedText: string;
|
|
22
|
+
/** 在触发按钮时的光标位置插入 */
|
|
23
|
+
insert: (payload: CodeInsertPayload) => void;
|
|
24
|
+
}
|
|
2
25
|
type RichTextEditorFeature = 'undo' | 'redo' | 'format' | 'bold' | 'italic' | 'strike' | 'list' | 'orderedList' | 'quote' | 'hr' | 'code' | 'table' | 'image' | 'link' | 'toc' | 'history' | 'color' | 'background' | 'underline' | 'align' | 'video' | 'formula' | 'template' | 'wide' | 'todo' | 'codeRun' | 'save' | 'sidebar' | 'outline' | 'syntax' | 'importMd' | 'exportMd' | 'more' | 'other';
|
|
3
26
|
type RichTextEditorFeatures = Partial<Record<RichTextEditorFeature, boolean>>;
|
|
4
27
|
interface RichTextEditorProps {
|
|
@@ -19,8 +42,10 @@ interface RichTextEditorProps {
|
|
|
19
42
|
className?: string;
|
|
20
43
|
/** 按按钮控制功能开关;不传 = 全部开启 */
|
|
21
44
|
features?: RichTextEditorFeatures;
|
|
45
|
+
/** 自定义「代码」入口;提供后工具栏代码按钮直接打开外部统一插入弹窗 */
|
|
46
|
+
onInsertCodeRequest?: (request: CodeInsertRequest) => void;
|
|
22
47
|
}
|
|
23
|
-
declare function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown, switchLabel, renderMarkdown, placeholder, className, features }: RichTextEditorProps): import("react").JSX.Element;
|
|
48
|
+
declare function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown, switchLabel, renderMarkdown, placeholder, className, features, onInsertCodeRequest }: RichTextEditorProps): import("react").JSX.Element;
|
|
24
49
|
type MarkdownEditorView = 'markdown' | 'split' | 'preview';
|
|
25
50
|
interface MarkdownEditorProps {
|
|
26
51
|
/** 当前内容(Markdown) */
|
|
@@ -46,8 +71,12 @@ interface MarkdownEditorProps {
|
|
|
46
71
|
/** 正文为空时的占位 */
|
|
47
72
|
placeholder?: string;
|
|
48
73
|
className?: string;
|
|
74
|
+
/** 自定义「代码」入口;提供后工具栏代码按钮直接打开外部统一插入弹窗 */
|
|
75
|
+
onInsertCodeRequest?: (request: CodeInsertRequest) => void;
|
|
76
|
+
/** HTML 预览 iframe 是否加 sandbox="allow-scripts",默认 true;Chrome 下可关掉换取交互 */
|
|
77
|
+
htmlPreviewSandbox?: boolean;
|
|
49
78
|
}
|
|
50
|
-
declare function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown, onSwitchToRichEditor, switchLabel, onSave, fileBaseName, features, initialView, placeholder, className }: MarkdownEditorProps): import("react").JSX.Element;
|
|
79
|
+
declare function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown, onSwitchToRichEditor, switchLabel, onSave, fileBaseName, features, initialView, placeholder, className, onInsertCodeRequest, htmlPreviewSandbox }: MarkdownEditorProps): import("react").JSX.Element;
|
|
51
80
|
type MarkdownRichEditorMode = 'rich' | 'markdown';
|
|
52
81
|
interface MarkdownRichEditorProps extends Omit<RichTextEditorProps, 'onSwitchToMarkdown' | 'className' | 'switchLabel'> {
|
|
53
82
|
/** 受控模式;不传时使用 defaultMode 内部维护 */
|
|
@@ -70,12 +99,14 @@ interface MarkdownRichEditorProps extends Omit<RichTextEditorProps, 'onSwitchToM
|
|
|
70
99
|
renderRichMarkdown?: (markdown: string) => string;
|
|
71
100
|
/** 共用功能开关 */
|
|
72
101
|
features?: RichTextEditorFeatures;
|
|
102
|
+
/** Markdown 预览 iframe 是否加 sandbox,默认 true;Chrome 下可关掉换取交互 */
|
|
103
|
+
htmlPreviewSandbox?: boolean;
|
|
73
104
|
className?: string;
|
|
74
105
|
markdownClassName?: string;
|
|
75
106
|
richClassName?: string;
|
|
76
107
|
}
|
|
77
108
|
/** 普通编辑器 + Markdown 编辑器,一个组件内切换 */
|
|
78
|
-
declare function MarkdownRichEditor({ mode, defaultMode, onModeChange, markdownView, markdownSwitchLabel, renderRichMarkdown, onSave, fileBaseName, richSwitchLabel, className, markdownClassName, richClassName, ...props }: MarkdownRichEditorProps): import("react").JSX.Element;
|
|
109
|
+
declare function MarkdownRichEditor({ mode, defaultMode, onModeChange, markdownView, markdownSwitchLabel, renderRichMarkdown, onSave, fileBaseName, richSwitchLabel, htmlPreviewSandbox, className, markdownClassName, richClassName, ...props }: MarkdownRichEditorProps): import("react").JSX.Element;
|
|
79
110
|
declare function defaultRenderMarkdown(markdown: string): string;
|
|
80
111
|
/**
|
|
81
112
|
* contentEditable HTML → Markdown(2026-11-16)
|
|
@@ -91,6 +122,10 @@ declare function safeRichHref(raw: string): string;
|
|
|
91
122
|
* 为 Markdown 渲染出的 HTML 代码块增加「源码 / 预览 / 复制 / 全屏 / 刷新」。
|
|
92
123
|
* 只处理 language-html / language-htm,普通代码块完全不动。
|
|
93
124
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
125
|
+
interface HtmlCodePreviewOptions {
|
|
126
|
+
/** 是否给预览 iframe 加 sandbox="allow-scripts",默认 true;关掉后脚本与宿主同源 */
|
|
127
|
+
sandbox?: boolean;
|
|
128
|
+
}
|
|
129
|
+
declare function enhanceHtmlCodeBlocks(container: HTMLElement, options?: HtmlCodePreviewOptions): void;
|
|
130
|
+
export { type CodeInsertMode, type CodeInsertPayload, type CodeInsertRequest, type HtmlCodePreviewOptions, MarkdownEditor, type MarkdownEditorProps, type MarkdownEditorView, MarkdownRichEditor, type MarkdownRichEditorMode, type MarkdownRichEditorProps, RichTextEditor, type RichTextEditorFeature, type RichTextEditorFeatures, type RichTextEditorProps, defaultRenderMarkdown, enhanceHtmlCodeBlocks, htmlToMarkdown, safeRichHref };
|
|
96
131
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* 统一「插入代码」的数据结构与序列化(2026-11-20)
|
|
4
|
+
*
|
|
5
|
+
* 工具栏只负责把当前选区交给外部弹窗;外部确定后回传 payload,
|
|
6
|
+
* 这里按当前编辑器模式生成 Markdown 或 contentEditable HTML。
|
|
7
|
+
*/
|
|
8
|
+
type CodeInsertMode = 'block' | 'file';
|
|
9
|
+
interface CodeInsertPayload {
|
|
10
|
+
/** 代码库片段 id;有值时按引用插入,不内联整段代码 */
|
|
11
|
+
refId?: number;
|
|
12
|
+
code: string;
|
|
13
|
+
language: string;
|
|
14
|
+
display: CodeInsertMode;
|
|
15
|
+
/** 新代码的文件名(可选) */
|
|
16
|
+
fileName?: string;
|
|
17
|
+
}
|
|
18
|
+
interface CodeInsertRequest {
|
|
19
|
+
mode: 'rich' | 'markdown';
|
|
20
|
+
/** 触发按钮时选中的文本,用于弹窗预填 */
|
|
21
|
+
selectedText: string;
|
|
22
|
+
/** 在触发按钮时的光标位置插入 */
|
|
23
|
+
insert: (payload: CodeInsertPayload) => void;
|
|
24
|
+
}
|
|
2
25
|
type RichTextEditorFeature = 'undo' | 'redo' | 'format' | 'bold' | 'italic' | 'strike' | 'list' | 'orderedList' | 'quote' | 'hr' | 'code' | 'table' | 'image' | 'link' | 'toc' | 'history' | 'color' | 'background' | 'underline' | 'align' | 'video' | 'formula' | 'template' | 'wide' | 'todo' | 'codeRun' | 'save' | 'sidebar' | 'outline' | 'syntax' | 'importMd' | 'exportMd' | 'more' | 'other';
|
|
3
26
|
type RichTextEditorFeatures = Partial<Record<RichTextEditorFeature, boolean>>;
|
|
4
27
|
interface RichTextEditorProps {
|
|
@@ -19,8 +42,10 @@ interface RichTextEditorProps {
|
|
|
19
42
|
className?: string;
|
|
20
43
|
/** 按按钮控制功能开关;不传 = 全部开启 */
|
|
21
44
|
features?: RichTextEditorFeatures;
|
|
45
|
+
/** 自定义「代码」入口;提供后工具栏代码按钮直接打开外部统一插入弹窗 */
|
|
46
|
+
onInsertCodeRequest?: (request: CodeInsertRequest) => void;
|
|
22
47
|
}
|
|
23
|
-
declare function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown, switchLabel, renderMarkdown, placeholder, className, features }: RichTextEditorProps): import("react").JSX.Element;
|
|
48
|
+
declare function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown, switchLabel, renderMarkdown, placeholder, className, features, onInsertCodeRequest }: RichTextEditorProps): import("react").JSX.Element;
|
|
24
49
|
type MarkdownEditorView = 'markdown' | 'split' | 'preview';
|
|
25
50
|
interface MarkdownEditorProps {
|
|
26
51
|
/** 当前内容(Markdown) */
|
|
@@ -46,8 +71,12 @@ interface MarkdownEditorProps {
|
|
|
46
71
|
/** 正文为空时的占位 */
|
|
47
72
|
placeholder?: string;
|
|
48
73
|
className?: string;
|
|
74
|
+
/** 自定义「代码」入口;提供后工具栏代码按钮直接打开外部统一插入弹窗 */
|
|
75
|
+
onInsertCodeRequest?: (request: CodeInsertRequest) => void;
|
|
76
|
+
/** HTML 预览 iframe 是否加 sandbox="allow-scripts",默认 true;Chrome 下可关掉换取交互 */
|
|
77
|
+
htmlPreviewSandbox?: boolean;
|
|
49
78
|
}
|
|
50
|
-
declare function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown, onSwitchToRichEditor, switchLabel, onSave, fileBaseName, features, initialView, placeholder, className }: MarkdownEditorProps): import("react").JSX.Element;
|
|
79
|
+
declare function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown, onSwitchToRichEditor, switchLabel, onSave, fileBaseName, features, initialView, placeholder, className, onInsertCodeRequest, htmlPreviewSandbox }: MarkdownEditorProps): import("react").JSX.Element;
|
|
51
80
|
type MarkdownRichEditorMode = 'rich' | 'markdown';
|
|
52
81
|
interface MarkdownRichEditorProps extends Omit<RichTextEditorProps, 'onSwitchToMarkdown' | 'className' | 'switchLabel'> {
|
|
53
82
|
/** 受控模式;不传时使用 defaultMode 内部维护 */
|
|
@@ -70,12 +99,14 @@ interface MarkdownRichEditorProps extends Omit<RichTextEditorProps, 'onSwitchToM
|
|
|
70
99
|
renderRichMarkdown?: (markdown: string) => string;
|
|
71
100
|
/** 共用功能开关 */
|
|
72
101
|
features?: RichTextEditorFeatures;
|
|
102
|
+
/** Markdown 预览 iframe 是否加 sandbox,默认 true;Chrome 下可关掉换取交互 */
|
|
103
|
+
htmlPreviewSandbox?: boolean;
|
|
73
104
|
className?: string;
|
|
74
105
|
markdownClassName?: string;
|
|
75
106
|
richClassName?: string;
|
|
76
107
|
}
|
|
77
108
|
/** 普通编辑器 + Markdown 编辑器,一个组件内切换 */
|
|
78
|
-
declare function MarkdownRichEditor({ mode, defaultMode, onModeChange, markdownView, markdownSwitchLabel, renderRichMarkdown, onSave, fileBaseName, richSwitchLabel, className, markdownClassName, richClassName, ...props }: MarkdownRichEditorProps): import("react").JSX.Element;
|
|
109
|
+
declare function MarkdownRichEditor({ mode, defaultMode, onModeChange, markdownView, markdownSwitchLabel, renderRichMarkdown, onSave, fileBaseName, richSwitchLabel, htmlPreviewSandbox, className, markdownClassName, richClassName, ...props }: MarkdownRichEditorProps): import("react").JSX.Element;
|
|
79
110
|
declare function defaultRenderMarkdown(markdown: string): string;
|
|
80
111
|
/**
|
|
81
112
|
* contentEditable HTML → Markdown(2026-11-16)
|
|
@@ -91,6 +122,10 @@ declare function safeRichHref(raw: string): string;
|
|
|
91
122
|
* 为 Markdown 渲染出的 HTML 代码块增加「源码 / 预览 / 复制 / 全屏 / 刷新」。
|
|
92
123
|
* 只处理 language-html / language-htm,普通代码块完全不动。
|
|
93
124
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
125
|
+
interface HtmlCodePreviewOptions {
|
|
126
|
+
/** 是否给预览 iframe 加 sandbox="allow-scripts",默认 true;关掉后脚本与宿主同源 */
|
|
127
|
+
sandbox?: boolean;
|
|
128
|
+
}
|
|
129
|
+
declare function enhanceHtmlCodeBlocks(container: HTMLElement, options?: HtmlCodePreviewOptions): void;
|
|
130
|
+
export { type CodeInsertMode, type CodeInsertPayload, type CodeInsertRequest, type HtmlCodePreviewOptions, MarkdownEditor, type MarkdownEditorProps, type MarkdownEditorView, MarkdownRichEditor, type MarkdownRichEditorMode, type MarkdownRichEditorProps, RichTextEditor, type RichTextEditorFeature, type RichTextEditorFeatures, type RichTextEditorProps, defaultRenderMarkdown, enhanceHtmlCodeBlocks, htmlToMarkdown, safeRichHref };
|
|
96
131
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -149,8 +149,23 @@ function preToMd(pre, langOverride = "") {
|
|
|
149
149
|
const code = pre.querySelector("code");
|
|
150
150
|
const text = (code || pre).textContent || "";
|
|
151
151
|
const lang = langOverride || code?.className.match(/language-([\w-]+)/)?.[1] || "";
|
|
152
|
+
const display = pre.getAttribute("data-kb-display") || "";
|
|
153
|
+
const refId = pre.getAttribute("data-kb-ref") || "";
|
|
154
|
+
if (refId) return `\n\n[[kbcode:${refId}${display === "file" ? ":file" : ""}]]\n\n`;
|
|
155
|
+
const info = [lang];
|
|
156
|
+
if (display === "block" || display === "file") info.push(`kbdisplay=${display}`);
|
|
157
|
+
const rawName = pre.getAttribute("data-kb-name") || "";
|
|
158
|
+
if (rawName) info.push(`kbname=${encodeURIComponent(rawName)}`);
|
|
152
159
|
const fence = text.includes("```") ? "~~~" : "```";
|
|
153
|
-
return `\n\n${fence}${
|
|
160
|
+
return `\n\n${fence}${info.filter(Boolean).join(" ")}\n${text.replace(/\n$/, "")}\n${fence}\n\n`;
|
|
161
|
+
}
|
|
162
|
+
function codeFileToMd(card) {
|
|
163
|
+
const codeEl = card.querySelector("code");
|
|
164
|
+
const code = codeEl?.textContent || "";
|
|
165
|
+
const lang = card.getAttribute("data-kb-lang") || codeEl?.className.match(/language-([\w-]+)/)?.[1] || "text";
|
|
166
|
+
const name = card.querySelector(".kb-code-file-meta b")?.textContent?.trim() || "";
|
|
167
|
+
const fence = code.includes("```") ? "~~~" : "```";
|
|
168
|
+
return `\n\n${fence}${lang} kbdisplay=file${name ? ` kbname=${encodeURIComponent(name)}` : ""}\n${code.replace(/\n$/, "")}\n${fence}\n\n`;
|
|
154
169
|
}
|
|
155
170
|
function blockToMd(el) {
|
|
156
171
|
switch (el.tagName) {
|
|
@@ -188,6 +203,7 @@ function nodeToMd(node) {
|
|
|
188
203
|
const annotation = el.querySelector("annotation[encoding=\"application/x-tex\"]")?.textContent?.trim() || "";
|
|
189
204
|
if (annotation) return `\n\n$$${annotation}$$\n\n`;
|
|
190
205
|
}
|
|
206
|
+
if (el.classList.contains("kb-code-file")) return codeFileToMd(el);
|
|
191
207
|
if (el.classList.contains("kb-code")) {
|
|
192
208
|
const pre = el.querySelector("pre");
|
|
193
209
|
const extra = Array.from(el.children).filter((child) => child.tagName !== "PRE" && !child.classList.contains("kb-code-lang")).map((child) => nodeToMd(child)).join("");
|
|
@@ -210,6 +226,35 @@ function safeRichHref(raw) {
|
|
|
210
226
|
if (/^\.\.?\//.test(url)) return escapeAttr(url);
|
|
211
227
|
return "";
|
|
212
228
|
}
|
|
229
|
+
// src/code-insert.ts
|
|
230
|
+
function escapeHtml$1(value) {
|
|
231
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
232
|
+
}
|
|
233
|
+
function languageOf$1(payload) {
|
|
234
|
+
return (payload.language || "text").trim().toLowerCase();
|
|
235
|
+
}
|
|
236
|
+
/** 代码库引用:[[kbcode:12]] / [[kbcode:12:file]] */
|
|
237
|
+
function codeInsertToken(payload) {
|
|
238
|
+
if (!payload.refId) return "";
|
|
239
|
+
return `[[kbcode:${payload.refId}${payload.display === "file" ? ":file" : ""}]]`;
|
|
240
|
+
}
|
|
241
|
+
/** Markdown 编辑器插入内容(前后留空行,避免粘在上一段后面) */
|
|
242
|
+
function codeInsertMarkdown(payload) {
|
|
243
|
+
if (payload.refId) return `\n\n${codeInsertToken(payload)}\n\n`;
|
|
244
|
+
const fence = payload.code.includes("```") ? "~~~" : "```";
|
|
245
|
+
const info = [languageOf$1(payload), `kbdisplay=${payload.display}`];
|
|
246
|
+
if (payload.fileName) info.push(`kbname=${encodeURIComponent(payload.fileName)}`);
|
|
247
|
+
return `\n\n${fence}${info.join(" ")}\n${payload.code.replace(/\n$/, "")}\n${fence}\n\n`;
|
|
248
|
+
}
|
|
249
|
+
/** 普通编辑器插入 HTML;data-* 会由 htmlToMarkdown 还原成上面的 Markdown */
|
|
250
|
+
function codeInsertHtml(payload) {
|
|
251
|
+
const attrs = [];
|
|
252
|
+
if (payload.refId) attrs.push(`data-kb-ref="${payload.refId}"`);
|
|
253
|
+
attrs.push(`data-kb-display="${payload.display}"`);
|
|
254
|
+
if (payload.fileName) attrs.push(`data-kb-name="${escapeHtml$1(payload.fileName)}"`);
|
|
255
|
+
const lang = languageOf$1(payload);
|
|
256
|
+
return `<pre ${attrs.join(" ")}><code class="language-${escapeHtml$1(lang)}">${escapeHtml$1(payload.code)}</code></pre><p><br></p>`;
|
|
257
|
+
}
|
|
213
258
|
// src/default-render.ts
|
|
214
259
|
function defaultRenderMarkdown(markdown) {
|
|
215
260
|
return markdown.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").split(/\n{2,}/).map((block) => `<p>${block.replace(/\n/g, "<br>")}</p>`).join("");
|
|
@@ -859,7 +904,7 @@ const TEMPLATES = [
|
|
|
859
904
|
markdown: "# 项目复盘\n\n## 目标\n\n## 做对了什么\n\n## 可以改进\n\n## 下一步\n"
|
|
860
905
|
}
|
|
861
906
|
];
|
|
862
|
-
function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown, switchLabel = "使用 Markdown 编辑器", renderMarkdown = defaultRenderMarkdown, placeholder = "开始写正文…", className = "", features }) {
|
|
907
|
+
function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown, switchLabel = "使用 Markdown 编辑器", renderMarkdown = defaultRenderMarkdown, placeholder = "开始写正文…", className = "", features, onInsertCodeRequest }) {
|
|
863
908
|
const feature = (key) => features?.[key] !== false;
|
|
864
909
|
const rootRef = useRef(null);
|
|
865
910
|
const [containerWidth, setContainerWidth] = useState(0);
|
|
@@ -1056,6 +1101,15 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
1056
1101
|
onChange(value);
|
|
1057
1102
|
setPopover(null);
|
|
1058
1103
|
}
|
|
1104
|
+
function requestCodeInsert() {
|
|
1105
|
+
const selectedText = window.getSelection()?.toString() || "";
|
|
1106
|
+
saveRange();
|
|
1107
|
+
onInsertCodeRequest?.({
|
|
1108
|
+
mode: "rich",
|
|
1109
|
+
selectedText,
|
|
1110
|
+
insert: (payload) => insertBlockHtml(codeInsertHtml(payload))
|
|
1111
|
+
});
|
|
1112
|
+
}
|
|
1059
1113
|
function openCodeDialog() {
|
|
1060
1114
|
const el = editorRef.current;
|
|
1061
1115
|
const sel = window.getSelection();
|
|
@@ -1078,7 +1132,7 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
1078
1132
|
setCodeOpen(true);
|
|
1079
1133
|
}
|
|
1080
1134
|
function insertCode() {
|
|
1081
|
-
|
|
1135
|
+
insertBlockHtml(`<pre><code class="language-${codeLang === "CSS/SCSS/LESS" ? "css" : codeLang.toLowerCase()}">${escapeHtml(codeText)}</code></pre><p><br></p>`);
|
|
1082
1136
|
setCodeOpen(false);
|
|
1083
1137
|
}
|
|
1084
1138
|
function runRichCode() {
|
|
@@ -1174,6 +1228,38 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
1174
1228
|
saveRange();
|
|
1175
1229
|
sync();
|
|
1176
1230
|
}
|
|
1231
|
+
/** 插入块级 HTML(代码块等):放在当前段落之后,避免 execCommand 把首行拆成行内 span */
|
|
1232
|
+
function insertBlockHtml(html) {
|
|
1233
|
+
const el = editorRef.current;
|
|
1234
|
+
if (!el) return;
|
|
1235
|
+
el.focus();
|
|
1236
|
+
restoreRange();
|
|
1237
|
+
const sel = window.getSelection();
|
|
1238
|
+
const range = sel?.rangeCount ? sel.getRangeAt(0) : null;
|
|
1239
|
+
if (range && !range.collapsed) range.deleteContents();
|
|
1240
|
+
let block = null;
|
|
1241
|
+
if (range) {
|
|
1242
|
+
const anchor = range.startContainer;
|
|
1243
|
+
block = (anchor instanceof Element ? anchor : anchor.parentElement)?.closest("p,div,li,h1,h2,h3,h4,h5,h6,blockquote,pre,figure,table") || null;
|
|
1244
|
+
if (block === el || block && !el.contains(block)) block = null;
|
|
1245
|
+
if (block?.tagName === "LI") block = block.closest("ul,ol") || block;
|
|
1246
|
+
}
|
|
1247
|
+
const template = document.createElement("template");
|
|
1248
|
+
template.innerHTML = html;
|
|
1249
|
+
const fragment = template.content;
|
|
1250
|
+
const tail = fragment.lastElementChild;
|
|
1251
|
+
if (block?.parentNode) block.parentNode.insertBefore(fragment, block.nextSibling);
|
|
1252
|
+
else el.appendChild(fragment);
|
|
1253
|
+
if (tail?.tagName === "P") {
|
|
1254
|
+
const next = document.createRange();
|
|
1255
|
+
next.setStart(tail, 0);
|
|
1256
|
+
next.collapse(true);
|
|
1257
|
+
sel?.removeAllRanges();
|
|
1258
|
+
sel?.addRange(next);
|
|
1259
|
+
}
|
|
1260
|
+
saveRange();
|
|
1261
|
+
sync();
|
|
1262
|
+
}
|
|
1177
1263
|
function onPaste(event) {
|
|
1178
1264
|
event.preventDefault();
|
|
1179
1265
|
const text = event.clipboardData.getData("text/plain");
|
|
@@ -1438,7 +1524,7 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
1438
1524
|
case "orderedList": return toolButton("有序", EditorIcons.ol, () => run("insertOrderedList", void 0, true));
|
|
1439
1525
|
case "quote": return toolButton("块引用", EditorIcons.quote, () => applyBlock("blockquote"));
|
|
1440
1526
|
case "hr": return toolButton("水平线", EditorIcons.hr, () => run("insertHorizontalRule"));
|
|
1441
|
-
case "code": return popoverTool("代码", EditorIcons.code, "code");
|
|
1527
|
+
case "code": return onInsertCodeRequest ? toolButton("代码", EditorIcons.code, requestCodeInsert) : popoverTool("代码", EditorIcons.code, "code");
|
|
1442
1528
|
case "table": return toolButton("表格", EditorIcons.table, insertTable);
|
|
1443
1529
|
case "image": return toolButton(busy ? "上传中" : "图像", EditorIcons.image, () => imageRef.current?.click());
|
|
1444
1530
|
case "history": return popoverTool("历史", EditorIcons.history, "history");
|
|
@@ -2051,9 +2137,9 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
2051
2137
|
]
|
|
2052
2138
|
}, command))
|
|
2053
2139
|
}) : null,
|
|
2054
|
-
popover.kind === "other" ? /* @__PURE__ */
|
|
2140
|
+
popover.kind === "other" ? /* @__PURE__ */ jsxs("div", {
|
|
2055
2141
|
className: "rre-popover-list",
|
|
2056
|
-
children: [
|
|
2142
|
+
children: [[
|
|
2057
2143
|
[
|
|
2058
2144
|
"倾斜",
|
|
2059
2145
|
"italic",
|
|
@@ -2087,7 +2173,17 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
2087
2173
|
children: EditorIcons.check
|
|
2088
2174
|
}) : null
|
|
2089
2175
|
]
|
|
2090
|
-
}, command))
|
|
2176
|
+
}, command)), onInsertCodeRequest && feature("code") ? /* @__PURE__ */ jsxs("button", {
|
|
2177
|
+
type: "button",
|
|
2178
|
+
onClick: () => {
|
|
2179
|
+
setPopover(null);
|
|
2180
|
+
runRichCode();
|
|
2181
|
+
},
|
|
2182
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
2183
|
+
className: "rre-popover-ico",
|
|
2184
|
+
children: EditorIcons.runCode
|
|
2185
|
+
}), /* @__PURE__ */ jsx("span", { children: "运行代码" })]
|
|
2186
|
+
}) : null]
|
|
2091
2187
|
}) : null,
|
|
2092
2188
|
popover.kind === "code" ? /* @__PURE__ */ jsxs("div", {
|
|
2093
2189
|
className: "rre-popover-list",
|
|
@@ -2313,11 +2409,15 @@ try { ${richRun.code.replace(/<\/script/gi, "<\\/script")} } catch (e) { send('e
|
|
|
2313
2409
|
* 为 Markdown 渲染出的 HTML 代码块增加「源码 / 预览 / 复制 / 全屏 / 刷新」。
|
|
2314
2410
|
* 只处理 language-html / language-htm,普通代码块完全不动。
|
|
2315
2411
|
*/
|
|
2316
|
-
const CSP = "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; img-src data: https: http:; font-src data: https: http:;";
|
|
2412
|
+
const CSP = "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; img-src data: https: http:; font-src data: https: http:; form-action 'none'; base-uri 'none';";
|
|
2317
2413
|
function buildDoc(code) {
|
|
2318
|
-
const
|
|
2319
|
-
|
|
2320
|
-
|
|
2414
|
+
const meta = "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
|
|
2415
|
+
const style = `<style>html,body{margin:0;padding:0;background:#fff;color:#111827;font:14px/1.7 -apple-system,BlinkMacSystemFont,'Segoe UI','PingFang SC','Microsoft YaHei',sans-serif}img{max-width:100%}a{color:#2563eb}</style>`;
|
|
2416
|
+
if (/<html[\s>]/i.test(code)) {
|
|
2417
|
+
if (/<head([^>]*)>/i.test(code)) return code.replace(/<head([^>]*)>/i, `<head$1>${meta}<meta http-equiv="Content-Security-Policy" content="${CSP}">${style}`);
|
|
2418
|
+
return code.replace(/<html([^>]*)>/i, `<html$1><head>${meta}<meta http-equiv="Content-Security-Policy" content="${CSP}">${style}</head>`);
|
|
2419
|
+
}
|
|
2420
|
+
return `<!doctype html><html><head><meta charset="utf-8">${meta}<meta http-equiv="Content-Security-Policy" content="${CSP}">${style}</head><body>${code}</body></html>`;
|
|
2321
2421
|
}
|
|
2322
2422
|
function makeButton(label, className) {
|
|
2323
2423
|
const button = document.createElement("button");
|
|
@@ -2329,7 +2429,8 @@ function makeButton(label, className) {
|
|
|
2329
2429
|
function languageOf(code) {
|
|
2330
2430
|
return ((code.className || "").match(/language-([\w-]+)/)?.[1] || "").toLowerCase();
|
|
2331
2431
|
}
|
|
2332
|
-
function enhanceHtmlCodeBlocks(container) {
|
|
2432
|
+
function enhanceHtmlCodeBlocks(container, options = {}) {
|
|
2433
|
+
const useSandbox = options.sandbox !== false;
|
|
2333
2434
|
container.querySelectorAll("pre > code").forEach((code) => {
|
|
2334
2435
|
const pre = code.closest("pre");
|
|
2335
2436
|
if (!pre) return;
|
|
@@ -2370,7 +2471,7 @@ function enhanceHtmlCodeBlocks(container) {
|
|
|
2370
2471
|
if (iframe) return iframe;
|
|
2371
2472
|
iframe = document.createElement("iframe");
|
|
2372
2473
|
iframe.className = "kb-html-preview-frame";
|
|
2373
|
-
iframe.setAttribute("sandbox", "allow-scripts");
|
|
2474
|
+
if (useSandbox) iframe.setAttribute("sandbox", "allow-scripts");
|
|
2374
2475
|
iframe.setAttribute("title", "HTML 预览");
|
|
2375
2476
|
iframe.srcdoc = buildDoc(source);
|
|
2376
2477
|
preview.appendChild(iframe);
|
|
@@ -2413,7 +2514,7 @@ function enhanceHtmlCodeBlocks(container) {
|
|
|
2413
2514
|
const close = makeButton("关闭", "kb-html-action-btn");
|
|
2414
2515
|
bar.append(label, close);
|
|
2415
2516
|
fullFrame = document.createElement("iframe");
|
|
2416
|
-
fullFrame.setAttribute("sandbox", "allow-scripts");
|
|
2517
|
+
if (useSandbox) fullFrame.setAttribute("sandbox", "allow-scripts");
|
|
2417
2518
|
fullFrame.setAttribute("title", "HTML 全屏预览");
|
|
2418
2519
|
panel.append(bar, fullFrame);
|
|
2419
2520
|
fullMask.appendChild(panel);
|
|
@@ -2496,7 +2597,7 @@ function markdownHeadings(markdown) {
|
|
|
2496
2597
|
};
|
|
2497
2598
|
});
|
|
2498
2599
|
}
|
|
2499
|
-
function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = defaultRenderMarkdown, onSwitchToRichEditor, switchLabel = "使用普通编辑器", onSave, fileBaseName = "article", features, initialView = "split", placeholder = "# 从这里开始写 Markdown…", className = "" }) {
|
|
2600
|
+
function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = defaultRenderMarkdown, onSwitchToRichEditor, switchLabel = "使用普通编辑器", onSave, fileBaseName = "article", features, initialView = "split", placeholder = "# 从这里开始写 Markdown…", className = "", onInsertCodeRequest, htmlPreviewSandbox = true }) {
|
|
2500
2601
|
const feature = (key) => features?.[key] !== false;
|
|
2501
2602
|
const [view, setView] = useState(initialView);
|
|
2502
2603
|
const [uploading, setUploading] = useState(false);
|
|
@@ -2536,7 +2637,7 @@ function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = de
|
|
|
2536
2637
|
useEffect(() => {
|
|
2537
2638
|
const pv = previewRef.current;
|
|
2538
2639
|
if (!pv) return;
|
|
2539
|
-
const run = () => enhanceHtmlCodeBlocks(pv);
|
|
2640
|
+
const run = () => enhanceHtmlCodeBlocks(pv, { sandbox: htmlPreviewSandbox });
|
|
2540
2641
|
run();
|
|
2541
2642
|
const observer = new MutationObserver(run);
|
|
2542
2643
|
observer.observe(pv, {
|
|
@@ -2544,7 +2645,7 @@ function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = de
|
|
|
2544
2645
|
subtree: true
|
|
2545
2646
|
});
|
|
2546
2647
|
return () => observer.disconnect();
|
|
2547
|
-
}, []);
|
|
2648
|
+
}, [htmlPreviewSandbox]);
|
|
2548
2649
|
useEffect(() => {
|
|
2549
2650
|
const pv = previewRef.current;
|
|
2550
2651
|
if (!pv) return;
|
|
@@ -2576,6 +2677,27 @@ function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = de
|
|
|
2576
2677
|
window.addEventListener("message", onMessage);
|
|
2577
2678
|
return () => window.removeEventListener("message", onMessage);
|
|
2578
2679
|
}, []);
|
|
2680
|
+
function requestCodeInsert() {
|
|
2681
|
+
const el = textareaRef.current;
|
|
2682
|
+
const start = el?.selectionStart ?? markdown.length;
|
|
2683
|
+
const end = el?.selectionEnd ?? markdown.length;
|
|
2684
|
+
const selectedText = markdown.slice(start, end);
|
|
2685
|
+
onInsertCodeRequest?.({
|
|
2686
|
+
mode: "markdown",
|
|
2687
|
+
selectedText,
|
|
2688
|
+
insert: (payload) => {
|
|
2689
|
+
const block = codeInsertMarkdown(payload);
|
|
2690
|
+
onChange(markdown.slice(0, start) + block + markdown.slice(end));
|
|
2691
|
+
window.requestAnimationFrame(() => {
|
|
2692
|
+
const next = textareaRef.current;
|
|
2693
|
+
if (!next) return;
|
|
2694
|
+
next.focus();
|
|
2695
|
+
const caret = start + block.length;
|
|
2696
|
+
next.setSelectionRange(caret, caret);
|
|
2697
|
+
});
|
|
2698
|
+
}
|
|
2699
|
+
});
|
|
2700
|
+
}
|
|
2579
2701
|
function insertAtCursor(before, after = "", fallback = "文本") {
|
|
2580
2702
|
const el = textareaRef.current;
|
|
2581
2703
|
const start = el?.selectionStart ?? markdown.length;
|
|
@@ -2872,7 +2994,7 @@ try { ${codeRun.code.replace(/<\/script/gi, "<\\/script")} } catch (e) { send('e
|
|
|
2872
2994
|
feature("strike") ? mdTool("删除线", EditorIcons.strike, () => insertAtCursor("~~", "~~", "删除线")) : null,
|
|
2873
2995
|
/* @__PURE__ */ jsx("span", { className: "rre-md-sep" }),
|
|
2874
2996
|
feature("list") ? menuButton("list", "列表", EditorIcons.ul) : null,
|
|
2875
|
-
feature("code") ? menuButton("code", "代码块", EditorIcons.code) : null,
|
|
2997
|
+
feature("code") ? onInsertCodeRequest ? mdTool("代码", EditorIcons.code, requestCodeInsert) : menuButton("code", "代码块", EditorIcons.code) : null,
|
|
2876
2998
|
feature("quote") ? menuButton("more-format", "更多格式") : null,
|
|
2877
2999
|
/* @__PURE__ */ jsx("span", { className: "rre-md-sep" }),
|
|
2878
3000
|
feature("image") ? mdTool(uploading ? "上传中" : "图片", EditorIcons.image, () => imageRef.current?.click()) : null,
|
|
@@ -3259,7 +3381,7 @@ try { ${codeRun.code.replace(/<\/script/gi, "<\\/script")} } catch (e) { send('e
|
|
|
3259
3381
|
}
|
|
3260
3382
|
// src/MarkdownRichEditor.tsx
|
|
3261
3383
|
/** 普通编辑器 + Markdown 编辑器,一个组件内切换 */
|
|
3262
|
-
function MarkdownRichEditor({ mode, defaultMode = "rich", onModeChange, markdownView, markdownSwitchLabel = "使用普通编辑器", renderRichMarkdown, onSave, fileBaseName, richSwitchLabel = "使用 Markdown 编辑器", className = "", markdownClassName = "", richClassName = "", ...props }) {
|
|
3384
|
+
function MarkdownRichEditor({ mode, defaultMode = "rich", onModeChange, markdownView, markdownSwitchLabel = "使用普通编辑器", renderRichMarkdown, onSave, fileBaseName, richSwitchLabel = "使用 Markdown 编辑器", htmlPreviewSandbox, className = "", markdownClassName = "", richClassName = "", ...props }) {
|
|
3263
3385
|
const [innerMode, setInnerMode] = useState(defaultMode);
|
|
3264
3386
|
const current = mode ?? innerMode;
|
|
3265
3387
|
function changeMode(next) {
|
|
@@ -3268,6 +3390,7 @@ function MarkdownRichEditor({ mode, defaultMode = "rich", onModeChange, markdown
|
|
|
3268
3390
|
}
|
|
3269
3391
|
if (current === "markdown") return /* @__PURE__ */ jsx(MarkdownEditor, {
|
|
3270
3392
|
...props,
|
|
3393
|
+
htmlPreviewSandbox,
|
|
3271
3394
|
className: markdownClassName,
|
|
3272
3395
|
initialView: markdownView,
|
|
3273
3396
|
switchLabel: markdownSwitchLabel,
|