svg-edit.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Copyright (C) 2010 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. our ($q, %Action, %Page, $OpenPageName, @MyInitVariables, $UploadAllowed, @UploadTypes, $FullUrl, $HtmlHeaders);
  17. our ($SvgMimeType, $SvgEditorUrl);
  18. AddModuleDescription('svg-edit.pl', 'SVG Editor Extension');
  19. $SvgMimeType = 'image/svg+xml';
  20. $SvgEditorUrl = 'http://svg-edit.googlecode.com/svn/tags/stable/editor/svg-editor.html';
  21. push (@MyInitVariables, \&SvgInitVariables);
  22. sub SvgInitVariables {
  23. $UploadAllowed = 1;
  24. push (@UploadTypes, $SvgMimeType)
  25. unless grep {$_ eq $SvgMimeType} @UploadTypes;
  26. }
  27. *OldSvgGetDownloadLink = \&GetDownloadLink;
  28. *GetDownloadLink = \&NewSvgGetDownloadLink;
  29. sub NewSvgGetDownloadLink {
  30. my ($name, $image, $revision, $alt) = @_;
  31. return OldSvgGetDownloadLink(@_) if $image != 1;
  32. # determine if this is SVG data we need to show in an iframe
  33. my $data;
  34. local (%Page, $OpenPageName);
  35. OpenPage($name);
  36. if ($revision) {
  37. $data = GetTextRevision($revision)->{text}; # ignore revision reset
  38. } else {
  39. $data = $Page{text};
  40. }
  41. return OldSvgGetDownloadLink(@_) unless SvgItIs($data);
  42. my ($width, $height) = SvgDimensions($data);
  43. # add 20 to compensate for scrollbars?
  44. return $q->iframe({-width => $width + 20, -height => $height + 20,
  45. -src => OldSvgGetDownloadLink($name, 2, $revision)}, "");
  46. }
  47. sub SvgItIs {
  48. my ($type) = TextIsFile(shift);
  49. return $type eq $SvgMimeType;
  50. }
  51. sub SvgDimensions {
  52. my $data = shift;
  53. $data =~ s/.*\n//; # strip first line
  54. require MIME::Base64;
  55. $data = MIME::Base64::decode($data);
  56. # crude hack to avoid parsing the SVG XML
  57. my ($x) = $data =~ /width="(.*?)"/;
  58. my ($y) = $data =~ /height="(.*?)"/;
  59. return $x, $y;
  60. }
  61. *OldSvgGetEditForm = \&GetEditForm;
  62. *GetEditForm = \&NewSvgGetEditForm;
  63. sub NewSvgGetEditForm {
  64. my $html = OldSvgGetEditForm(@_);
  65. my $action = 'action=svg;id=' . UrlEncode($OpenPageName);
  66. $action .= ";revision=" . GetParam('revision', '')
  67. if GetParam('revision', '');
  68. my $link = ScriptLink($action, T('Edit image in the browser'), 'svg');
  69. my $text1 = T('Replace this file with text');
  70. my $text2 = T('Replace this text with a file');
  71. $html =~ s!($text1|$text2)</a>!$1</a> $link!;
  72. return $html;
  73. }
  74. $Action{svg} = \&DoSvg;
  75. sub DoSvg {
  76. my $id = shift;
  77. my $summary = T('Summary of your changes:') . ' ';
  78. $HtmlHeaders .= qq{
  79. <script type="text/javascript">
  80. var keyStr =
  81. "ABCDEFGHIJKLMNOP" +
  82. "QRSTUVWXYZabcdef" +
  83. "ghijklmnopqrstuv" +
  84. "wxyz0123456789+/" +
  85. "=";
  86. function encode64(input) {
  87. var output = "";
  88. var chr1, chr2, chr3 = "";
  89. var enc1, enc2, enc3, enc4 = "";
  90. var i = 0;
  91. do {
  92. chr1 = input.charCodeAt(i++);
  93. chr2 = input.charCodeAt(i++);
  94. chr3 = input.charCodeAt(i++);
  95. enc1 = chr1 >> 2;
  96. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  97. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  98. enc4 = chr3 & 63;
  99. if (isNaN(chr2)) {
  100. enc3 = enc4 = 64;
  101. } else if (isNaN(chr3)) {
  102. enc4 = 64;
  103. }
  104. output = output +
  105. keyStr.charAt(enc1) +
  106. keyStr.charAt(enc2) +
  107. keyStr.charAt(enc3) +
  108. keyStr.charAt(enc4);
  109. chr1 = chr2 = chr3 = "";
  110. enc1 = enc2 = enc3 = enc4 = "";
  111. } while (i < input.length);
  112. return output;
  113. }
  114. function oddmuseSaveHandler (window, svg) {
  115. window.show_save_warning = false;
  116. var summary = prompt("$summary");
  117. frames['svgeditor'].jQuery.post('$FullUrl', { title: '$id', raw: 1,
  118. summary: summary,
  119. question: 1,
  120. text: '#FILE $SvgMimeType\\n'
  121. + encode64(svg) } );
  122. }
  123. function oddmuseInit () {
  124. if (frames['svgeditor'].svgCanvas != null) {
  125. frames['svgeditor'].svgCanvas.bind("saved", oddmuseSaveHandler);
  126. var elem = document.getElementsByTagName("div");
  127. for (i=0; i<elem.length; i++) {
  128. if (elem[i].className=="sidebar") {
  129. elem[i].style.display='none';
  130. }
  131. }
  132. } else {
  133. window.setTimeout("oddmuseInit()", 1000);
  134. }
  135. }
  136. window.setTimeout("oddmuseInit()", 1000);
  137. </script>
  138. };
  139. print GetHeader('', Ts('Editing %s', $id));
  140. # This only works if editor and file are on the same site, I think.
  141. my $url = GetDownloadLink($id, 2, GetParam('revision', ''));
  142. my $src = $SvgEditorUrl . '?url=' . UrlEncode($url);
  143. print $q->iframe({-src => $src,
  144. -name => 'svgeditor',
  145. -width => "100%",
  146. -height => "500"}, "");
  147. PrintFooter($id, 'edit');
  148. }