Skip to content

Commit

Permalink
fix: 华为鸿蒙 globalThis 不存在
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Dec 21, 2022
1 parent 9f3e82c commit 7497db2
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 90 deletions.
88 changes: 52 additions & 36 deletions packages/g-dom-mutation-observer-api/src/dom/MutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ export class Registration {

private addListeners_(node: IElement) {
const options = this.options;
if (options.attributes) node.addEventListener(ElementEvent.ATTR_MODIFIED, this, true);
if (options.attributes)
node.addEventListener(ElementEvent.ATTR_MODIFIED, this, true);

// if (options.characterData) node.addEventListener('DOMCharacterDataModified', this, true);

if (options.childList) node.addEventListener(ElementEvent.INSERTED, this, true);
if (options.childList)
node.addEventListener(ElementEvent.INSERTED, this, true);

if (options.childList || options.subtree)
node.addEventListener(ElementEvent.REMOVED, this, true);
Expand All @@ -58,11 +60,13 @@ export class Registration {

removeListeners_(node: IElement) {
const options = this.options;
if (options.attributes) node.removeEventListener(ElementEvent.ATTR_MODIFIED, this, true);
if (options.attributes)
node.removeEventListener(ElementEvent.ATTR_MODIFIED, this, true);

// if (options.characterData) node.removeEventListener('DOMCharacterDataModified', this, true);

if (options.childList) node.removeEventListener(ElementEvent.INSERTED, this, true);
if (options.childList)
node.removeEventListener(ElementEvent.INSERTED, this, true);

if (options.childList || options.subtree)
node.removeEventListener(ElementEvent.REMOVED, this, true);
Expand Down Expand Up @@ -131,27 +135,32 @@ export class Registration {
record.attributeNamespace = namespace;

// 2.
const oldValue = e.attrChange === MutationEvent.ADDITION ? null : e.prevValue;

forEachAncestorAndObserverEnqueueRecord(target as IElement, (options) => {
// 3.1, 4.2
if (!options.attributes) return;

// 3.2, 4.3
if (
options.attributeFilter &&
options.attributeFilter.length &&
options.attributeFilter.indexOf(name) === -1 &&
options.attributeFilter.indexOf(namespace) === -1
) {
return;
}
// 3.3, 4.4
if (options.attributeOldValue) return getRecordWithOldValue(oldValue);

// 3.4, 4.5
return record;
});
const oldValue =
e.attrChange === MutationEvent.ADDITION ? null : e.prevValue;

forEachAncestorAndObserverEnqueueRecord(
target as IElement,
(options) => {
// 3.1, 4.2
if (!options.attributes) return;

// 3.2, 4.3
if (
options.attributeFilter &&
options.attributeFilter.length &&
options.attributeFilter.indexOf(name) === -1 &&
options.attributeFilter.indexOf(namespace) === -1
) {
return;
}
// 3.3, 4.4
if (options.attributeOldValue)
return getRecordWithOldValue(oldValue);

// 3.4, 4.5
return record;
},
);

break;

Expand Down Expand Up @@ -206,13 +215,16 @@ export class Registration {
record.previousSibling = previousSibling as IElement;
record.nextSibling = nextSibling as IElement;

forEachAncestorAndObserverEnqueueRecord(target as IElement, function (options) {
// 2.1, 3.2
if (!options.childList) return;
forEachAncestorAndObserverEnqueueRecord(
target as IElement,
function (options) {
// 2.1, 3.2
if (!options.childList) return;

// 2.2, 3.3
return record;
});
// 2.2, 3.3
return record;
},
);
}

clearRecords();
Expand All @@ -237,7 +249,9 @@ export class MutationObserver {
// 1.2
(options.attributeOldValue && !options.attributes) ||
// 1.3
(options.attributeFilter && options.attributeFilter.length && !options.attributes) ||
(options.attributeFilter &&
options.attributeFilter.length &&
!options.attributes) ||
// 1.4
(options.characterDataOldValue && !options.characterData)
) {
Expand Down Expand Up @@ -342,7 +356,8 @@ function selectRecord(lastRecord: MutationRecord, newRecord: MutationRecord) {

// Check if the the record we are adding represents the same record. If
// so, we keep the one with the oldValue in it.
if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord)) return recordWithOldValue;
if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord))
return recordWithOldValue;

return null;
}
Expand All @@ -352,7 +367,8 @@ function removeTransientObserversFor(observer: MutationObserver) {
const registrations = registrationsTable.get(node);
if (!registrations) return;
registrations.forEach(function (registration) {
if (registration.observer === observer) registration.removeTransientObservers();
if (registration.observer === observer)
registration.removeTransientObservers();
});
});
}
Expand Down Expand Up @@ -402,8 +418,8 @@ function scheduleCallback(observer: MutationObserver) {
if (!isScheduled) {
isScheduled = true;
// setImmediate(dispatchCallbacks);
if (typeof globalThis !== 'undefined') {
globalThis.setTimeout(dispatchCallbacks);
if (typeof window !== 'undefined') {
setTimeout(dispatchCallbacks);
} else {
dispatchCallbacks();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/g-image-exporter/src/ImageExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ export class ImageExporter {

// account for IE
// @see https://stackoverflow.com/a/41434373
if ((globalThis.navigator as any).msSaveBlob) {
(globalThis.navigator as any).msSaveBlob(blobObj, fileName);
if ((window.navigator as any).msSaveBlob) {
(window.navigator as any).msSaveBlob(blobObj, fileName);
} else {
link.addEventListener('click', () => {
link.download = fileName;
Expand Down
18 changes: 8 additions & 10 deletions packages/g-lite/src/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,27 @@ export class Canvas extends EventTarget implements ICanvas {
* implements `Window` interface
*/
this.devicePixelRatio = dpr;
this.requestAnimationFrame = requestAnimationFrame ?? raf.bind(globalThis);
this.cancelAnimationFrame = cancelAnimationFrame ?? caf.bind(globalThis);
this.requestAnimationFrame = requestAnimationFrame ?? raf.bind(window);
this.cancelAnimationFrame = cancelAnimationFrame ?? caf.bind(window);

/**
* limits query
*/
// the following feature-detect from hammer.js
// @see https://github.com/hammerjs/hammer.js/blob/master/src/inputjs/input-consts.js#L5
this.supportsTouchEvents =
supportsTouchEvents ?? 'ontouchstart' in globalThis;
this.supportsPointerEvents =
supportsPointerEvents ?? !!globalThis.PointerEvent;
this.supportsTouchEvents = supportsTouchEvents ?? 'ontouchstart' in window;
this.supportsPointerEvents = supportsPointerEvents ?? !!window.PointerEvent;
this.isTouchEvent =
isTouchEvent ??
((event: InteractivePointerEvent): event is TouchEvent =>
this.supportsTouchEvents && event instanceof globalThis.TouchEvent);
this.supportsTouchEvents && event instanceof window.TouchEvent);
this.isMouseEvent =
isMouseEvent ??
((event: InteractivePointerEvent): event is MouseEvent =>
!globalThis.MouseEvent ||
(event instanceof globalThis.MouseEvent &&
!window.MouseEvent ||
(event instanceof window.MouseEvent &&
(!this.supportsPointerEvents ||
!(event instanceof globalThis.PointerEvent))));
!(event instanceof window.PointerEvent))));

this.initRenderingContext({
container,
Expand Down
2 changes: 1 addition & 1 deletion packages/g-plugin-a11y/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { isMobileResult } from 'ismobilejs';
import isMobileCall from 'ismobilejs';

const isMobile: isMobileResult = isMobileCall(globalThis.navigator);
const isMobile: isMobileResult = isMobileCall(window.navigator);

export { isMobile };
2 changes: 1 addition & 1 deletion packages/g-plugin-control/src/ControlPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Hammer from 'hammerjs';

const MOTION_FACTOR = 10;
// https://gist.github.com/handleman/3c99e754065f647b082f
const isMac = globalThis.navigator.platform.toUpperCase().indexOf('MAC') >= 0;
const isMac = window.navigator.platform.toUpperCase().indexOf('MAC') >= 0;

export class ControlPlugin implements RenderingPlugin {
static tag = 'Control';
Expand Down
81 changes: 43 additions & 38 deletions packages/g-plugin-dom-interaction/src/DOMInteractionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export class DOMInteractionPlugin implements RenderingPlugin {
};

const addPointerEventListener = ($el: HTMLElement) => {
globalThis.document.addEventListener('pointermove', onPointerMove, true);
window.document.addEventListener('pointermove', onPointerMove, true);
$el.addEventListener('pointerdown', onPointerDown, true);
$el.addEventListener('pointerleave', onPointerOut, true);
$el.addEventListener('pointerover', onPointerOver, true);
globalThis.addEventListener('pointerup', onPointerUp, true);
window.addEventListener('pointerup', onPointerUp, true);
};

const addTouchEventListener = ($el: HTMLElement) => {
Expand All @@ -65,19 +65,19 @@ export class DOMInteractionPlugin implements RenderingPlugin {
};

const addMouseEventListener = ($el: HTMLElement) => {
globalThis.document.addEventListener('mousemove', onPointerMove, true);
window.document.addEventListener('mousemove', onPointerMove, true);
$el.addEventListener('mousedown', onPointerDown, true);
$el.addEventListener('mouseout', onPointerOut, true);
$el.addEventListener('mouseover', onPointerOver, true);
globalThis.addEventListener('mouseup', onPointerUp, true);
window.addEventListener('mouseup', onPointerUp, true);
};

const removePointerEventListener = ($el: HTMLElement) => {
globalThis.document.removeEventListener('pointermove', onPointerMove, true);
window.document.removeEventListener('pointermove', onPointerMove, true);
$el.removeEventListener('pointerdown', onPointerDown, true);
$el.removeEventListener('pointerleave', onPointerOut, true);
$el.removeEventListener('pointerover', onPointerOver, true);
globalThis.removeEventListener('pointerup', onPointerUp, true);
window.removeEventListener('pointerup', onPointerUp, true);
};

const removeTouchEventListener = ($el: HTMLElement) => {
Expand All @@ -88,49 +88,54 @@ export class DOMInteractionPlugin implements RenderingPlugin {
};

const removeMouseEventListener = ($el: HTMLElement) => {
globalThis.document.removeEventListener('mousemove', onPointerMove, true);
window.document.removeEventListener('mousemove', onPointerMove, true);
$el.removeEventListener('mousedown', onPointerDown, true);
$el.removeEventListener('mouseout', onPointerOut, true);
$el.removeEventListener('mouseover', onPointerOver, true);
globalThis.removeEventListener('mouseup', onPointerUp, true);
window.removeEventListener('mouseup', onPointerUp, true);
};

renderingService.hooks.init.tapPromise(DOMInteractionPlugin.tag, async () => {
const $el = this.context.contextService.getDomElement() as unknown as HTMLElement;
renderingService.hooks.init.tapPromise(
DOMInteractionPlugin.tag,
async () => {
const $el =
this.context.contextService.getDomElement() as unknown as HTMLElement;

// @ts-ignore
if (globalThis.navigator.msPointerEnabled) {
// @ts-ignore
$el.style.msContentZooming = 'none';
// @ts-ignore
$el.style.msTouchAction = 'none';
} else if (canvas.supportsPointerEvents) {
$el.style.touchAction = 'none';
}

if (canvas.supportsPointerEvents) {
addPointerEventListener($el);
} else {
addMouseEventListener($el);
}

if (canvas.supportsTouchEvents) {
addTouchEventListener($el);
}

// use passive event listeners
// @see https://zhuanlan.zhihu.com/p/24555031
$el.addEventListener('wheel', onPointerWheel, {
passive: true,
capture: true,
});
});
if (window.navigator.msPointerEnabled) {
// @ts-ignore
$el.style.msContentZooming = 'none';
// @ts-ignore
$el.style.msTouchAction = 'none';
} else if (canvas.supportsPointerEvents) {
$el.style.touchAction = 'none';
}

if (canvas.supportsPointerEvents) {
addPointerEventListener($el);
} else {
addMouseEventListener($el);
}

if (canvas.supportsTouchEvents) {
addTouchEventListener($el);
}

// use passive event listeners
// @see https://zhuanlan.zhihu.com/p/24555031
$el.addEventListener('wheel', onPointerWheel, {
passive: true,
capture: true,
});
},
);

renderingService.hooks.destroy.tap(DOMInteractionPlugin.tag, () => {
const $el = this.context.contextService.getDomElement() as unknown as HTMLElement;
const $el =
this.context.contextService.getDomElement() as unknown as HTMLElement;

// @ts-ignore
if (globalThis.navigator.msPointerEnabled) {
if (window.navigator.msPointerEnabled) {
// @ts-ignore
$el.style.msContentZooming = '';
// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class WebGPUDeviceContribution implements DeviceContribution {
constructor(private pluginOptions: Partial<WebGPUDeviceOptions>) {}

async createSwapChain($canvas: HTMLCanvasElement) {
if ((globalThis.navigator as any).gpu === undefined) return null;
if ((window.navigator as any).gpu === undefined) return null;

let adapter = null;
try {
adapter = await (globalThis.navigator as any).gpu.requestAdapter();
adapter = await (window.navigator as any).gpu.requestAdapter();
} catch (e) {
console.log(e);
}
Expand Down

0 comments on commit 7497db2

Please sign in to comment.