|
113 | 113 | */
|
114 | 114 | EditorUi.mermaidDiagramTypes = ['flowchart', 'classDiagram', 'sequenceDiagram',
|
115 | 115 | 'stateDiagram', 'mindmap', 'graph', 'erDiagram', 'requirementDiagram',
|
116 |
| - 'journey', 'gantt', 'pie', 'gitGraph', 'C4Context']; |
| 116 | + 'journey', 'gantt', 'pie', 'gitGraph']; |
117 | 117 |
|
118 | 118 | /**
|
119 | 119 | * Default Mermaid config without using foreign objects in flowcharts.
|
|
7942 | 7942 | */
|
7943 | 7943 | EditorUi.prototype.generateOpenAiMermaidDiagram = function(key, prompt, success, error)
|
7944 | 7944 | {
|
7945 |
| - if (this.spinner.spin(document.body, mxResources.get('loading'))) |
| 7945 | + var maxRetries = 3; |
| 7946 | + var retryCount = 0; |
| 7947 | + |
| 7948 | + var fn = mxUtils.bind(this, function() |
7946 | 7949 | {
|
7947 |
| - this.createTimeout(15000, mxUtils.bind(this, function(timeout) |
| 7950 | + if (this.spinner.spin(document.body, mxResources.get('loading'))) |
7948 | 7951 | {
|
7949 |
| - EditorUi.logEvent({category: 'OPENAI-DIAGRAM', |
7950 |
| - action: 'generateOpenAiMermaidDiagram', |
7951 |
| - label: prompt}); |
7952 |
| - var url = 'https://api.openai.com/v1/engines/text-davinci-003/completions'; |
| 7952 | + this.createTimeout(40000, mxUtils.bind(this, function(timeout) |
| 7953 | + { |
| 7954 | + EditorUi.logEvent({category: 'OPENAI-DIAGRAM', |
| 7955 | + action: 'generateOpenAiMermaidDiagram', |
| 7956 | + label: prompt}); |
| 7957 | + var url = 'https://api.openai.com/v1/engines/text-davinci-003/completions'; |
7953 | 7958 |
|
7954 |
| - var params = { |
7955 |
| - prompt: prompt, |
7956 |
| - temperature: 0.9, |
7957 |
| - max_tokens: 4000 |
7958 |
| - }; |
| 7959 | + var params = { |
| 7960 | + prompt: prompt, |
| 7961 | + temperature: 0.9, |
| 7962 | + max_tokens: 4000 |
| 7963 | + }; |
7959 | 7964 |
|
7960 |
| - var req = new mxXmlRequest(url, JSON.stringify(params), 'POST'); |
7961 |
| - |
7962 |
| - req.setRequestHeaders = mxUtils.bind(this, function(request, params) |
7963 |
| - { |
7964 |
| - request.setRequestHeader('Authorization', 'Bearer ' + key); |
7965 |
| - request.setRequestHeader('Content-Type', 'application/json'); |
7966 |
| - }); |
| 7965 | + var req = new mxXmlRequest(url, JSON.stringify(params), 'POST'); |
| 7966 | + |
| 7967 | + req.setRequestHeaders = mxUtils.bind(this, function(request, params) |
| 7968 | + { |
| 7969 | + request.setRequestHeader('Authorization', 'Bearer ' + key); |
| 7970 | + request.setRequestHeader('Content-Type', 'application/json'); |
| 7971 | + }); |
7967 | 7972 |
|
7968 |
| - var handleError = mxUtils.bind(this, function(e) |
7969 |
| - { |
7970 |
| - if (timeout.clear()) |
| 7973 | + var handleError = mxUtils.bind(this, function(e) |
7971 | 7974 | {
|
7972 |
| - this.spinner.stop(); |
7973 |
| - error(e); |
7974 |
| - } |
7975 |
| - }); |
| 7975 | + if (timeout.clear()) |
| 7976 | + { |
| 7977 | + this.spinner.stop(); |
| 7978 | + error(e); |
| 7979 | + } |
| 7980 | + }); |
7976 | 7981 |
|
7977 |
| - req.send(mxUtils.bind(this, function(req) |
7978 |
| - { |
7979 |
| - if (timeout.isAlive()) |
| 7982 | + req.send(mxUtils.bind(this, function(req) |
7980 | 7983 | {
|
7981 |
| - if (req.getStatus() >= 200 && req.getStatus() <= 299) |
| 7984 | + if (timeout.isAlive()) |
7982 | 7985 | {
|
7983 |
| - this.tryAndHandle(mxUtils.bind(this, function() |
| 7986 | + if (req.getStatus() >= 200 && req.getStatus() <= 299) |
7984 | 7987 | {
|
7985 |
| - var response = JSON.parse(req.getText()); |
7986 |
| - var text = mxUtils.trim(response.choices[0].text); |
7987 |
| - var result = this.extractMermaidDeclaration(text); |
7988 |
| - |
7989 |
| - this.generateMermaidImage(result, null, mxUtils.bind(this, function(data, w, h) |
| 7988 | + this.tryAndHandle(mxUtils.bind(this, function() |
7990 | 7989 | {
|
7991 |
| - this.tryAndHandle(mxUtils.bind(this, function() |
| 7990 | + var response = JSON.parse(req.getText()); |
| 7991 | + var text = mxUtils.trim(response.choices[0].text); |
| 7992 | + var result = this.extractMermaidDeclaration(text); |
| 7993 | + |
| 7994 | + this.generateMermaidImage(result, null, mxUtils.bind(this, function(data, w, h) |
7992 | 7995 | {
|
7993 |
| - if (timeout.clear()) |
| 7996 | + this.tryAndHandle(mxUtils.bind(this, function() |
7994 | 7997 | {
|
7995 |
| - EditorUi.debug('EditorUi.generateOpenAiMermaidDiagram', |
7996 |
| - '\nrequest:', params, '\nresponse:', response, |
7997 |
| - '\nprompt:', prompt, '\noutput:', text, |
7998 |
| - '\nresult:', result); |
7999 |
| - |
8000 |
| - this.spinner.stop(); |
8001 |
| - success(result, data, w, h); |
| 7998 | + if (timeout.clear()) |
| 7999 | + { |
| 8000 | + EditorUi.debug('EditorUi.generateOpenAiMermaidDiagram', |
| 8001 | + '\nrequest:', params, '\nresponse:', response, |
| 8002 | + '\nprompt:', prompt, '\noutput:', text, |
| 8003 | + '\nresult:', result); |
| 8004 | + |
| 8005 | + this.spinner.stop(); |
| 8006 | + success(result, data, w, h); |
| 8007 | + } |
| 8008 | + }), handleError); |
| 8009 | + }), handleError, mxUtils.bind(this, function(e) |
| 8010 | + { |
| 8011 | + if (retryCount++ < maxRetries) |
| 8012 | + { |
| 8013 | + if (timeout.clear()) |
| 8014 | + { |
| 8015 | + this.spinner.stop(); |
| 8016 | + fn(); |
| 8017 | + } |
8002 | 8018 | }
|
8003 |
| - }), handleError); |
| 8019 | + else |
| 8020 | + { |
| 8021 | + handleError(e); |
| 8022 | + } |
| 8023 | + })); |
8004 | 8024 | }), handleError);
|
8005 |
| - }), handleError); |
8006 |
| - } |
8007 |
| - else |
8008 |
| - { |
8009 |
| - var e = null; |
8010 |
| - |
8011 |
| - try |
8012 |
| - { |
8013 |
| - e = JSON.parse(req.getText()); |
8014 |
| - e = e.error; |
8015 | 8025 | }
|
8016 |
| - catch (e) |
| 8026 | + else |
8017 | 8027 | {
|
8018 |
| - // ignore |
8019 |
| - } |
| 8028 | + var e = null; |
8020 | 8029 |
|
8021 |
| - handleError(e); |
| 8030 | + try |
| 8031 | + { |
| 8032 | + e = JSON.parse(req.getText()); |
| 8033 | + e = e.error; |
| 8034 | + } |
| 8035 | + catch (e) |
| 8036 | + { |
| 8037 | + // ignore |
| 8038 | + } |
| 8039 | + |
| 8040 | + handleError(e); |
| 8041 | + } |
8022 | 8042 | }
|
8023 |
| - } |
8024 |
| - }), handleError); |
8025 |
| - }), error); |
8026 |
| - } |
| 8043 | + }), handleError); |
| 8044 | + }), error); |
| 8045 | + } |
| 8046 | + }); |
| 8047 | + |
| 8048 | + fn(); |
8027 | 8049 | };
|
8028 | 8050 |
|
8029 | 8051 | /**
|
|
8076 | 8098 | /**
|
8077 | 8099 | * Generates a Mermaid image.
|
8078 | 8100 | */
|
8079 |
| - EditorUi.prototype.generateMermaidImage = function(data, config, success, error) |
| 8101 | + EditorUi.prototype.generateMermaidImage = function(data, config, success, error, parseErrorHandler) |
8080 | 8102 | {
|
8081 | 8103 | var onerror = mxUtils.bind(this, function(e)
|
8082 | 8104 | {
|
|
8109 | 8131 |
|
8110 | 8132 | mermaid.mermaidAPI.initialize(config);
|
8111 | 8133 |
|
8112 |
| - mermaid.mermaidAPI.render('geMermaidOutput-' + new Date().getTime(), data, mxUtils.bind(this, function(svg) |
| 8134 | + mermaid.mermaidAPI.renderAsync('geMermaidOutput-' + new Date().getTime(), data, mxUtils.bind(this, function(svg) |
8113 | 8135 | {
|
8114 | 8136 | try
|
8115 | 8137 | {
|
|
8123 | 8145 | var doc = mxUtils.parseXml(svg);
|
8124 | 8146 | var svgs = doc.getElementsByTagName('svg');
|
8125 | 8147 |
|
8126 |
| - if (svgs.length > 0) |
| 8148 | + if (svgs.length > 0 && svgs[0].getAttribute('aria-roledescription') != 'error') |
8127 | 8149 | {
|
8128 | 8150 | var w = parseFloat(svgs[0].getAttribute('width'));
|
8129 | 8151 | var h = parseFloat(svgs[0].getAttribute('height'));
|
|
8148 | 8170 | }
|
8149 | 8171 | else
|
8150 | 8172 | {
|
8151 |
| - error({message: mxResources.get('invalidInput')}); |
| 8173 | + if (parseErrorHandler != null) |
| 8174 | + { |
| 8175 | + parseErrorHandler(); |
| 8176 | + } |
| 8177 | + else |
| 8178 | + { |
| 8179 | + error({message: mxResources.get('invalidInput')}); |
| 8180 | + } |
8152 | 8181 | }
|
8153 | 8182 | }
|
8154 | 8183 | catch (e)
|
|
8169 | 8198 |
|
8170 | 8199 | if (urlParams['dev'] == '1')
|
8171 | 8200 | {
|
8172 |
| - /*mxMermaidToDrawio.addListener(mxUtils.bind(this, function(modelXml) |
8173 |
| - { |
8174 |
| - this.importXml(modelXml, null, null, null, null, null, true); |
8175 |
| - }));*/ |
| 8201 | + if (urlParams['mermaidToDrawioTest'] == '1') |
| 8202 | + { |
| 8203 | + mxMermaidToDrawio.addListener(mxUtils.bind(this, function(modelXml) |
| 8204 | + { |
| 8205 | + this.importXml(modelXml, null, null, null, null, null, true); |
| 8206 | + })); |
| 8207 | + } |
8176 | 8208 |
|
8177 |
| - mxscript('js/mermaid/mermaid.min.js', delayed, |
8178 |
| - null, null, null, onerror); |
| 8209 | + mxscript('js/mermaid/mermaid.min.js', function() |
| 8210 | + { |
| 8211 | + // Load mindmap plugin in dev only so far |
| 8212 | + mxscript('js/mermaid/mermaid-mindmap.min.js', async function() |
| 8213 | + { |
| 8214 | + await mermaid.registerExternalDiagrams([window['mermaid-mindmap']]); |
| 8215 | + delayed(); |
| 8216 | + }, null, null, null, onerror); |
| 8217 | + }, null, null, null, onerror); |
8179 | 8218 | }
|
8180 | 8219 | else
|
8181 | 8220 | {
|
@@ -14737,21 +14776,23 @@
|
14737 | 14776 |
|
14738 | 14777 | return cells;
|
14739 | 14778 | };
|
14740 |
| - |
| 14779 | + |
14741 | 14780 | /**
|
14742 | 14781 | * Opens the given files in the editor.
|
14743 | 14782 | */
|
14744 | 14783 | EditorUi.prototype.openFileHandle = function(data, name, file, temp, fileHandle)
|
14745 | 14784 | {
|
14746 | 14785 | if (name != null && name.length > 0)
|
14747 | 14786 | {
|
14748 |
| - if (!this.useCanvasForExport && /(\.png)$/i.test(name)) |
| 14787 | + if ((!this.useCanvasForExport && /(\.png)$/i.test(name)) || |
| 14788 | + /(\.pdf)$/i.test(name)) |
14749 | 14789 | {
|
14750 |
| - name = name.substring(0, name.length - 4) + '.drawio'; |
14751 |
| - } |
14752 |
| - else if (/(\.pdf)$/i.test(name)) |
14753 |
| - { |
14754 |
| - name = name.substring(0, name.length - 4) + '.drawio'; |
| 14790 | + name = name.substring(0, name.length - 4); |
| 14791 | + |
| 14792 | + if (!/(\.drawio)$/i.test(name)) |
| 14793 | + { |
| 14794 | + name = name + '.drawio'; |
| 14795 | + } |
14755 | 14796 | }
|
14756 | 14797 |
|
14757 | 14798 | var handleResult = mxUtils.bind(this, function(xml)
|
|
0 commit comments