Repository navigation
Expand file tree
/
Copy patheditor-code.js
More file actions
118 lines (96 loc) · 4.61 KB
/
Copy patheditor-code.js
File metadata and controls
118 lines (96 loc) · 4.61 KB
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
/**
* VectorCutter - SVG Editor Module
* Module: editor-code.js
* Description: Live SVG Code Inspection, Code Modal & Two-Way Code Editing
*/
Object.assign(SVGEditor.prototype, {
async showCode() {
// Must await here too
const code = await this.getCleanSVG();
document.getElementById('svgCode').value = code;
document.getElementById('codeModal').style.display = 'flex';
},
toggleCodeEditor() {
const editBtn = document.getElementById('editCodeBtn');
const propertiesPanel = document.getElementById('propertiesPanel');
const codeEditorPanel = document.getElementById('codeEditorPanel');
const textarea = document.getElementById('liveSvgCode');
this.isCodeEditing = !this.isCodeEditing;
if (this.isCodeEditing) {
// Switch to code view
editBtn.classList.add('active');
propertiesPanel.style.display = 'none';
codeEditorPanel.style.display = 'flex';
// 1. Get RAW source elements directly from canvas (not the export version)
const userElements = this.getUserElements();
// 2. Serialize them to a string
const rawSource = userElements.map(el => {
// Clone to ensure we don't modify the live canvas yet
const clone = el.cloneNode(true);
// Clean up temporary UI styles (outline, cursor)
// This prevents internal editor states from appearing in the code
clone.style.outline = '';
clone.style.cursor = '';
// Clean up temporary data attributes if present
if (clone.hasAttribute('data-original-fill')) clone.removeAttribute('data-original-fill');
if (clone.hasAttribute('data-original-stroke')) clone.removeAttribute('data-original-stroke');
return clone.outerHTML;
}).join('\n');
// 3. Format nicely for readability
const formatted = rawSource
.replace(/>/g, '>\n') // Newline after closing tag
.replace(/</g, '\n<') // Newline before opening tag
.replace(/\n\n/g, '\n') // Remove empty lines
.trim();
textarea.value = formatted;
} else {
// Switch back to properties view
editBtn.classList.remove('active');
codeEditorPanel.style.display = 'none';
propertiesPanel.style.display = 'flex';
// Refresh properties view
this.updateContextMenu();
}
},
applyCodeToCanvas() {
const textarea = document.getElementById('liveSvgCode');
const rawCode = textarea.value;
try {
// Create a temporary container to parse HTML
const tempDiv = document.createElement('div');
tempDiv.innerHTML = `<svg>${rawCode}</svg>`;
const newElements = Array.from(tempDiv.querySelector('svg').children);
// Validate basic structure
if (newElements.length === 0 && rawCode.trim().length > 0) {
throw new Error("No valid SVG elements found.");
}
// Clear existing user elements
const userElements = this.getUserElements();
userElements.forEach(el => el.remove());
// Append new elements to canvas (before selection group)
// We need to insert them before the selection group to keep selection on top
newElements.forEach(el => {
// Clone to ensure we own the node
const clone = el.cloneNode(true);
this.canvas.insertBefore(clone, this.selectionGroup);
});
// Clear selection as references are lost
this.clearSelection();
// Flash success or something subtle
const applyBtn = document.getElementById('applyCodeBtn');
const originalText = applyBtn.textContent;
applyBtn.textContent = "Applied!";
applyBtn.style.background = "#28a745";
setTimeout(() => {
applyBtn.textContent = originalText;
applyBtn.style.background = "#007acc";
}, 1000);
// Update lists
this.updateContextMenu();
} catch (e) {
alert("Error parsing SVG code: " + e.message);
}
}
});