-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #878 from obgnail/dev
Dev
- Loading branch information
Showing
8 changed files
with
245 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
class drawIOPlugin extends BaseCustomPlugin { | ||
init = () => { | ||
this.hasLoaded = null | ||
this.defaultConfig = this._getDefaultConfig() | ||
} | ||
|
||
callback = anchorNode => this.utils.insertText(anchorNode, this.config.TEMPLATE) | ||
|
||
process = () => { | ||
this.utils.thirdPartyDiagramParser.register({ | ||
lang: this.config.LANGUAGE, | ||
mappingLang: "javascript", | ||
destroyWhenUpdate: false, | ||
interactiveMode: this.config.INTERACTIVE_MODE, | ||
checkSelector: ".plugin-drawio-content", | ||
wrapElement: '<div class="plugin-drawio-content"></div>', | ||
css: { | ||
height: this.config.DEFAULT_FENCE_HEIGHT, | ||
"background-color": this.config.DEFAULT_FENCE_BACKGROUND_COLOR, | ||
}, | ||
lazyLoadFunc: this.lazyLoad, | ||
createFunc: this.create, | ||
updateFunc: null, | ||
destroyFunc: null, | ||
beforeExport: null, | ||
extraStyleGetter: null, | ||
versionGetter: this.versionGetter, | ||
}) | ||
} | ||
|
||
create = async ($wrap, content) => { | ||
const graphConfig = this._getConfig(content) | ||
if (!graphConfig.source && !graphConfig.xml) { | ||
throw new Error("缺失必须的配置项: source") | ||
} | ||
await this._getXML(graphConfig) | ||
$wrap[0].innerHTML = await this._toElement(graphConfig) | ||
this._refresh() | ||
} | ||
|
||
_getConfig = content => new Function(`return (${content})`)() | ||
|
||
_getXML = async graphConfig => { | ||
if (graphConfig.xml) return | ||
|
||
let { source } = graphConfig | ||
const isNetwork = this.utils.isNetworkImage(source) | ||
try { | ||
if (isNetwork) { | ||
graphConfig.xml = await this._memorizedFetch(source) | ||
} else { | ||
const dir = this.utils.getCurrentDirPath() | ||
source = this.utils.Package.Path.resolve(dir, source) | ||
graphConfig.xml = await this.utils.Package.Fs.promises.readFile(source, "utf-8") | ||
} | ||
} catch (e) { | ||
const from = isNetwork ? "网络" : "本地" | ||
throw new Error(`读取${from}文件失败: ${source}\n\n${e}`) | ||
} | ||
} | ||
|
||
_toElement = graphConfig => { | ||
const mxGraphData = { ...this.defaultConfig, ...graphConfig } | ||
const jsonString = JSON.stringify(mxGraphData) | ||
const escaped = this.utils.escape(jsonString) | ||
return `<div class="mxgraph" style="max-width:100%; width:100%; margin-top: 26px;" data-mxgraph="${escaped}"></div>` | ||
} | ||
|
||
_refresh = this.utils.debounce(() => window.GraphViewer.processElements(), 100) | ||
|
||
_memorizedFetch = this.utils.memorize(async url => { | ||
console.log(`memorized fetch url: ${url}`) | ||
const resp = await this.utils.fetch(url, { timeout: 60 * 1000 }) | ||
return resp.text() | ||
}) | ||
|
||
_getDefaultConfig = (type = "showOnly") => { | ||
const config = { | ||
showOnly: { highlight: "#0000ff", nav: false, resize: false, edit: null, editable: true, lightbox: false, zoom: "1", toolbar: null, "toolbar-nohide": true, }, | ||
editable: { highlight: "#0000ff", nav: false, resize: true, edit: null, editable: false, toolbar: null, "toolbar-nohide": true, }, | ||
showToolbar: { | ||
highlight: "#0000ff", | ||
nav: true, | ||
resize: true, | ||
edit: null, | ||
editable: true, | ||
lightbox: false, | ||
zoom: "1", | ||
toolbar: "zoom lightbox layers", | ||
"toolbar-position": "inline", | ||
"toolbar-nohide": true, | ||
}, | ||
} | ||
return config[type] | ||
} | ||
|
||
lazyLoad = async () => { | ||
if (!this.hasLoaded) { | ||
const from = this.config.RESOURCE_URI | ||
const path = this.utils.isNetworkImage(from) ? from : `file:///${this.utils.Package.Path.resolve(from)}` | ||
await $.getScript(path) | ||
this.hasLoaded = true | ||
|
||
window.GraphViewer.prototype.toolbarZIndex = 7 | ||
} | ||
} | ||
|
||
versionGetter = () => "24.8.9" | ||
} | ||
|
||
module.exports = { | ||
plugin: drawIOPlugin, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.