xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* globals jQuery */
/**
 * ext-arrows.js
 *
 * @license MIT
 *
 * @copyright 2010 Alexis Deveria
 *
 */
export default {
  name: 'arrows',
  async init (S) {
    const strings = await S.importLocale();
    const svgEditor = this;
    const svgCanvas = svgEditor.canvas;
    const $ = jQuery;
    const // {svgcontent} = S,
      addElem = svgCanvas.addSVGElementFromJson,
      {nonce} = S,
      prefix = 'se_arrow_';
 
    let selElems, arrowprefix, randomizeIds = S.randomize_ids;
 
    function setArrowNonce (window, n) {
      randomizeIds = true;
      arrowprefix = prefix + n + '_';
      pathdata.fw.id = arrowprefix + 'fw';
      pathdata.bk.id = arrowprefix + 'bk';
    }
 
    function unsetArrowNonce (window) {
      randomizeIds = false;
      arrowprefix = prefix;
      pathdata.fw.id = arrowprefix + 'fw';
      pathdata.bk.id = arrowprefix + 'bk';
    }
 
    svgCanvas.bind('setnonce', setArrowNonce);
    svgCanvas.bind('unsetnonce', unsetArrowNonce);
 
    if (randomizeIds) {
      arrowprefix = prefix + nonce + '_';
    } else {
      arrowprefix = prefix;
    }
 
    const pathdata = {
      fw: {d: 'm0,0l10,5l-10,5l5,-5l-5,-5z', refx: 8, id: arrowprefix + 'fw'},
      bk: {d: 'm10,0l-10,5l10,5l-5,-5l5,-5z', refx: 2, id: arrowprefix + 'bk'}
    };
 
    function getLinked (elem, attr) {
      const str = elem.getAttribute(attr);
      if (!str) { return null; }
      const m = str.match(/\(#(.*)\)/);
      if (!m || m.length !== 2) {
        return null;
      }
      return svgCanvas.getElem(m[1]);
    }
 
    function showPanel (on) {
      $('#arrow_panel').toggle(on);
      if (on) {
        const el = selElems[0];
        const end = el.getAttribute('marker-end');
        const start = el.getAttribute('marker-start');
        const mid = el.getAttribute('marker-mid');
        let val;
        if (end && start) {
          val = 'both';
        } else if (end) {
          val = 'end';
        } else if (start) {
          val = 'start';
        } else if (mid) {
          val = 'mid';
          if (mid.includes('bk')) {
            val = 'mid_bk';
          }
        }
 
        if (!start && !mid && !end) {
          val = 'none';
        }
 
        $('#arrow_list').val(val);
      }
    }
 
    function resetMarker () {
      const el = selElems[0];
      el.removeAttribute('marker-start');
      el.removeAttribute('marker-mid');
      el.removeAttribute('marker-end');
    }
 
    function addMarker (dir, type, id) {
      // TODO: Make marker (or use?) per arrow type, since refX can be different
      id = id || arrowprefix + dir;
 
      const data = pathdata[dir];
 
      if (type === 'mid') {
        data.refx = 5;
      }
 
      let marker = svgCanvas.getElem(id);
      if (!marker) {
        marker = addElem({
          element: 'marker',
          attr: {
            viewBox: '0 0 10 10',
            id,
            refY: 5,
            markerUnits: 'strokeWidth',
            markerWidth: 5,
            markerHeight: 5,
            orient: 'auto',
            style: 'pointer-events:none' // Currently needed for Opera
          }
        });
        const arrow = addElem({
          element: 'path',
          attr: {
            d: data.d,
            fill: '#000000'
          }
        });
        marker.append(arrow);
        svgCanvas.findDefs().append(marker);
      }
 
      marker.setAttribute('refX', data.refx);
 
      return marker;
    }
 
    function setArrow () {
      resetMarker();
 
      let type = this.value;
      if (type === 'none') {
        return;
      }
 
      // Set marker on element
      let dir = 'fw';
      if (type === 'mid_bk') {
        type = 'mid';
        dir = 'bk';
      } else if (type === 'both') {
        addMarker('bk', type);
        svgCanvas.changeSelectedAttribute('marker-start', 'url(#' + pathdata.bk.id + ')');
        type = 'end';
        dir = 'fw';
      } else if (type === 'start') {
        dir = 'bk';
      }
 
      addMarker(dir, type);
      svgCanvas.changeSelectedAttribute('marker-' + type, 'url(#' + pathdata[dir].id + ')');
      svgCanvas.call('changed', selElems);
    }
 
    function colorChanged (elem) {
      const color = elem.getAttribute('stroke');
      const mtypes = ['start', 'mid', 'end'];
      const defs = svgCanvas.findDefs();
 
      $.each(mtypes, function (i, type) {
        const marker = getLinked(elem, 'marker-' + type);
        if (!marker) { return; }
 
        const curColor = $(marker).children().attr('fill');
        const curD = $(marker).children().attr('d');
        if (curColor === color) { return; }
 
        const allMarkers = $(defs).find('marker');
        let newMarker = null;
        // Different color, check if already made
        allMarkers.each(function () {
          const attrs = $(this).children().attr(['fill', 'd']);
          if (attrs.fill === color && attrs.d === curD) {
            // Found another marker with this color and this path
            newMarker = this;
          }
        });
 
        if (!newMarker) {
          // Create a new marker with this color
          const lastId = marker.id;
          const dir = lastId.includes('_fw') ? 'fw' : 'bk';
 
          newMarker = addMarker(dir, type, arrowprefix + dir + allMarkers.length);
 
          $(newMarker).children().attr('fill', color);
        }
 
        $(elem).attr('marker-' + type, 'url(#' + newMarker.id + ')');
 
        // Check if last marker can be removed
        let remove = true;
        $(S.svgcontent).find('line, polyline, path, polygon').each(function () {
          const elem = this;
          $.each(mtypes, function (j, mtype) {
            if ($(elem).attr('marker-' + mtype) === 'url(#' + marker.id + ')') {
              remove = false;
              return remove;
            }
          });
          if (!remove) { return false; }
        });
 
        // Not found, so can safely remove
        if (remove) {
          $(marker).remove();
        }
      });
    }
 
    const contextTools = [
      {
        type: 'select',
        panel: 'arrow_panel',
        id: 'arrow_list',
        defval: 'none',
        events: {
          change: setArrow
        }
      }
    ];
 
    return {
      name: strings.name,
      context_tools: strings.contextTools.map((contextTool, i) => {
        return Object.assign(contextTools[i], contextTool);
      }),
      callback () {
        $('#arrow_panel').hide();
        // Set ID so it can be translated in locale file
        $('#arrow_list option')[0].id = 'connector_no_arrow';
      },
      async addLangData ({lang, importLocale}) {
        const strings = await importLocale();
        return {
          data: strings.langList
        };
      },
      selectedChanged (opts) {
        // Use this to update the current selected elements
        selElems = opts.elems;
 
        const markerElems = ['line', 'path', 'polyline', 'polygon'];
        let i = selElems.length;
        while (i--) {
          const elem = selElems[i];
          if (elem && markerElems.includes(elem.tagName)) {
            if (opts.selectedElement && !opts.multiselected) {
              showPanel(true);
            } else {
              showPanel(false);
            }
          } else {
            showPanel(false);
          }
        }
      },
      elementChanged (opts) {
        const elem = opts.elems[0];
        if (elem && (
          elem.getAttribute('marker-start') ||
          elem.getAttribute('marker-mid') ||
          elem.getAttribute('marker-end')
        )) {
          // const start = elem.getAttribute('marker-start');
          // const mid = elem.getAttribute('marker-mid');
          // const end = elem.getAttribute('marker-end');
          // Has marker, so see if it should match color
          colorChanged(elem);
        }
      }
    };
  }
};