xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
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
/* global svgCanvas, jQuery, Components, netscape */
// Note: This JavaScript file must be included as the last script on the main HTML editor page to override the open/save handlers
jQuery(function () {
  if (!window.Components) return;
 
  function mozFilePicker (readflag) {
    const fp = window.Components.classes['@mozilla.org/filepicker;1']
      .createInstance(Components.interfaces.nsIFilePicker);
    if (readflag) fp.init(window, 'Pick a SVG file', fp.modeOpen);
    else fp.init(window, 'Pick a SVG file', fp.modeSave);
    fp.defaultExtension = '*.svg';
    fp.show();
    return fp.file;
  }
 
  svgCanvas.setCustomHandlers({
    open () {
      try {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
        const file = mozFilePicker(true);
        if (!file) {
          return null;
        }
 
        const inputStream = Components.classes['@mozilla.org/network/file-input-stream;1'].createInstance(Components.interfaces.nsIFileInputStream);
        inputStream.init(file, 0x01, parseInt('00004', 8), null);
        const sInputStream = Components.classes['@mozilla.org/scriptableinputstream;1'].createInstance(Components.interfaces.nsIScriptableInputStream);
        sInputStream.init(inputStream);
        svgCanvas.setSvgString(sInputStream.read(sInputStream.available()));
      } catch (e) {
        console.log('Exception while attempting to load' + e);
      }
    },
    save (svg, str) {
      try {
        const file = mozFilePicker(false);
        if (!file) {
          return;
        }
 
        if (!file.exists()) {
          file.create(0, parseInt('0664', 8));
        }
 
        const out = Components.classes['@mozilla.org/network/file-output-stream;1'].createInstance(Components.interfaces.nsIFileOutputStream);
        out.init(file, 0x20 | 0x02, parseInt('00004', 8), null);
        out.write(str, str.length);
        out.flush();
        out.close();
      } catch (e) {
        alert(e);
      }
    }
  });
});