-
Notifications
You must be signed in to change notification settings - Fork 3
/
SVGEditor.js
192 lines (155 loc) · 5.47 KB
/
SVGEditor.js
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
/*** SVG Editor ***/
// Adds a button to SVG file pages to view and edit their source code
// Documentation at [[en:w:User:BrandonXLF/SVGEditor]]
// By [[en:w:User:BrandonXLF]]
mw.loader.using(['oojs-ui', 'jquery.textSelection']).then(function() {
function showEditor(text) {
var isCommons = !!$('#ca-view-foreign').length,
lineWrap = new OO.ui.ButtonInputWidget({
label: 'Toggle line wrap',
icon: 'newline',
id: 'svgeditor-linewrap-btn',
framed: false
}),
code = new OO.ui.MultilineTextInputWidget({
rows: 25,
name: 'wpTextbox1',
id: 'svgeditor'
}),
comment = new OO.ui.TextInputWidget({
id: 'svgeditor-comment',
label: 'Comment',
labelPosition: 'before'
}),
save = new OO.ui.ButtonInputWidget({
label: 'Save',
flags: ['primary', 'progressive'],
icon: isCommons ? 'logoWikimediaCommons' : void 0,
title: isCommons ? 'Save To Wikimedia Commons' : void 0
}),
preview = new OO.ui.ButtonInputWidget({
label: 'Preview',
flags: ['progressive']
}),
cancel = new OO.ui.ButtonInputWidget({
label: 'Cancel',
flags: ['destructive']
}),
buttons = new OO.ui.HorizontalLayout({
items: [save, preview, cancel]
}),
img,
container = $('<div>');
mw.loader.load('oojs-ui.styles.icons-editing-advanced');
lineWrap.on('click', function() {
code.$input.toggleClass('line-wrap');
});
mw.config.set('wgCodeEditorCurrentLanguage', 'svg');
code.$input.attr('id', 'wpTextbox1');
code.$input.textSelection('setContents', text);
if (isCommons) mw.loader.load('oojs-ui.styles.icons-wikimedia');
save.on('click', function() {
var api = isCommons ? new mw.ForeignApi('https://commons.wikimedia.org/w/api.php') : new mw.Api(),
req = api.upload(new Blob([code.$input.textSelection('getContents')], {type: 'image/svg+xml'}), {
filename: mw.config.get('wgTitle'),
comment: comment.getValue(),
ignorewarnings: 1
});
req.always(function(errOrRes, resWhenErr) {
var res = resWhenErr || errOrRes;
if (res.error) {
mw.notify(api.getErrorMessage(res), {type: 'error'});
return;
}
mw.notify('File saved! Loading updated page...', {type: 'success'});
$.get().then(function(html) {
var doc = new DOMParser().parseFromString(html, 'text/html');
$('#content').replaceWith($('#content', doc).prepend(container));
});
});
});
preview.on('click', function() {
if (!img) {
img = $('<img>');
img.css('max-height', '15em');
container.append(img);
}
img.attr('src', 'data:image/svg+xml,' + encodeURIComponent(code.$input.textSelection('getContents')));
});
cancel.on('click', function() {
$('#svgeditor-container').remove();
});
container.attr('id', 'svgeditor-container').addClass('content').append(
lineWrap.$element,
code.$element,
comment.$element,
buttons.$element
);
mw.util.$content.prepend(container);
mw.loader.using(['ext.wikiEditor'], function() {
mw.addWikiEditor(code.$input);
if (mw.loader.getState('ext.codeEditor') === 'ready') {
mw.loader.state({'ext.codeEditor': 'loaded'});
}
mw.loader.load(['ext.codeEditor']);
$.wikiEditor.modules.editSVGTools = {
req: ['editSVGTools']
};
$.wikiEditor.extensions.editSVGTools = function(ctx) {
lineWrap.$element.remove();
ctx.api.addToToolbar(ctx, {
section: 'main',
groups: {
'svgeditor-tools': {
tools: {
lineWrap: {
label: 'Toggle line wrap',
type: 'toggle',
oouiIcon: 'newline',
action: {
type: 'callback',
execute: function() {
code.$input.toggleClass('line-wrap');
}
}
}
}
},
}
});
};
code.$input.wikiEditor('addModule', 'editSVGTools');
});
}
$(function() {
if (mw.config.get('wgNamespaceNumber') !== 6) return;
var url = $('#file > a').attr('href');
if (!/.+svg$/.test(url)) return;
var link;
if (mw.config.get('skin') === 'minerva') {
mw.loader.load('oojs-ui.styles.icons-editing-advanced');
link = $(mw.util.addPortletLink('page-actions', '#', 'Edit SVG', 'page-actions-svgeditor', '', '', '#page-actions-edit'));
var iconUrl ='/w/load.php?modules=oojs-ui.styles.icons-editing-advanced&image=markup&format=rasterized';
mw.loader.addStyleTag('#page-actions-svgeditor .mw-ui-icon:before { background: url(' + iconUrl + '); }');
} else {
link = $(mw.util.addPortletLink('p-views', '#', 'Edit SVG', 'ca-svgeditor', '', '', '#ca-edit'));
}
link.on('click', function(e) {
e.preventDefault();
$('#svgeditor-container').remove();
$.get(url, null, null, 'text').then(showEditor);
});
mw.loader.addStyleTag(
'#svgeditor, #svgeditor-comment { width: unset !important; max-width: unset !important; marin: none !important; }' +
'#svgeditor textarea { font-family: monospace; resize: vertical; white-space: nowrap; }' +
'#svgeditor textarea.line-wrap { white-space: normal; }' +
'#svgeditor .wikiEditor-ui textarea { border: none !important; }' +
'#svgeditor-container > * { margin: 1em 0; }' +
'.skin-minerva #svgeditor-container { padding: 1em; }' +
'.skin-minerva #svgeditor-linewrap-btn { margin-bottom: -0.5em; }' +
'#svgeditor .group-insert, #svgeditor .group-format, #svgeditor .sections { display: none; }' +
'#svgeditor .tabs span.tab-advanced, #svgeditor .tabs span.tab-characters, #svgeditor .tabs span.tab-help { display: none; }' +
'.codeEditor-ui-toolbar .group-svgeditor-tools { display: none; }'
);
});
});