Découvrez les nouveautés de cette version : Fonctionnalités, améliorations et évolutions vous attendent ! 👉 Cliquez ici pour en savoir plus

Modifications pour le document DiagramEditSheet

Modifié par Admin le 19/03/2025 - 19:24

Depuis la version 8.1
modifié par Admin
sur 10/02/2025 - 13:05
Commentaire de modification : Install extension [com.xwiki.diagram:application-diagram/1.22.0]
À la version 9.1
modifié par Admin
sur 19/03/2025 - 19:24
Commentaire de modification : Install extension [com.xwiki.diagram:application-diagram/1.22.1]

Résumé

Détails

XWiki.JavaScriptExtension[1]
Code
... ... @@ -105,18 +105,19 @@
105 105   var attachmentReference = new XWiki.AttachmentReference(fileName, documentReference);
106 106   var uploadMethod = (diagramConfig.isTemporaryUploadSupported) ? xutils.temporaryUploadAttachment : xutils.uploadAttachment;
107 107   var uploadAttachment = $.proxy(uploadMethod, null, blob, attachmentReference);
108 - // Avoid creating too many versions of the attachment. Upload the attachment even if we failed to delete it first.
109 - return xutils.deleteAttachment(attachmentReference).then(uploadAttachment, uploadAttachment);
108 + return uploadAttachment();
110 110   };
111 111  
112 112   var imageCache = {};
113 - var saveFileAsPNGImageAttachment = function(file) {
112 + var saveFileAsPNGImageAttachment = function(file, index, originalPage) {
114 114   var deferred = $.Deferred();
115 - file.getUi().exportToCanvas(/* callback */ function(canvas) {
114 + let page = file.ui.pages[index];
115 + file.ui.selectPage(page, true, null);
116 + file.getUi().exportToCanvas(/* callback */ function (canvas) {
116 116   if (canvas) {
117 117   try {
118 - canvas.toBlob(function(blob) {
119 - pipeDeferred(saveBlobAsImageAttachment(blob, 'diagram.png', file.documentReference), deferred);
119 + canvas.toBlob(function (blob) {
120 + pipeDeferred(saveBlobAsImageAttachment(blob, `${getXWikiAttachmentName(index)}.png`, file.documentReference), deferred);
120 120   });
121 121   } catch(err) {
122 122   deferred.reject();
... ... @@ -129,13 +129,17 @@
129 129   $jsontool.serialize($services.localization.render('diagram.editor.saveAsImageAttachmentError')), 'error');
130 130   deferred.reject();
131 131   }, /* limitHeight */ null, /* ignoreSelection */ true, /* scale */ diagramConfig.pdfImageExportZoom);
133 + file.ui.selectPage(originalPage, true, null);
132 132   return deferred.promise();
133 133   };
134 134  
135 - var saveFileAsSVGImageAttachment = function(file) {
136 - var deferred = $.Deferred();
137 + var saveFileAsSVGImageAttachment = function(file, index, originalPage) {
138 + var deferred = $.Deferred()
139 + let page = file.ui.pages[index];
140 + file.ui.selectPage(page, true, null);
137 137   var svgRoot = file.ui.editor.graph.getSvg(/* background: */ '#ffffff', /* scale: */ null, /* border: */ null,
138 138   /* nocrop: */ true, /* crisp: */ null, /* ignoreSelection: */ true);
143 + file.ui.selectPage(originalPage, true, null);
139 139   // Embed the images because the PDF exporter might not be able to access them.
140 140   file.ui.convertImages(svgRoot, function() {
141 141   var svg = '<?xml version="1.0" encoding="UTF-8"?>\n' +
... ... @@ -142,28 +142,62 @@
142 142   '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' +
143 143   mxUtils.getXml(svgRoot);
144 144   var blob = new Blob([svg], {type: 'image/svg+xml'});
145 - pipeDeferred(saveBlobAsImageAttachment(blob, 'diagram.svg', file.documentReference), deferred);
150 + pipeDeferred(saveBlobAsImageAttachment(blob, `${getXWikiAttachmentName(index)}.svg`, file.documentReference),
151 + deferred);
146 146   }, imageCache);
147 147   return deferred.promise();
148 148   };
149 149  
150 - var saveFileAsImageAttachments = function(file) {
156 + var saveFileAsImageAttachments = function(file, index, originalPage) {
151 151   // This is a workaround for https://github.com/jgraph/drawio/issues/490
152 152   // Stop editing for getting the latest content from diagram
153 153   file.ui.editor.graph.stopEditing(false);
154 154   // We upload the PNG image even if the SVG upload has failed.
155 - var pngUpload = $.proxy(saveFileAsPNGImageAttachment, null, file);
156 - return saveFileAsSVGImageAttachment(file).then(pngUpload, pngUpload);
161 + var pngUpload = $.proxy(saveFileAsPNGImageAttachment, null, file, index, originalPage);
162 + return saveFileAsSVGImageAttachment(file, index, originalPage).then(pngUpload, pngUpload);
157 157   };
158 158  
165 + var getXWikiAttachmentName = function (index) {
166 + if (index == 0)
167 + {
168 + // If is the first page we keep the old naming strategy -> diagram.png / diagram.svg
169 + return "diagram";
170 + }
171 + return `diagram${index+1}`;
172 + };
173 +
174 + var deleteAllDiagrams = function() {
175 + let baseUrl = `/xwiki/rest/diagram/${xm.documentReference.toString()}`;
176 + let queryString = $.param({"form_token": xm.form_token});
177 + return $.post(`${baseUrl}?${queryString}`);
178 + };
179 +
159 159   var saveFilesAsImageAttachments = function() {
160 - var uploadDeferred = $.Deferred().resolve();
161 - forEachOpenedFile(function(file) {
162 - // We do the next upload even if the previous uploads have failed.
163 - var nextUpload = $.proxy(saveFileAsImageAttachments, null, file);
164 - uploadDeferred = uploadDeferred.then(nextUpload, nextUpload);
181 + var uploadDeferredList = []
182 +
183 + forEachOpenedFile(function (file) {
184 + var uploadDeferred = $.Deferred().resolve();
185 + // Get the current page so we know where to return after saving.
186 + let originalPage = file.ui.currentPage;
187 +
188 + file.ui.pages.forEach(function (page, index) {
189 + // We do the next upload even if the previous uploads have failed.
190 + var nextUpload = $.proxy(saveFileAsImageAttachments, null, file, index,originalPage);
191 + uploadDeferred = uploadDeferred.then(nextUpload, nextUpload);
192 + uploadDeferredList.push(uploadDeferred.promise());
193 + });
165 165   });
166 - return uploadDeferred.promise();
195 + // Wait for all deferreds to complete.
196 + return $.when.apply($, uploadDeferredList).then(
197 + function () {
198 + // Resolve the overall promise if all succeeded.
199 + return { status: 'success' };
200 + },
201 + function () {
202 + // Reject the overall promise if any failed.
203 + return $.Deferred().reject({ status: 'fail' });
204 + }
205 + ).promise();
167 167   };
168 168  
169 169   var uploadInProgress = false;
... ... @@ -175,10 +175,13 @@
175 175   event.stopPropagation();
176 176   var saveButton = $(event.target);
177 177   saveButton.prop('disabled', true);
178 - saveFilesAsImageAttachments().fail(function(e) {
217 + deleteAllDiagrams()
218 + .then(saveFilesAsImageAttachments)
219 + .catch(function(e) {
179 179   new XWiki.widgets.Notification(
180 180   $jsontool.serialize($services.localization.render('diagram.editor.saveAsImageAttachmentError')), 'error');
181 - }).always(function() {
222 + })
223 + .always(function() {
182 182   saveButton.prop('disabled', false).click();
183 183   });
184 184   } else {
Parser le contenu
... ... @@ -1,1 +1,1 @@
1 -Oui
1 +Non
XWiki.JavaScriptExtension[2]
Code
... ... @@ -18,6 +18,29 @@
18 18   a.nodeName.toLowerCase() != b.toLowerCase() ? !1 : null == c || a.getAttribute(c) == d;
19 19   };
20 20  
21 +/**
22 + * TODO: When upgrading, make sure to check if this method has changed.
23 + * This method was copied directly from draw.io, and the only change made was the call to this.showDialog.
24 + * Both the width and height were modified to fit better within the XWiki UI.
25 + */
26 +EditorUi.prototype.showBackgroundImageDialog = function (apply, img, color, showColor) {
27 + apply = (apply != null) ? apply : mxUtils.bind(this, function (image, failed, color, shadowVisible) {
28 + if (!failed) {
29 + var change = new ChangePageSetup(this, (showColor) ? color : null, image);
30 + change.ignoreColor = !showColor;
31 + if (shadowVisible != null && showColor) {
32 + change.shadowVisible = shadowVisible;
33 + }
34 + this.editor.graph.model.execute(change);
35 + }
36 + });
37 +
38 + var dlg = new BackgroundImageDialog(this, apply, img, color, showColor);
39 + this.showDialog(dlg.container, 420, (showColor) ? 260 : 240, true, true);
40 + dlg.init();
41 +};
42 +
43 +
21 21   EditorUi.prototype.showLinkDialog = function(value, selectLabel, callback, showNewWindowOption, linkTarget) {
22 22   var resourceReference = diagramLinkHandler.getResourceReferenceFromCustomLink(value);
23 23   // We append the modal to the body element in order to fix Issue #108: "Inserting a link in full screen mode is not
... ... @@ -371,8 +371,7 @@
371 371   //
372 372   // Change the service name in order to disable notifications.
373 373   //
374 - EditorUi.prototype.getServiceName = function()
375 - {
397 + EditorUi.prototype.getServiceName = function() {
376 376   return 'xwiki.com';
377 377   };
378 378