diff --git a/package.json b/package.json index 83e268b4920..8b7bfbd479a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "roosterjs", - "version": "7.10.5", + "version": "7.10.6", "description": "Framework-independent javascript editor", "repository": { "type": "git", diff --git a/packages/roosterjs-cross-window/lib/index.ts b/packages/roosterjs-cross-window/lib/index.ts index 4be253a0bae..21d2189136f 100644 --- a/packages/roosterjs-cross-window/lib/index.ts +++ b/packages/roosterjs-cross-window/lib/index.ts @@ -5,3 +5,5 @@ export { default as isHTMLTableCellElement } from './typeUtils/isHTMLTableCellEl export { default as isHTMLTableElement } from './typeUtils/isHTMLTableElement'; export { default as isNode } from './typeUtils/isNode'; export { default as isRange } from './typeUtils/isRange'; +export { default as safeInstanceOf } from './typeUtils/safeInstanceOf'; +export { default as TargetWindow } from './types/TargetWindow'; diff --git a/packages/roosterjs-cross-window/lib/typeUtils/safeInstanceOf.ts b/packages/roosterjs-cross-window/lib/typeUtils/safeInstanceOf.ts index 1e3823d2bc1..830c1a8094a 100644 --- a/packages/roosterjs-cross-window/lib/typeUtils/safeInstanceOf.ts +++ b/packages/roosterjs-cross-window/lib/typeUtils/safeInstanceOf.ts @@ -1,12 +1,7 @@ -export interface TargetWindow { - Node: Node; - HTMLElement: HTMLElement; - HTMLOListElement: HTMLOListElement; - HTMLTableElement: HTMLTableElement; - HTMLTableCellElement: HTMLTableCellElement; - Range: Range; - DocumentFragment: DocumentFragment; -} +import TargetWindow from '../types/TargetWindow'; + +// NOTE: Type TargetWindow is an auto-generated type. +// Run node ./tools/generateTargetWindow.js to generate it. /** * Try get window from the given node or range diff --git a/packages/roosterjs-cross-window/lib/types/TargetWindow.ts b/packages/roosterjs-cross-window/lib/types/TargetWindow.ts new file mode 100644 index 00000000000..7fdc8164b51 --- /dev/null +++ b/packages/roosterjs-cross-window/lib/types/TargetWindow.ts @@ -0,0 +1,168 @@ +/** + * Define DOM types of window, used by safeInstanceOf() to check if a given object is of the specified type of its own window + * + * !!!Note: This file is generated from /tools/generateTargetWindow.js. Do NOT modify this file directly!!! + */ +export default interface TargetWindow { + Range: Range; + Node: Node; + Element: Element; + SVGElement: SVGElement; + SVGViewElement: SVGViewElement; + SVGTitleElement: SVGTitleElement; + SVGSymbolElement: SVGSymbolElement; + SVGStyleElement: SVGStyleElement; + SVGStopElement: SVGStopElement; + SVGScriptElement: SVGScriptElement; + SVGPatternElement: SVGPatternElement; + SVGMetadataElement: SVGMetadataElement; + SVGMaskElement: SVGMaskElement; + SVGMarkerElement: SVGMarkerElement; + SVGGraphicsElement: SVGGraphicsElement; + SVGUseElement: SVGUseElement; + SVGTextContentElement: SVGTextContentElement; + SVGTextPositioningElement: SVGTextPositioningElement; + SVGTextElement: SVGTextElement; + SVGTSpanElement: SVGTSpanElement; + SVGTextPathElement: SVGTextPathElement; + SVGSwitchElement: SVGSwitchElement; + SVGSVGElement: SVGSVGElement; + SVGPathElement: SVGPathElement; + SVGImageElement: SVGImageElement; + SVGGeometryElement: SVGGeometryElement; + SVGRectElement: SVGRectElement; + SVGPolylineElement: SVGPolylineElement; + SVGPolygonElement: SVGPolygonElement; + SVGLineElement: SVGLineElement; + SVGEllipseElement: SVGEllipseElement; + SVGCircleElement: SVGCircleElement; + SVGGElement: SVGGElement; + SVGForeignObjectElement: SVGForeignObjectElement; + SVGDefsElement: SVGDefsElement; + SVGClipPathElement: SVGClipPathElement; + SVGAElement: SVGAElement; + SVGGradientElement: SVGGradientElement; + SVGRadialGradientElement: SVGRadialGradientElement; + SVGLinearGradientElement: SVGLinearGradientElement; + SVGFilterElement: SVGFilterElement; + SVGFETurbulenceElement: SVGFETurbulenceElement; + SVGFETileElement: SVGFETileElement; + SVGFESpotLightElement: SVGFESpotLightElement; + SVGFESpecularLightingElement: SVGFESpecularLightingElement; + SVGFEPointLightElement: SVGFEPointLightElement; + SVGFEOffsetElement: SVGFEOffsetElement; + SVGFEMorphologyElement: SVGFEMorphologyElement; + SVGFEMergeNodeElement: SVGFEMergeNodeElement; + SVGFEMergeElement: SVGFEMergeElement; + SVGFEImageElement: SVGFEImageElement; + SVGFEGaussianBlurElement: SVGFEGaussianBlurElement; + SVGFEFloodElement: SVGFEFloodElement; + SVGFEDropShadowElement: SVGFEDropShadowElement; + SVGFEDistantLightElement: SVGFEDistantLightElement; + SVGFEDisplacementMapElement: SVGFEDisplacementMapElement; + SVGFEDiffuseLightingElement: SVGFEDiffuseLightingElement; + SVGFEConvolveMatrixElement: SVGFEConvolveMatrixElement; + SVGFECompositeElement: SVGFECompositeElement; + SVGFEComponentTransferElement: SVGFEComponentTransferElement; + SVGFEColorMatrixElement: SVGFEColorMatrixElement; + SVGFEBlendElement: SVGFEBlendElement; + SVGDescElement: SVGDescElement; + SVGCursorElement: SVGCursorElement; + SVGComponentTransferFunctionElement: SVGComponentTransferFunctionElement; + SVGFEFuncRElement: SVGFEFuncRElement; + SVGFEFuncGElement: SVGFEFuncGElement; + SVGFEFuncBElement: SVGFEFuncBElement; + SVGFEFuncAElement: SVGFEFuncAElement; + SVGAnimationElement: SVGAnimationElement; + SVGAnimateTransformElement: SVGAnimateTransformElement; + SVGAnimateMotionElement: SVGAnimateMotionElement; + SVGAnimateElement: SVGAnimateElement; + HTMLElement: HTMLElement; + HTMLUnknownElement: HTMLUnknownElement; + HTMLUListElement: HTMLUListElement; + HTMLTrackElement: HTMLTrackElement; + HTMLTitleElement: HTMLTitleElement; + HTMLTimeElement: HTMLTimeElement; + HTMLTextAreaElement: HTMLTextAreaElement; + HTMLTemplateElement: HTMLTemplateElement; + HTMLTableSectionElement: HTMLTableSectionElement; + HTMLTableRowElement: HTMLTableRowElement; + HTMLTableElement: HTMLTableElement; + HTMLTableColElement: HTMLTableColElement; + HTMLTableCellElement: HTMLTableCellElement; + HTMLTableHeaderCellElement: HTMLTableHeaderCellElement; + HTMLTableDataCellElement: HTMLTableDataCellElement; + HTMLTableCaptionElement: HTMLTableCaptionElement; + HTMLStyleElement: HTMLStyleElement; + HTMLSpanElement: HTMLSpanElement; + HTMLSourceElement: HTMLSourceElement; + HTMLSlotElement: HTMLSlotElement; + HTMLSelectElement: HTMLSelectElement; + HTMLScriptElement: HTMLScriptElement; + HTMLQuoteElement: HTMLQuoteElement; + HTMLProgressElement: HTMLProgressElement; + HTMLPreElement: HTMLPreElement; + HTMLPictureElement: HTMLPictureElement; + HTMLParamElement: HTMLParamElement; + HTMLParagraphElement: HTMLParagraphElement; + HTMLOutputElement: HTMLOutputElement; + HTMLOptionElement: HTMLOptionElement; + HTMLOptGroupElement: HTMLOptGroupElement; + HTMLObjectElement: HTMLObjectElement; + HTMLOListElement: HTMLOListElement; + HTMLModElement: HTMLModElement; + HTMLMeterElement: HTMLMeterElement; + HTMLMetaElement: HTMLMetaElement; + HTMLMenuElement: HTMLMenuElement; + HTMLMediaElement: HTMLMediaElement; + HTMLVideoElement: HTMLVideoElement; + HTMLAudioElement: HTMLAudioElement; + HTMLMarqueeElement: HTMLMarqueeElement; + HTMLMapElement: HTMLMapElement; + HTMLLinkElement: HTMLLinkElement; + HTMLLegendElement: HTMLLegendElement; + HTMLLabelElement: HTMLLabelElement; + HTMLLIElement: HTMLLIElement; + HTMLInputElement: HTMLInputElement; + HTMLImageElement: HTMLImageElement; + HTMLIFrameElement: HTMLIFrameElement; + HTMLHtmlElement: HTMLHtmlElement; + HTMLHeadingElement: HTMLHeadingElement; + HTMLHeadElement: HTMLHeadElement; + HTMLHRElement: HTMLHRElement; + HTMLFrameSetElement: HTMLFrameSetElement; + HTMLFrameElement: HTMLFrameElement; + HTMLFormElement: HTMLFormElement; + HTMLFontElement: HTMLFontElement; + HTMLFieldSetElement: HTMLFieldSetElement; + HTMLEmbedElement: HTMLEmbedElement; + HTMLDivElement: HTMLDivElement; + HTMLDirectoryElement: HTMLDirectoryElement; + HTMLDialogElement: HTMLDialogElement; + HTMLDetailsElement: HTMLDetailsElement; + HTMLDataListElement: HTMLDataListElement; + HTMLDataElement: HTMLDataElement; + HTMLDListElement: HTMLDListElement; + HTMLCanvasElement: HTMLCanvasElement; + HTMLButtonElement: HTMLButtonElement; + HTMLBodyElement: HTMLBodyElement; + HTMLBaseFontElement: HTMLBaseFontElement; + HTMLBaseElement: HTMLBaseElement; + HTMLBRElement: HTMLBRElement; + HTMLAreaElement: HTMLAreaElement; + HTMLAppletElement: HTMLAppletElement; + HTMLAnchorElement: HTMLAnchorElement; + DocumentType: DocumentType; + DocumentFragment: DocumentFragment; + ShadowRoot: ShadowRoot; + Document: Document; + XMLDocument: XMLDocument; + HTMLDocument: HTMLDocument; + ChildNode: ChildNode; + CharacterData: CharacterData; + Text: Text; + CDATASection: CDATASection; + ProcessingInstruction: ProcessingInstruction; + Comment: Comment; + Attr: Attr; +} diff --git a/packages/roosterjs-cross-window/package.json b/packages/roosterjs-cross-window/package.json index ef4f614383d..afbda17e006 100644 --- a/packages/roosterjs-cross-window/package.json +++ b/packages/roosterjs-cross-window/package.json @@ -3,5 +3,5 @@ "description": "Cross window utilities for roosterjs", "dependencies": {}, "main": "./lib/index.ts", - "version": "1.0.1" + "version": "1.1.0" } diff --git a/tools/generateTargetWindow.js b/tools/generateTargetWindow.js new file mode 100644 index 00000000000..42eefa1a158 --- /dev/null +++ b/tools/generateTargetWindow.js @@ -0,0 +1,46 @@ +const fs = require('fs'); +const path = require('path'); +const libDomFilePath = path.join(__dirname, '../node_modules/typescript/lib/lib.dom.d.ts'); +const crossWindowPath = path.join(__dirname, '../packages/roosterjs-cross-window'); +const targetPath = path.join(crossWindowPath, 'lib/types/TargetWindow.ts'); +const packageJsonPath = path.join(crossWindowPath, 'package.json'); + +if (!fs.existsSync(libDomFilePath)) { + console.error(`Cannot find source file: ${libDomFilePath}`); + return 1; +} + +const source = fs.readFileSync(libDomFilePath).toString(); +const search = ['Node', 'Range']; + +let result = '/**\n'; +result += + ' * Define DOM types of window, used by safeInstanceOf() to check if a given object is of the specified type of its own window\n'; +result += ' *\n'; +result += + ' * !!!Note: This file is generated from /tools/generateTargetWindow.js. Do NOT modify this file directly!!!\n'; +result += ' */\n'; +result += 'export default interface TargetWindow {\n'; + +while (search.length > 0) { + const name = search.pop(); + result += ` ${name}: ${name};\n`; + const reg = new RegExp(`^interface (\\w+) extends\\s?${name}( \\{$|,.*$)`, 'gm'); + let match; + + while ((match = reg.exec(source)) != null) { + search.push(match[1]); + } +} + +result += '}\n'; + +try { + fs.writeFileSync(targetPath, result); + console.log( + `\nSuccessfully generated target file ${targetPath}.\n\nRemember to change package version in ${packageJsonPath}.` + ); +} catch (e) { + console.error(e); + return 1; +}