mh-two-thousand-and-two
2024-03-25 b8c93990f3fa5e50a8aca16bdc9c2758168aa0fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * @file Manages SVG clipPath elements.
 * @author Zhang Wenli
 */
 
import Definable from './Definable';
import * as zrUtil from '../../core/util';
import Displayable from '../../graphic/Displayable';
import Path from '../../graphic/Path';
import {path} from '../graphic';
import { Dictionary } from '../../core/types';
import { isClipPathChanged } from '../../canvas/helper';
import { getClipPathsKey, getIdURL } from '../../svg/helper';
import { createElement } from '../../svg/core';
 
type PathExtended = Path & {
    _dom: SVGElement
}
 
export function hasClipPath(displayable: Displayable) {
    const clipPaths = displayable.__clipPaths;
    return clipPaths && clipPaths.length > 0;
}
/**
 * Manages SVG clipPath elements.
 */
export default class ClippathManager extends Definable {
 
    private _refGroups: Dictionary<SVGElement> = {};
    private _keyDuplicateCount: Dictionary<number> = {};
 
    constructor(zrId: number, svgRoot: SVGElement) {
        super(zrId, svgRoot, 'clipPath', '__clippath_in_use__');
    }
 
    markAllUnused() {
        super.markAllUnused();
        const refGroups = this._refGroups;
        for (let key in refGroups) {
            if (refGroups.hasOwnProperty(key)) {
                this.markDomUnused(refGroups[key]);
            }
        }
        this._keyDuplicateCount = {};
    }
 
 
    private _getClipPathGroup(displayable: Displayable, prevDisplayable: Displayable) {
        if (!hasClipPath(displayable)) {
            return;
        }
        const clipPaths = displayable.__clipPaths;
 
        const keyDuplicateCount = this._keyDuplicateCount;
        let clipPathKey = getClipPathsKey(clipPaths);
        if (isClipPathChanged(clipPaths, prevDisplayable && prevDisplayable.__clipPaths)) {
            keyDuplicateCount[clipPathKey] = keyDuplicateCount[clipPathKey] || 0;
            keyDuplicateCount[clipPathKey] && (clipPathKey += '-' + keyDuplicateCount[clipPathKey]);
            keyDuplicateCount[clipPathKey]++;
        }
 
        return this._refGroups[clipPathKey]
            || (this._refGroups[clipPathKey] = createElement('g'));
    }
 
    /**
     * Update clipPath.
     *
     * @param displayable displayable element
     */
    update(displayable: Displayable, prevDisplayable: Displayable) {
        const clipGroup = this._getClipPathGroup(displayable, prevDisplayable);
        if (clipGroup) {
            this.markDomUsed(clipGroup);
            this.updateDom(clipGroup, displayable.__clipPaths);
        }
        return clipGroup;
    };
 
 
    /**
     * Create an SVGElement of displayable and create a <clipPath> of its
     * clipPath
     */
    updateDom(parentEl: SVGElement, clipPaths: Path[]) {
        if (clipPaths && clipPaths.length > 0) {
            // Has clipPath, create <clipPath> with the first clipPath
            const defs = this.getDefs(true);
            const clipPath = clipPaths[0] as PathExtended;
            let clipPathEl;
            let id;
 
            if (clipPath._dom) {
                // Use a dom that is already in <defs>
                id = clipPath._dom.getAttribute('id');
                clipPathEl = clipPath._dom;
 
                // Use a dom that is already in <defs>
                if (!defs.contains(clipPathEl)) {
                    // This happens when set old clipPath that has
                    // been previously removed
                    defs.appendChild(clipPathEl);
                }
            }
            else {
                // New <clipPath>
                id = 'zr' + this._zrId + '-clip-' + this.nextId;
                ++this.nextId;
                clipPathEl = createElement('clipPath');
                clipPathEl.setAttribute('id', id);
                defs.appendChild(clipPathEl);
 
                clipPath._dom = clipPathEl;
            }
 
            // Build path and add to <clipPath>
            path.brush(clipPath);
 
            const pathEl = this.getSvgElement(clipPath);
 
            clipPathEl.innerHTML = '';
            clipPathEl.appendChild(pathEl);
 
            parentEl.setAttribute('clip-path', getIdURL(id));
 
            if (clipPaths.length > 1) {
                // Make the other clipPaths recursively
                this.updateDom(clipPathEl, clipPaths.slice(1));
            }
        }
        else {
            // No clipPath
            if (parentEl) {
                parentEl.setAttribute('clip-path', 'none');
            }
        }
    };
 
    /**
     * Mark a single clipPath to be used
     *
     * @param displayable displayable element
     */
    markUsed(displayable: Displayable) {
        // displayable.__clipPaths can only be `null`/`undefined` or an non-empty array.
        if (displayable.__clipPaths) {
            zrUtil.each(displayable.__clipPaths, (clipPath: PathExtended) => {
                if (clipPath._dom) {
                    super.markDomUsed(clipPath._dom);
                }
            });
        }
    };
 
    removeUnused() {
        super.removeUnused();
 
        const newRefGroupsMap: Dictionary<SVGElement> = {};
        const refGroups = this._refGroups;
        for (let key in refGroups) {
            if (refGroups.hasOwnProperty(key)) {
                const group = refGroups[key];
                if (!this.isDomUnused(group)) {
                    newRefGroupsMap[key] = group;
                }
                else if (group.parentNode) {
                    group.parentNode.removeChild(group);
                }
            }
        }
        this._refGroups = newRefGroupsMap;
    }
}