Skip to content

Commit 2cdd1d3

Browse files
author
Knight Chen
committed
feat: 支持微前端 iframe 沙箱
1 parent f491c00 commit 2cdd1d3

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

packages/core/src/editor/dom-editor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import $, {
3636
walkTextNodes,
3737
} from '../utils/dom'
3838
import { IS_CHROME, IS_FIREFOX } from '../utils/ua'
39+
import { isDocument, isShadowRoot } from '../utils/check'
3940

4041
/**
4142
* 自定义全局 command
@@ -122,7 +123,7 @@ export const DomEditor = {
122123
const el = DomEditor.toDOMNode(editor, editor)
123124
const root = el.getRootNode()
124125

125-
if ((root instanceof Document || root instanceof ShadowRoot) && root.getSelection != null) {
126+
if ((isDocument(root) || isShadowRoot(root)) && Reflect.get(root, 'getSelection') != null) {
126127
return root
127128
}
128129
return el.ownerDocument

packages/core/src/editor/plugins/with-content.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export const withContent = <T extends Editor>(editor: T) => {
370370
* 重置 HTML 内容
371371
* @param html html string
372372
*/
373-
e.setHtml = (html: string = '') => {
373+
e.setHtml = (html: string | null = '') => {
374374
// 记录编辑器当前状态
375375
const isEditorDisabled = e.isDisabled()
376376
const isEditorFocused = e.isFocused()
@@ -384,7 +384,7 @@ export const withContent = <T extends Editor>(editor: T) => {
384384
// https://github.com/wangeditor-team/wangEditor/issues/4754
385385
e.clear()
386386
// 设置新内容
387-
const newContent = htmlToContent(e, html)
387+
const newContent = htmlToContent(e, html == null ? '' : html)
388388
Transforms.insertFragment(e, newContent)
389389

390390
// 恢复编辑器状态和选区

packages/core/src/text-area/event-handlers/beforeInput.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { hasEditableTarget } from '../helpers'
1111
import { DOMStaticRange } from '../../utils/dom'
1212
import { HAS_BEFORE_INPUT_SUPPORT } from '../../utils/ua'
1313
import { EDITOR_TO_CAN_PASTE } from '../../utils/weak-maps'
14+
import { isDataTransfer } from '../../utils/check'
1415

1516
// 补充 beforeInput event 的属性
1617
interface BeforeInputEventType {
@@ -138,7 +139,7 @@ function handleBeforeInput(e: Event, textarea: TextArea, editor: IDomEditor) {
138139
if (!EDITOR_TO_CAN_PASTE.get(editor)) break // 不可默认粘贴
139140
}
140141

141-
if (data instanceof DataTransfer) {
142+
if (isDataTransfer(data)) {
142143
// 这里处理非纯文本(如 html 图片文件等)的粘贴。对于纯文本的粘贴,使用 paste 事件
143144
editor.insertData(data)
144145
} else if (typeof data === 'string') {

packages/core/src/utils/check.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const toString = (val: unknown): string => Object.prototype.toString.call(val)
2+
3+
export function isDocument(val: unknown): val is Document {
4+
return toString(val) === '[object HTMLDocument]'
5+
}
6+
7+
export function isShadowRoot(val: any): val is ShadowRoot {
8+
return toString(val) === '[object ShadowRoot]'
9+
}
10+
11+
export function isDataTransfer(val: any): val is DataTransfer {
12+
return toString(val) === '[object DataTransfer]'
13+
}
14+
15+
export function isSelection(val: any): val is Selection {
16+
return toString(val) === '[object Selection]'
17+
}
18+
19+
export function isHTMLElement(val: any): val is HTMLElement {
20+
return !!val && Reflect.get(val, 'nodeType') === Node.ELEMENT_NODE
21+
}

packages/core/src/utils/dom.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import DOMText = globalThis.Text
8080
import DOMRange = globalThis.Range
8181
import DOMSelection = globalThis.Selection
8282
import DOMStaticRange = globalThis.StaticRange
83+
import { isHTMLElement, isSelection } from './check'
8384
export { DOMNode, DOMComment, DOMElement, DOMText, DOMRange, DOMSelection, DOMStaticRange }
8485

8586
export type DOMPoint = [Node, number]
@@ -109,20 +110,15 @@ export const isDOMElement = (value: any): value is DOMElement => {
109110
* Check if a value is a DOM node.
110111
*/
111112
export const isDOMNode = (value: any): value is DOMNode => {
112-
const window = getDefaultView(value)
113-
return (
114-
!!window &&
115-
// @ts-ignore
116-
value instanceof window.Node
117-
)
113+
return value != null && typeof value.nodeType === 'number'
118114
}
119115

120116
/**
121117
* Check if a value is a DOM selection.
122118
*/
123119
export const isDOMSelection = (value: any): value is DOMSelection => {
124120
const window = value && value.anchorNode && getDefaultView(value.anchorNode)
125-
return !!window && value instanceof window.Selection
121+
return !!window && isSelection(value)
126122
}
127123

128124
/**
@@ -343,7 +339,7 @@ export function walkTextNodes(
343339
handler: (textNode: DOMNode, parent: DOMElement) => void
344340
) {
345341
// void elem 内部的 text 不处理
346-
if (elem instanceof HTMLElement && elem.dataset.slateVoid === 'true') return
342+
if (isHTMLElement(elem) && elem.dataset.slateVoid === 'true') return
347343

348344
for (let nodes = elem.childNodes, i = nodes.length; i--; ) {
349345
const node = nodes[i]

0 commit comments

Comments
 (0)