@zhenryx/react-editor 0.1.4 → 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 -200
- package/dist/index.cjs +10 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -4
- package/dist/index.d.mts +12 -4
- package/dist/index.mjs +10 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +62 -62
package/dist/index.d.cts
CHANGED
|
@@ -73,8 +73,10 @@ interface MarkdownEditorProps {
|
|
|
73
73
|
className?: string;
|
|
74
74
|
/** 自定义「代码」入口;提供后工具栏代码按钮直接打开外部统一插入弹窗 */
|
|
75
75
|
onInsertCodeRequest?: (request: CodeInsertRequest) => void;
|
|
76
|
+
/** HTML 预览 iframe 是否加 sandbox="allow-scripts",默认 true;Chrome 下可关掉换取交互 */
|
|
77
|
+
htmlPreviewSandbox?: boolean;
|
|
76
78
|
}
|
|
77
|
-
declare function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown, onSwitchToRichEditor, switchLabel, onSave, fileBaseName, features, initialView, placeholder, className, onInsertCodeRequest }: 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;
|
|
78
80
|
type MarkdownRichEditorMode = 'rich' | 'markdown';
|
|
79
81
|
interface MarkdownRichEditorProps extends Omit<RichTextEditorProps, 'onSwitchToMarkdown' | 'className' | 'switchLabel'> {
|
|
80
82
|
/** 受控模式;不传时使用 defaultMode 内部维护 */
|
|
@@ -97,12 +99,14 @@ interface MarkdownRichEditorProps extends Omit<RichTextEditorProps, 'onSwitchToM
|
|
|
97
99
|
renderRichMarkdown?: (markdown: string) => string;
|
|
98
100
|
/** 共用功能开关 */
|
|
99
101
|
features?: RichTextEditorFeatures;
|
|
102
|
+
/** Markdown 预览 iframe 是否加 sandbox,默认 true;Chrome 下可关掉换取交互 */
|
|
103
|
+
htmlPreviewSandbox?: boolean;
|
|
100
104
|
className?: string;
|
|
101
105
|
markdownClassName?: string;
|
|
102
106
|
richClassName?: string;
|
|
103
107
|
}
|
|
104
108
|
/** 普通编辑器 + Markdown 编辑器,一个组件内切换 */
|
|
105
|
-
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;
|
|
106
110
|
declare function defaultRenderMarkdown(markdown: string): string;
|
|
107
111
|
/**
|
|
108
112
|
* contentEditable HTML → Markdown(2026-11-16)
|
|
@@ -118,6 +122,10 @@ declare function safeRichHref(raw: string): string;
|
|
|
118
122
|
* 为 Markdown 渲染出的 HTML 代码块增加「源码 / 预览 / 复制 / 全屏 / 刷新」。
|
|
119
123
|
* 只处理 language-html / language-htm,普通代码块完全不动。
|
|
120
124
|
*/
|
|
121
|
-
|
|
122
|
-
|
|
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 };
|
|
123
131
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -73,8 +73,10 @@ interface MarkdownEditorProps {
|
|
|
73
73
|
className?: string;
|
|
74
74
|
/** 自定义「代码」入口;提供后工具栏代码按钮直接打开外部统一插入弹窗 */
|
|
75
75
|
onInsertCodeRequest?: (request: CodeInsertRequest) => void;
|
|
76
|
+
/** HTML 预览 iframe 是否加 sandbox="allow-scripts",默认 true;Chrome 下可关掉换取交互 */
|
|
77
|
+
htmlPreviewSandbox?: boolean;
|
|
76
78
|
}
|
|
77
|
-
declare function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown, onSwitchToRichEditor, switchLabel, onSave, fileBaseName, features, initialView, placeholder, className, onInsertCodeRequest }: 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;
|
|
78
80
|
type MarkdownRichEditorMode = 'rich' | 'markdown';
|
|
79
81
|
interface MarkdownRichEditorProps extends Omit<RichTextEditorProps, 'onSwitchToMarkdown' | 'className' | 'switchLabel'> {
|
|
80
82
|
/** 受控模式;不传时使用 defaultMode 内部维护 */
|
|
@@ -97,12 +99,14 @@ interface MarkdownRichEditorProps extends Omit<RichTextEditorProps, 'onSwitchToM
|
|
|
97
99
|
renderRichMarkdown?: (markdown: string) => string;
|
|
98
100
|
/** 共用功能开关 */
|
|
99
101
|
features?: RichTextEditorFeatures;
|
|
102
|
+
/** Markdown 预览 iframe 是否加 sandbox,默认 true;Chrome 下可关掉换取交互 */
|
|
103
|
+
htmlPreviewSandbox?: boolean;
|
|
100
104
|
className?: string;
|
|
101
105
|
markdownClassName?: string;
|
|
102
106
|
richClassName?: string;
|
|
103
107
|
}
|
|
104
108
|
/** 普通编辑器 + Markdown 编辑器,一个组件内切换 */
|
|
105
|
-
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;
|
|
106
110
|
declare function defaultRenderMarkdown(markdown: string): string;
|
|
107
111
|
/**
|
|
108
112
|
* contentEditable HTML → Markdown(2026-11-16)
|
|
@@ -118,6 +122,10 @@ declare function safeRichHref(raw: string): string;
|
|
|
118
122
|
* 为 Markdown 渲染出的 HTML 代码块增加「源码 / 预览 / 复制 / 全屏 / 刷新」。
|
|
119
123
|
* 只处理 language-html / language-htm,普通代码块完全不动。
|
|
120
124
|
*/
|
|
121
|
-
|
|
122
|
-
|
|
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 };
|
|
123
131
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -2409,7 +2409,7 @@ try { ${richRun.code.replace(/<\/script/gi, "<\\/script")} } catch (e) { send('e
|
|
|
2409
2409
|
* 为 Markdown 渲染出的 HTML 代码块增加「源码 / 预览 / 复制 / 全屏 / 刷新」。
|
|
2410
2410
|
* 只处理 language-html / language-htm,普通代码块完全不动。
|
|
2411
2411
|
*/
|
|
2412
|
-
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';";
|
|
2413
2413
|
function buildDoc(code) {
|
|
2414
2414
|
const meta = "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
|
|
2415
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>`;
|
|
@@ -2429,7 +2429,8 @@ function makeButton(label, className) {
|
|
|
2429
2429
|
function languageOf(code) {
|
|
2430
2430
|
return ((code.className || "").match(/language-([\w-]+)/)?.[1] || "").toLowerCase();
|
|
2431
2431
|
}
|
|
2432
|
-
function enhanceHtmlCodeBlocks(container) {
|
|
2432
|
+
function enhanceHtmlCodeBlocks(container, options = {}) {
|
|
2433
|
+
const useSandbox = options.sandbox !== false;
|
|
2433
2434
|
container.querySelectorAll("pre > code").forEach((code) => {
|
|
2434
2435
|
const pre = code.closest("pre");
|
|
2435
2436
|
if (!pre) return;
|
|
@@ -2470,7 +2471,7 @@ function enhanceHtmlCodeBlocks(container) {
|
|
|
2470
2471
|
if (iframe) return iframe;
|
|
2471
2472
|
iframe = document.createElement("iframe");
|
|
2472
2473
|
iframe.className = "kb-html-preview-frame";
|
|
2473
|
-
iframe.setAttribute("sandbox", "allow-scripts");
|
|
2474
|
+
if (useSandbox) iframe.setAttribute("sandbox", "allow-scripts");
|
|
2474
2475
|
iframe.setAttribute("title", "HTML 预览");
|
|
2475
2476
|
iframe.srcdoc = buildDoc(source);
|
|
2476
2477
|
preview.appendChild(iframe);
|
|
@@ -2513,7 +2514,7 @@ function enhanceHtmlCodeBlocks(container) {
|
|
|
2513
2514
|
const close = makeButton("关闭", "kb-html-action-btn");
|
|
2514
2515
|
bar.append(label, close);
|
|
2515
2516
|
fullFrame = document.createElement("iframe");
|
|
2516
|
-
fullFrame.setAttribute("sandbox", "allow-scripts");
|
|
2517
|
+
if (useSandbox) fullFrame.setAttribute("sandbox", "allow-scripts");
|
|
2517
2518
|
fullFrame.setAttribute("title", "HTML 全屏预览");
|
|
2518
2519
|
panel.append(bar, fullFrame);
|
|
2519
2520
|
fullMask.appendChild(panel);
|
|
@@ -2596,7 +2597,7 @@ function markdownHeadings(markdown) {
|
|
|
2596
2597
|
};
|
|
2597
2598
|
});
|
|
2598
2599
|
}
|
|
2599
|
-
function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = defaultRenderMarkdown, onSwitchToRichEditor, switchLabel = "使用普通编辑器", onSave, fileBaseName = "article", features, initialView = "split", placeholder = "# 从这里开始写 Markdown…", className = "", onInsertCodeRequest }) {
|
|
2600
|
+
function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = defaultRenderMarkdown, onSwitchToRichEditor, switchLabel = "使用普通编辑器", onSave, fileBaseName = "article", features, initialView = "split", placeholder = "# 从这里开始写 Markdown…", className = "", onInsertCodeRequest, htmlPreviewSandbox = true }) {
|
|
2600
2601
|
const feature = (key) => features?.[key] !== false;
|
|
2601
2602
|
const [view, setView] = useState(initialView);
|
|
2602
2603
|
const [uploading, setUploading] = useState(false);
|
|
@@ -2636,7 +2637,7 @@ function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = de
|
|
|
2636
2637
|
useEffect(() => {
|
|
2637
2638
|
const pv = previewRef.current;
|
|
2638
2639
|
if (!pv) return;
|
|
2639
|
-
const run = () => enhanceHtmlCodeBlocks(pv);
|
|
2640
|
+
const run = () => enhanceHtmlCodeBlocks(pv, { sandbox: htmlPreviewSandbox });
|
|
2640
2641
|
run();
|
|
2641
2642
|
const observer = new MutationObserver(run);
|
|
2642
2643
|
observer.observe(pv, {
|
|
@@ -2644,7 +2645,7 @@ function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = de
|
|
|
2644
2645
|
subtree: true
|
|
2645
2646
|
});
|
|
2646
2647
|
return () => observer.disconnect();
|
|
2647
|
-
}, []);
|
|
2648
|
+
}, [htmlPreviewSandbox]);
|
|
2648
2649
|
useEffect(() => {
|
|
2649
2650
|
const pv = previewRef.current;
|
|
2650
2651
|
if (!pv) return;
|
|
@@ -3380,7 +3381,7 @@ try { ${codeRun.code.replace(/<\/script/gi, "<\\/script")} } catch (e) { send('e
|
|
|
3380
3381
|
}
|
|
3381
3382
|
// src/MarkdownRichEditor.tsx
|
|
3382
3383
|
/** 普通编辑器 + Markdown 编辑器,一个组件内切换 */
|
|
3383
|
-
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 }) {
|
|
3384
3385
|
const [innerMode, setInnerMode] = useState(defaultMode);
|
|
3385
3386
|
const current = mode ?? innerMode;
|
|
3386
3387
|
function changeMode(next) {
|
|
@@ -3389,6 +3390,7 @@ function MarkdownRichEditor({ mode, defaultMode = "rich", onModeChange, markdown
|
|
|
3389
3390
|
}
|
|
3390
3391
|
if (current === "markdown") return /* @__PURE__ */ jsx(MarkdownEditor, {
|
|
3391
3392
|
...props,
|
|
3393
|
+
htmlPreviewSandbox,
|
|
3392
3394
|
className: markdownClassName,
|
|
3393
3395
|
initialView: markdownView,
|
|
3394
3396
|
switchLabel: markdownSwitchLabel,
|