|
1 | 1 | import _ from 'lodash'; |
2 | 2 | import chroma from 'chroma-js'; |
3 | | -import { inflateRaw, deflateRaw } from 'pako'; |
4 | 3 | import { FlowchartCtrl } from 'flowchart_ctrl'; |
5 | 4 | import { GFEvents } from 'flowcharting_base'; |
6 | 5 | import { nanoid } from 'nanoid/non-secure'; |
@@ -831,135 +830,6 @@ export class GFPlugin { |
831 | 830 | // } |
832 | 831 | // } |
833 | 832 |
|
834 | | -export class GFDrawioTools { |
835 | | - static parseXml(xmlString: string): Document { |
836 | | - var parser = new DOMParser(); |
837 | | - return parser.parseFromString(xmlString, 'text/xml'); |
838 | | - } |
839 | | - /** |
840 | | - * drawio context source |
841 | | - * @param {Object} node |
842 | | - * @returns string |
843 | | - */ |
844 | | - static getTextContent(node: Object): string { |
845 | | - // TODO : fixit |
846 | | - const _node: any = node; |
847 | | - return _node != null ? _node[_node.hasOwnProperty('textContent') === undefined ? 'text' : 'textContent'] : ''; |
848 | | - } |
849 | | - |
850 | | - /** |
851 | | - * Valided XML definition |
852 | | - * |
853 | | - * @static |
854 | | - * @param {string} source |
855 | | - * @returns |
856 | | - * @memberof XGraph |
857 | | - */ |
858 | | - static isValidXml(source: string) { |
859 | | - try { |
860 | | - const div = document.createElement('div'); |
861 | | - const g = new Graph(div); |
862 | | - if (GFDrawioTools.isEncoded(source)) { |
863 | | - source = GFDrawioTools.decode(source); |
864 | | - } |
865 | | - const xmlDoc = mxUtils.parseXml(source); |
866 | | - const codec = new mxCodec(xmlDoc); |
867 | | - g.getModel().beginUpdate(); |
868 | | - codec.decode(xmlDoc.documentElement, g.getModel()); |
869 | | - g.getModel().endUpdate(); |
870 | | - g.destroy(); |
871 | | - return true; |
872 | | - } catch (error) { |
873 | | - GFLog.error('isValidXml', error); |
874 | | - return false; |
875 | | - } |
876 | | - } |
877 | | - |
878 | | - static decode(data: string): string { |
879 | | - try { |
880 | | - let node = this.parseXml(data).documentElement; |
881 | | - if (node != null && node.nodeName === 'mxfile') { |
882 | | - var diagrams = node.getElementsByTagName('diagram'); |
883 | | - if (diagrams.length > 0) { |
884 | | - data = this.getTextContent(diagrams[0]); |
885 | | - } |
886 | | - } |
887 | | - } catch (e) { |
888 | | - GFLog.error(`parseXml : Unable to decode ${data}`); |
889 | | - return ''; |
890 | | - } |
891 | | - // data = atob(data); |
892 | | - data = Buffer.from(data, 'base64').toString('binary'); |
893 | | - if (data.length > 0) { |
894 | | - try { |
895 | | - data = inflateRaw( |
896 | | - Uint8Array.from(data, (c) => c.charCodeAt(0)), |
897 | | - { to: 'string' } |
898 | | - ); |
899 | | - } catch (e) { |
900 | | - GFLog.error(`Pako : Unable to decode ${data}`); |
901 | | - return ''; |
902 | | - } |
903 | | - } |
904 | | - |
905 | | - try { |
906 | | - data = decodeURIComponent(data); |
907 | | - } catch (e) { |
908 | | - GFLog.error(`Unable to decode ${data}`); |
909 | | - return ''; |
910 | | - } |
911 | | - return data; |
912 | | - } |
913 | | - |
914 | | - static encode(data: string) { |
915 | | - { |
916 | | - try { |
917 | | - data = encodeURIComponent(data); |
918 | | - } catch (e) { |
919 | | - GFLog.error(`Unable to encode/encodeURIComponent : ${data}`, e); |
920 | | - return; |
921 | | - } |
922 | | - |
923 | | - if (data.length > 0) { |
924 | | - try { |
925 | | - let deflateRaws = deflateRaw(data); |
926 | | - data = String.fromCharCode.apply(null, new Array(...deflateRaws)); |
927 | | - } catch (e) { |
928 | | - console.log(e); |
929 | | - GFLog.error(`Unable to encode ${data}`); |
930 | | - return ''; |
931 | | - } |
932 | | - } |
933 | | - |
934 | | - try { |
935 | | - data = Buffer.from(data, 'binary').toString('base64'); |
936 | | - } catch (e) { |
937 | | - GFLog.error(`Unable to encode ${data}`); |
938 | | - return; |
939 | | - } |
940 | | - |
941 | | - return data; |
942 | | - } |
943 | | - } |
944 | | - |
945 | | - static isEncoded(data: string) { |
946 | | - try { |
947 | | - const node = this.parseXml(data).documentElement; |
948 | | - if (node != null && node.nodeName === 'mxfile') { |
949 | | - const diagrams = node.getElementsByTagName('diagram'); |
950 | | - if (diagrams.length > 0) { |
951 | | - return true; |
952 | | - } |
953 | | - } else { |
954 | | - return data.indexOf('mxGraphModel') === -1; |
955 | | - } |
956 | | - } catch (error) { |
957 | | - return true; |
958 | | - } |
959 | | - return false; |
960 | | - } |
961 | | -} |
962 | | - |
963 | 833 | export class $GF { |
964 | 834 | uid = `GFGlobal-${nanoid()}`; |
965 | 835 | private _globalvars: GFVariables = GFVariables.create(); |
|
0 commit comments