@svgedit/svgcanvas 7.2.3 → 7.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.md +4 -0
- package/core/coords.js +203 -99
- package/core/draw.js +129 -56
- package/core/event.js +1 -0
- package/core/math.js +138 -154
- package/core/recalculate.js +232 -613
- package/core/sanitize.js +3 -3
- package/core/selected-elem.js +1 -1
- package/core/selection.js +1 -1
- package/core/svg-exec.js +163 -140
- package/core/text-actions.js +160 -129
- package/dist/svgcanvas.js +20310 -19109
- package/dist/svgcanvas.js.map +1 -1
- package/package.json +1 -1
- package/svgcanvas.js +1 -1
package/core/draw.js
CHANGED
|
@@ -9,15 +9,14 @@ import Layer from './layer.js'
|
|
|
9
9
|
import HistoryRecordingService from './historyrecording.js'
|
|
10
10
|
|
|
11
11
|
import { NS } from './namespaces.js'
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
} from './utilities.js'
|
|
15
|
-
import {
|
|
16
|
-
copyElem as utilCopyElem
|
|
17
|
-
} from './copy-elem.js'
|
|
12
|
+
import { toXml, getElement } from './utilities.js'
|
|
13
|
+
import { copyElem as utilCopyElem } from './copy-elem.js'
|
|
18
14
|
import { getParentsUntil } from '../common/util.js'
|
|
19
15
|
|
|
20
|
-
const visElems =
|
|
16
|
+
const visElems =
|
|
17
|
+
'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use'.split(
|
|
18
|
+
','
|
|
19
|
+
)
|
|
21
20
|
|
|
22
21
|
const RandomizeModes = {
|
|
23
22
|
LET_DOCUMENT_DECIDE: 0,
|
|
@@ -64,9 +63,10 @@ function isLayerElement (element) {
|
|
|
64
63
|
*/
|
|
65
64
|
function getNewLayerName (existingLayerNames) {
|
|
66
65
|
let i = 1
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
while (existingLayerNames.includes(`Layer ${i}`)) {
|
|
67
|
+
i++
|
|
68
|
+
}
|
|
69
|
+
return `Layer ${i}`
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
/**
|
|
@@ -81,9 +81,16 @@ export class Drawing {
|
|
|
81
81
|
* @throws {Error} If not initialized with an SVG element
|
|
82
82
|
*/
|
|
83
83
|
constructor (svgElem, optIdPrefix) {
|
|
84
|
-
if (
|
|
85
|
-
svgElem
|
|
86
|
-
|
|
84
|
+
if (
|
|
85
|
+
!svgElem ||
|
|
86
|
+
!svgElem.tagName ||
|
|
87
|
+
!svgElem.namespaceURI ||
|
|
88
|
+
svgElem.tagName !== 'svg' ||
|
|
89
|
+
svgElem.namespaceURI !== NS.SVG
|
|
90
|
+
) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
'Error: svgedit.draw.Drawing instance initialized without a <svg> element'
|
|
93
|
+
)
|
|
87
94
|
}
|
|
88
95
|
|
|
89
96
|
/**
|
|
@@ -260,7 +267,11 @@ export class Drawing {
|
|
|
260
267
|
|
|
261
268
|
// if we didn't get a positive number or we already released this number
|
|
262
269
|
// then return false.
|
|
263
|
-
if (
|
|
270
|
+
if (
|
|
271
|
+
typeof num !== 'number' ||
|
|
272
|
+
num <= 0 ||
|
|
273
|
+
this.releasedNums.includes(num)
|
|
274
|
+
) {
|
|
264
275
|
return false
|
|
265
276
|
}
|
|
266
277
|
|
|
@@ -353,7 +364,9 @@ export class Drawing {
|
|
|
353
364
|
}
|
|
354
365
|
|
|
355
366
|
const oldpos = this.indexCurrentLayer()
|
|
356
|
-
if (
|
|
367
|
+
if (oldpos === -1 || oldpos === newpos) {
|
|
368
|
+
return null
|
|
369
|
+
}
|
|
357
370
|
|
|
358
371
|
// if our new position is below us, we need to insert before the node after newpos
|
|
359
372
|
const currentGroup = this.current_layer.getGroup()
|
|
@@ -386,7 +399,9 @@ export class Drawing {
|
|
|
386
399
|
mergeLayer (hrService) {
|
|
387
400
|
const currentGroup = this.current_layer.getGroup()
|
|
388
401
|
const prevGroup = currentGroup.previousElementSibling
|
|
389
|
-
if (!prevGroup) {
|
|
402
|
+
if (!prevGroup) {
|
|
403
|
+
return
|
|
404
|
+
}
|
|
390
405
|
|
|
391
406
|
hrService.startBatchCommand('Merge Layer')
|
|
392
407
|
|
|
@@ -490,7 +505,8 @@ export class Drawing {
|
|
|
490
505
|
this.layer_map = {}
|
|
491
506
|
const numchildren = this.svgElem_.childNodes.length
|
|
492
507
|
// loop through all children of SVG element
|
|
493
|
-
const orphans = []
|
|
508
|
+
const orphans = []
|
|
509
|
+
const layernames = []
|
|
494
510
|
let layer = null
|
|
495
511
|
let childgroups = false
|
|
496
512
|
for (let i = 0; i < numchildren; ++i) {
|
|
@@ -518,7 +534,8 @@ export class Drawing {
|
|
|
518
534
|
|
|
519
535
|
// If orphans or no layers found, create a new layer and add all the orphans to it
|
|
520
536
|
if (orphans.length > 0 || !childgroups) {
|
|
521
|
-
|
|
537
|
+
const name = getNewLayerName(layernames)
|
|
538
|
+
layer = new Layer(name, null, this.svgElem_)
|
|
522
539
|
layer.appendChildren(orphans)
|
|
523
540
|
this.all_layers.push(layer)
|
|
524
541
|
this.layer_map[name] = layer
|
|
@@ -541,7 +558,12 @@ export class Drawing {
|
|
|
541
558
|
this.current_layer.deactivate()
|
|
542
559
|
}
|
|
543
560
|
// Check for duplicate name.
|
|
544
|
-
if (
|
|
561
|
+
if (
|
|
562
|
+
name === undefined ||
|
|
563
|
+
name === null ||
|
|
564
|
+
name === '' ||
|
|
565
|
+
this.layer_map[name]
|
|
566
|
+
) {
|
|
545
567
|
name = getNewLayerName(Object.keys(this.layer_map))
|
|
546
568
|
}
|
|
547
569
|
|
|
@@ -568,10 +590,17 @@ export class Drawing {
|
|
|
568
590
|
* also the current layer of this drawing.
|
|
569
591
|
*/
|
|
570
592
|
cloneLayer (name, hrService) {
|
|
571
|
-
if (!this.current_layer) {
|
|
593
|
+
if (!this.current_layer) {
|
|
594
|
+
return null
|
|
595
|
+
}
|
|
572
596
|
this.current_layer.deactivate()
|
|
573
597
|
// Check for duplicate name.
|
|
574
|
-
if (
|
|
598
|
+
if (
|
|
599
|
+
name === undefined ||
|
|
600
|
+
name === null ||
|
|
601
|
+
name === '' ||
|
|
602
|
+
this.layer_map[name]
|
|
603
|
+
) {
|
|
575
604
|
name = getNewLayerName(Object.keys(this.layer_map))
|
|
576
605
|
}
|
|
577
606
|
|
|
@@ -582,8 +611,10 @@ export class Drawing {
|
|
|
582
611
|
|
|
583
612
|
// Clone children
|
|
584
613
|
const children = [...currentGroup.childNodes]
|
|
585
|
-
children.forEach(
|
|
586
|
-
if (child.localName === 'title') {
|
|
614
|
+
children.forEach(child => {
|
|
615
|
+
if (child.localName === 'title') {
|
|
616
|
+
return
|
|
617
|
+
}
|
|
587
618
|
group.append(this.copyElem(child))
|
|
588
619
|
})
|
|
589
620
|
|
|
@@ -630,7 +661,9 @@ export class Drawing {
|
|
|
630
661
|
return null
|
|
631
662
|
}
|
|
632
663
|
const layer = this.layer_map[layerName]
|
|
633
|
-
if (!layer) {
|
|
664
|
+
if (!layer) {
|
|
665
|
+
return null
|
|
666
|
+
}
|
|
634
667
|
layer.setVisible(bVisible)
|
|
635
668
|
return layer.getGroup()
|
|
636
669
|
}
|
|
@@ -643,7 +676,9 @@ export class Drawing {
|
|
|
643
676
|
*/
|
|
644
677
|
getLayerOpacity (layerName) {
|
|
645
678
|
const layer = this.layer_map[layerName]
|
|
646
|
-
if (!layer) {
|
|
679
|
+
if (!layer) {
|
|
680
|
+
return null
|
|
681
|
+
}
|
|
647
682
|
return layer.getOpacity()
|
|
648
683
|
}
|
|
649
684
|
|
|
@@ -676,7 +711,9 @@ export class Drawing {
|
|
|
676
711
|
*/
|
|
677
712
|
copyElem (el) {
|
|
678
713
|
const that = this
|
|
679
|
-
const getNextIdClosure = function () {
|
|
714
|
+
const getNextIdClosure = function () {
|
|
715
|
+
return that.getNextId()
|
|
716
|
+
}
|
|
680
717
|
return utilCopyElem(el, getNextIdClosure)
|
|
681
718
|
}
|
|
682
719
|
}
|
|
@@ -690,13 +727,20 @@ export class Drawing {
|
|
|
690
727
|
* @returns {void}
|
|
691
728
|
*/
|
|
692
729
|
export const randomizeIds = function (enableRandomization, currentDrawing) {
|
|
693
|
-
randIds =
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
730
|
+
randIds =
|
|
731
|
+
enableRandomization === false
|
|
732
|
+
? RandomizeModes.NEVER_RANDOMIZE
|
|
733
|
+
: RandomizeModes.ALWAYS_RANDOMIZE
|
|
734
|
+
|
|
735
|
+
if (
|
|
736
|
+
randIds === RandomizeModes.ALWAYS_RANDOMIZE &&
|
|
737
|
+
!currentDrawing.getNonce()
|
|
738
|
+
) {
|
|
698
739
|
currentDrawing.setNonce(Math.floor(Math.random() * 100001))
|
|
699
|
-
} else if (
|
|
740
|
+
} else if (
|
|
741
|
+
randIds === RandomizeModes.NEVER_RANDOMIZE &&
|
|
742
|
+
currentDrawing.getNonce()
|
|
743
|
+
) {
|
|
700
744
|
currentDrawing.clearNonce()
|
|
701
745
|
}
|
|
702
746
|
}
|
|
@@ -768,7 +812,7 @@ let svgCanvas
|
|
|
768
812
|
* @param {module:draw.DrawCanvasInit} canvas
|
|
769
813
|
* @returns {void}
|
|
770
814
|
*/
|
|
771
|
-
export const init =
|
|
815
|
+
export const init = canvas => {
|
|
772
816
|
svgCanvas = canvas
|
|
773
817
|
}
|
|
774
818
|
|
|
@@ -802,10 +846,9 @@ export const indexCurrentLayer = () => {
|
|
|
802
846
|
* @returns {void}
|
|
803
847
|
*/
|
|
804
848
|
export const createLayer = (name, hrService) => {
|
|
805
|
-
const newLayer = svgCanvas
|
|
806
|
-
|
|
807
|
-
historyRecordingService(hrService)
|
|
808
|
-
)
|
|
849
|
+
const newLayer = svgCanvas
|
|
850
|
+
.getCurrentDrawing()
|
|
851
|
+
.createLayer(name, historyRecordingService(hrService))
|
|
809
852
|
svgCanvas.clearSelection()
|
|
810
853
|
svgCanvas.call('changed', [newLayer])
|
|
811
854
|
}
|
|
@@ -822,7 +865,9 @@ export const createLayer = (name, hrService) => {
|
|
|
822
865
|
*/
|
|
823
866
|
export const cloneLayer = (name, hrService) => {
|
|
824
867
|
// Clone the current layer and make the cloned layer the new current layer
|
|
825
|
-
const newLayer = svgCanvas
|
|
868
|
+
const newLayer = svgCanvas
|
|
869
|
+
.getCurrentDrawing()
|
|
870
|
+
.cloneLayer(name, historyRecordingService(hrService))
|
|
826
871
|
|
|
827
872
|
svgCanvas.clearSelection()
|
|
828
873
|
leaveContext()
|
|
@@ -845,7 +890,9 @@ export const deleteCurrentLayer = () => {
|
|
|
845
890
|
if (currentLayer) {
|
|
846
891
|
const batchCmd = new BatchCommand('Delete Layer')
|
|
847
892
|
// store in our Undo History
|
|
848
|
-
batchCmd.addSubCommand(
|
|
893
|
+
batchCmd.addSubCommand(
|
|
894
|
+
new RemoveElementCommand(currentLayer, nextSibling, parent)
|
|
895
|
+
)
|
|
849
896
|
svgCanvas.addCommandToHistory(batchCmd)
|
|
850
897
|
svgCanvas.clearSelection()
|
|
851
898
|
svgCanvas.call('changed', [parent])
|
|
@@ -861,7 +908,7 @@ export const deleteCurrentLayer = () => {
|
|
|
861
908
|
* @param {string} name - The name of the layer you want to switch to.
|
|
862
909
|
* @returns {boolean} true if the current layer was switched, otherwise false
|
|
863
910
|
*/
|
|
864
|
-
export const setCurrentLayer =
|
|
911
|
+
export const setCurrentLayer = name => {
|
|
865
912
|
const result = svgCanvas.getCurrentDrawing().setCurrentLayer(toXml(name))
|
|
866
913
|
if (result) {
|
|
867
914
|
svgCanvas.clearSelection()
|
|
@@ -878,11 +925,14 @@ export const setCurrentLayer = (name) => {
|
|
|
878
925
|
* @fires module:svgcanvas.SvgCanvas#event:changed
|
|
879
926
|
* @returns {boolean} Whether the rename succeeded
|
|
880
927
|
*/
|
|
881
|
-
export const renameCurrentLayer =
|
|
928
|
+
export const renameCurrentLayer = newName => {
|
|
882
929
|
const drawing = svgCanvas.getCurrentDrawing()
|
|
883
930
|
const layer = drawing.getCurrentLayer()
|
|
884
931
|
if (layer) {
|
|
885
|
-
const result = drawing.setCurrentLayerName(
|
|
932
|
+
const result = drawing.setCurrentLayerName(
|
|
933
|
+
newName,
|
|
934
|
+
historyRecordingService()
|
|
935
|
+
)
|
|
886
936
|
if (result) {
|
|
887
937
|
svgCanvas.call('changed', [layer])
|
|
888
938
|
return true
|
|
@@ -900,12 +950,18 @@ export const renameCurrentLayer = (newName) => {
|
|
|
900
950
|
* 0 and (number of layers - 1)
|
|
901
951
|
* @returns {boolean} `true` if the current layer position was changed, `false` otherwise.
|
|
902
952
|
*/
|
|
903
|
-
export const setCurrentLayerPosition =
|
|
953
|
+
export const setCurrentLayerPosition = newPos => {
|
|
904
954
|
const { MoveElementCommand } = svgCanvas.history
|
|
905
955
|
const drawing = svgCanvas.getCurrentDrawing()
|
|
906
956
|
const result = drawing.setCurrentLayerPosition(newPos)
|
|
907
957
|
if (result) {
|
|
908
|
-
svgCanvas.addCommandToHistory(
|
|
958
|
+
svgCanvas.addCommandToHistory(
|
|
959
|
+
new MoveElementCommand(
|
|
960
|
+
result.currentGroup,
|
|
961
|
+
result.oldNextSibling,
|
|
962
|
+
svgCanvas.getSvgContent()
|
|
963
|
+
)
|
|
964
|
+
)
|
|
909
965
|
return true
|
|
910
966
|
}
|
|
911
967
|
return false
|
|
@@ -926,7 +982,13 @@ export const setLayerVisibility = (layerName, bVisible) => {
|
|
|
926
982
|
const layer = drawing.setLayerVisibility(layerName, bVisible)
|
|
927
983
|
if (layer) {
|
|
928
984
|
const oldDisplay = prevVisibility ? 'inline' : 'none'
|
|
929
|
-
svgCanvas.addCommandToHistory(
|
|
985
|
+
svgCanvas.addCommandToHistory(
|
|
986
|
+
new ChangeElementCommand(
|
|
987
|
+
layer,
|
|
988
|
+
{ display: oldDisplay },
|
|
989
|
+
'Layer Visibility'
|
|
990
|
+
)
|
|
991
|
+
)
|
|
930
992
|
} else {
|
|
931
993
|
return false
|
|
932
994
|
}
|
|
@@ -946,12 +1008,14 @@ export const setLayerVisibility = (layerName, bVisible) => {
|
|
|
946
1008
|
* @param {string} layerName - The name of the layer you want to which you want to move the selected elements
|
|
947
1009
|
* @returns {boolean} Whether the selected elements were moved to the layer.
|
|
948
1010
|
*/
|
|
949
|
-
export const moveSelectedToLayer =
|
|
1011
|
+
export const moveSelectedToLayer = layerName => {
|
|
950
1012
|
const { BatchCommand, MoveElementCommand } = svgCanvas.history
|
|
951
1013
|
// find the layer
|
|
952
1014
|
const drawing = svgCanvas.getCurrentDrawing()
|
|
953
1015
|
const layer = drawing.getLayerByName(layerName)
|
|
954
|
-
if (!layer) {
|
|
1016
|
+
if (!layer) {
|
|
1017
|
+
return false
|
|
1018
|
+
}
|
|
955
1019
|
|
|
956
1020
|
const batchCmd = new BatchCommand('Move Elements to Layer')
|
|
957
1021
|
|
|
@@ -960,12 +1024,16 @@ export const moveSelectedToLayer = (layerName) => {
|
|
|
960
1024
|
let i = selElems.length
|
|
961
1025
|
while (i--) {
|
|
962
1026
|
const elem = selElems[i]
|
|
963
|
-
if (!elem) {
|
|
1027
|
+
if (!elem) {
|
|
1028
|
+
continue
|
|
1029
|
+
}
|
|
964
1030
|
const oldNextSibling = elem.nextSibling
|
|
965
1031
|
// TODO: this is pretty brittle!
|
|
966
1032
|
const oldLayer = elem.parentNode
|
|
967
1033
|
layer.append(elem)
|
|
968
|
-
batchCmd.addSubCommand(
|
|
1034
|
+
batchCmd.addSubCommand(
|
|
1035
|
+
new MoveElementCommand(elem, oldNextSibling, oldLayer)
|
|
1036
|
+
)
|
|
969
1037
|
}
|
|
970
1038
|
|
|
971
1039
|
svgCanvas.addCommandToHistory(batchCmd)
|
|
@@ -978,7 +1046,7 @@ export const moveSelectedToLayer = (layerName) => {
|
|
|
978
1046
|
* @param {module:history.HistoryRecordingService} hrService
|
|
979
1047
|
* @returns {void}
|
|
980
1048
|
*/
|
|
981
|
-
export const mergeLayer =
|
|
1049
|
+
export const mergeLayer = hrService => {
|
|
982
1050
|
svgCanvas.getCurrentDrawing().mergeLayer(historyRecordingService(hrService))
|
|
983
1051
|
svgCanvas.clearSelection()
|
|
984
1052
|
leaveContext()
|
|
@@ -990,8 +1058,10 @@ export const mergeLayer = (hrService) => {
|
|
|
990
1058
|
* @param {module:history.HistoryRecordingService} hrService
|
|
991
1059
|
* @returns {void}
|
|
992
1060
|
*/
|
|
993
|
-
export const mergeAllLayers =
|
|
994
|
-
svgCanvas
|
|
1061
|
+
export const mergeAllLayers = hrService => {
|
|
1062
|
+
svgCanvas
|
|
1063
|
+
.getCurrentDrawing()
|
|
1064
|
+
.mergeAllLayers(historyRecordingService(hrService))
|
|
995
1065
|
svgCanvas.clearSelection()
|
|
996
1066
|
leaveContext()
|
|
997
1067
|
svgCanvas.changeSvgContent()
|
|
@@ -1032,7 +1102,7 @@ export const leaveContext = () => {
|
|
|
1032
1102
|
* @fires module:svgcanvas.SvgCanvas#event:contextset
|
|
1033
1103
|
* @returns {void}
|
|
1034
1104
|
*/
|
|
1035
|
-
export const setContext =
|
|
1105
|
+
export const setContext = elem => {
|
|
1036
1106
|
const dataStorage = svgCanvas.getDataStorage()
|
|
1037
1107
|
leaveContext()
|
|
1038
1108
|
if (typeof elem === 'string') {
|
|
@@ -1046,9 +1116,12 @@ export const setContext = (elem) => {
|
|
|
1046
1116
|
const parentsUntil = getParentsUntil(elem, '#svgcontent')
|
|
1047
1117
|
const siblings = []
|
|
1048
1118
|
parentsUntil.forEach(function (parent) {
|
|
1049
|
-
const elements = Array.prototype.filter.call(
|
|
1050
|
-
|
|
1051
|
-
|
|
1119
|
+
const elements = Array.prototype.filter.call(
|
|
1120
|
+
parent.parentNode.children,
|
|
1121
|
+
function (child) {
|
|
1122
|
+
return child !== parent
|
|
1123
|
+
}
|
|
1124
|
+
)
|
|
1052
1125
|
elements.forEach(function (element) {
|
|
1053
1126
|
siblings.push(element)
|
|
1054
1127
|
})
|
package/core/event.js
CHANGED