Skip to content

Commit

Permalink
7.0.6: Support more types for safeIntanceOf (#395)
Browse files Browse the repository at this point in the history
* Fix #388 (2nd try)

* add test case

* 7.0.6: Support types in roosterjs-cross-window

* auto generate TargetWIndow type

* export TargetWindow
  • Loading branch information
JiuqingSong authored Mar 12, 2020
1 parent 4f6c9f1 commit 726d610
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "roosterjs",
"version": "7.10.5",
"version": "7.10.6",
"description": "Framework-independent javascript editor",
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions packages/roosterjs-cross-window/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
13 changes: 4 additions & 9 deletions packages/roosterjs-cross-window/lib/typeUtils/safeInstanceOf.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
168 changes: 168 additions & 0 deletions packages/roosterjs-cross-window/lib/types/TargetWindow.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion packages/roosterjs-cross-window/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"description": "Cross window utilities for roosterjs",
"dependencies": {},
"main": "./lib/index.ts",
"version": "1.0.1"
"version": "1.1.0"
}
46 changes: 46 additions & 0 deletions tools/generateTargetWindow.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 726d610

Please sign in to comment.