Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid access document or window in server side #4101

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/x6-common/src/dom/matrix.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PointLike } from '../types'
import { createSvgElement } from './elem'

const svgDocument = createSvgElement('svg') as SVGSVGElement
const transformRegex = /(\w+)\(([^,)]+),?([^)]+)?\)/gi
const transformSeparatorRegex = /[ ,]+/
const transformationListRegex = /^(\w+)\((.*)\)/
Expand Down Expand Up @@ -36,6 +35,7 @@ export interface Scale {
* @see https://developer.mozilla.org/en/docs/Web/API/SVGPoint
*/
export function createSVGPoint(x: number, y: number) {
const svgDocument = createSvgElement('svg') as SVGSVGElement
const p = svgDocument.createSVGPoint()
p.x = x
p.y = y
Expand All @@ -58,6 +58,7 @@ export function createSVGPoint(x: number, y: number) {
* @see https://developer.mozilla.org/en/docs/Web/API/SVGMatrix
*/
export function createSVGMatrix(matrix?: DOMMatrix | MatrixLike | null) {
const svgDocument = createSvgElement('svg') as SVGSVGElement
const mat = svgDocument.createSVGMatrix()
if (matrix != null) {
const source = matrix as any
Expand All @@ -75,6 +76,7 @@ export function createSVGMatrix(matrix?: DOMMatrix | MatrixLike | null) {
* @see https://developer.mozilla.org/en/docs/Web/API/SVGTransform
*/
export function createSVGTransform(matrix?: DOMMatrix | MatrixLike) {
const svgDocument = createSvgElement('svg') as SVGSVGElement
if (matrix != null) {
if (!(matrix instanceof DOMMatrix)) {
matrix = createSVGMatrix(matrix) // eslint-disable-line
Expand Down
2 changes: 1 addition & 1 deletion packages/x6-common/src/dom/prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function camelize(str: string) {

const memoized: { [key: string]: string | null } = {}
const prefixes = ['webkit', 'ms', 'moz', 'o']
const testStyle = document ? document.createElement('div').style : {}
const testStyle = typeof document !== 'undefined' ? document.createElement('div').style : {}

function getWithPrefix(name: string) {
for (let i = 0; i < prefixes.length; i += 1) {
Expand Down
2 changes: 2 additions & 0 deletions packages/x6-common/src/dom/selection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const clearSelection = (function () {
if (typeof document == 'undefined')
return function () {}
const doc = document as any
if (doc.selection) {
return function () {
Expand Down
3 changes: 1 addition & 2 deletions packages/x6-common/src/dom/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { attr } from './attr'
import { Vector } from '../vector'
import { createSvgElement, empty } from './elem'

const canvasContext = document.createElement('canvas').getContext('2d')!

function createTextPathNode(
attrs: { d?: string; 'xlink:href'?: string },
elem: SVGElement,
Expand Down Expand Up @@ -374,6 +372,7 @@ export function text(
}

export function measureText(text: string, styles: any = {}) {
const canvasContext = document.createElement('canvas').getContext('2d')!
if (!text) {
return { width: 0 }
}
Expand Down
44 changes: 23 additions & 21 deletions packages/x6-common/src/polyfill/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ if (

// compatible with ParentNode.append() before chrome 54
// https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md
;(function (arr) {
arr.forEach((item) => {
if (Object.prototype.hasOwnProperty.call(item, 'append')) {
return
}
Object.defineProperty(item, 'append', {
configurable: true,
enumerable: true,
writable: true,
value(...args: any[]) {
const docFrag = document.createDocumentFragment()
if (typeof window !== 'undefined') {
;(function (arr) {
arr.forEach((item) => {
if (Object.prototype.hasOwnProperty.call(item, 'append')) {
return
}
Object.defineProperty(item, 'append', {
configurable: true,
enumerable: true,
writable: true,
value(...args: any[]) {
const docFrag = document.createDocumentFragment()

args.forEach((arg: any) => {
const isNode = arg instanceof Node
docFrag.appendChild(
isNode ? arg : document.createTextNode(String(arg)),
)
})
args.forEach((arg: any) => {
const isNode = arg instanceof Node
docFrag.appendChild(
isNode ? arg : document.createTextNode(String(arg)),
)
})

this.appendChild(docFrag)
},
this.appendChild(docFrag)
},
})
})
})
})([Element.prototype, Document.prototype, DocumentFragment.prototype])
})([Element.prototype, Document.prototype, DocumentFragment.prototype])
}
3 changes: 1 addition & 2 deletions packages/x6/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { Dom, PointData, PointLike } from '@antv/x6-common'
import { normalize } from '../registry/marker/util'

export namespace Util {
const svgDocument = Dom.createSvgElement('svg') as SVGSVGElement

export const normalizeMarker = normalize
/**
* Transforms point by an SVG transformation represented by `matrix`.
Expand Down Expand Up @@ -47,6 +45,7 @@ export namespace Util {
rect: Rectangle.RectangleLike,
matrix: DOMMatrix,
) {
const svgDocument = Dom.createSvgElement('svg') as SVGSVGElement
const p = svgDocument.createSVGPoint()

p.x = rect.x
Expand Down
Loading