antvis / X6

🚀 JavaScript diagramming library that uses SVG and HTML for rendering.
https://x6.antv.antgroup.com
MIT License
5.51k stars 1.66k forks source link

支持 ShadowDom 和 WebComponents #1082

Open OpportunityLiu opened 3 years ago

OpportunityLiu commented 3 years ago

Expected Behavior

支持 ShadowDom 和 WebComponents 提供官方封装的 WebComponent

Possible Solution

Related #835

主要是:

  1. 正确注入样式到 shadow root
  2. 获取事件 target 时考虑 composed event

Additional Context

目前我的项目是在 shadowDom 里使用 X6 的,做了一些修改后暂时还没有发现什么问题。

但是还是希望官方能够考虑支持 shadowDom

OpportunityLiu commented 3 years ago

附上我现在做的修正

import { View, EdgeView } from '@antv/x6';

// WORKAROUND: 修复获取 EventTarget
View.prototype.getEventTarget = function getEventTarget(
    e: JQuery.TriggeredEvent,
    options: { fromPoint?: boolean } = {},
): unknown {
    // Touchmove/Touchend event's target is not reflecting the element
    // under the coordinates as mousemove does.
    // It holds the element when a touchstart triggered.
    const { type, clientX = 0, clientY = 0 } = e;
    if (options.fromPoint || type === 'touchmove' || type === 'touchend') {
        let rootNode: HTMLDocument | ShadowRoot | undefined;
        if (this.container && 'getRootNode' in this.container) {
            rootNode = this.container.getRootNode() as HTMLDocument | ShadowRoot;
        }
        if (!rootNode || !('elementFromPoint' in rootNode)) {
            rootNode = document;
        }
        return rootNode.elementFromPoint(clientX, clientY);
    }

    return e.target as unknown;
};

// WORKAROUND: 修复全局事件的 target
View.prototype.normalizeEvent = function <T extends JQuery.TriggeredEvent>(evt: T) {
    return normalizeEvent(evt, this.container);
};

// WORKAROUND: 修复全局事件的 target
const _normalizeEvent = View.normalizeEvent;
View.normalizeEvent = normalizeEvent;

/** 设置正确的 target */
function normalizeEvent<T extends JQuery.TriggeredEvent>(event: T, element?: Element): T {
    event = _normalizeEvent(event);
    let original: Event & JQuery.TriggeredEvent = event as unknown as Event & JQuery.TriggeredEvent;
    while (original.originalEvent) {
        original = original.originalEvent as Event & JQuery.TriggeredEvent;
    }
    if (original.composed) {
        const path = original.composedPath();
        let start = path.length - 1;
        if (element) {
            const root = element.getRootNode();
            if (root instanceof DocumentFragment) {
                const fixedStart = path.lastIndexOf(root);
                if (fixedStart > 0) start = fixedStart;
            }
        }
        let containerIndex = -1,
            fragmentIndex = -1;
        for (let i = start; i >= 0; i--) {
            const el = path[i];
            if (containerIndex < 0) {
                if (el instanceof HTMLElement && el.classList.contains('x6-graph')) {
                    containerIndex = i;
                    continue;
                }
            } else if (fragmentIndex < 0) {
                if (el instanceof DocumentFragment) {
                    fragmentIndex = i;
                    break;
                }
            }
        }
        if (containerIndex >= 0) {
            const target = path[fragmentIndex + 1];
            event.target = target;
        }
    }
    return event;
}
x6-bot[bot] commented 3 years ago

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not-stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the AntV community! 💪💯

OpportunityLiu commented 3 years ago

Any idea?

x6-bot[bot] commented 2 years ago

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not-stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the AntV community! 💪💯

NewByVector commented 2 years ago

@OpportunityLiu 暂时还没有精力来看这块

x6-bot[bot] commented 2 years ago

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not-stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the AntV community! 💪💯

yurili8 commented 2 years ago

与遇到了同样的问题,大神支持一下呗。

x6-bot[bot] commented 2 years ago

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not-stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the AntV community! 💪💯

x6-bot[bot] commented 2 years ago

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not-stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the AntV community! 💪💯

x6-bot[bot] commented 2 years ago

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not-stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the AntV community! 💪💯

NewByVector commented 2 years ago

@yurili8 可以按照上面 @OpportunityLiu 的用法来支持。

x6-bot[bot] commented 2 years ago

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not-stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the AntV community! 💪💯

josiahhaswell commented 2 years ago

While @OpportunityLiu's patch works for click/touch events (thank you!), there seems to still be an issue with events such as hover in ShadowDOM

x6-bot[bot] commented 2 years ago

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not-stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the AntV community! 💪💯