-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,47 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
bundle: { | ||
directory: 'modules', | ||
modules: getBundleModuleNames() | ||
extend: '@apostrophecms/widget-type', | ||
options: { | ||
label: 'Mermaid Widget' | ||
}, | ||
init(self) { | ||
self.addMermaidFieldType(); | ||
}, | ||
fields: { | ||
add: { | ||
mermaid: { | ||
type: 'mermaidField', | ||
label: 'Mermaid', | ||
help: 'Enter your mermaid code here' | ||
} | ||
}, | ||
group: { | ||
basics: { | ||
label: 'Basics', | ||
fields: [ | ||
'mermaid' | ||
] | ||
} | ||
} | ||
}, | ||
methods(self) { | ||
return { | ||
addMermaidFieldType() { | ||
self.apos.schema.addFieldType({ | ||
name: 'mermaidField', | ||
convert: self.convertInput, | ||
vueComponent: 'InputMermaid' | ||
}); | ||
}, | ||
async convertInput(req, field, data, object) { | ||
const input = data[field.name]; | ||
if ((data[field.name] == null) || (data[field.name] === '')) { | ||
if (field.required) { | ||
throw self.apos.error('notfound'); | ||
} | ||
}; | ||
object[field.name] = self.apos.launder.string(input, field.def); | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
function getBundleModuleNames() { | ||
const source = path.join(__dirname, './modules/@apostrophecms'); | ||
return fs | ||
.readdirSync(source, { withFileTypes: true }) | ||
.filter(dirent => dirent.isDirectory()) | ||
.map(dirent => `@apostrophecms/${dirent.name}`); | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,97 @@ | ||
<template> | ||
<AposInputWrapper :modifiers="modifiers" :field="field" :error="effectiveError" :uid="uid" | ||
:display-options="displayOptions"> | ||
<template #body> | ||
<div> | ||
<h2>Mermaid Diagram</h2> | ||
<div id="preview-editor" ref="editor"></div> <!-- Use a ref for more control --> | ||
<button @click="renderDiagram">Render Diagram</button> | ||
<button @click="clearDiagram">Clear</button> | ||
<div id="mermaidOutput"></div> | ||
</div> | ||
</template> | ||
</AposInputWrapper> | ||
</template> | ||
|
||
<script> | ||
import mermaid from 'mermaid'; | ||
import ace from 'ace-builds/src-noconflict/ace' | ||
import 'ace-builds/src-noconflict/mode-plain_text' | ||
import 'ace-builds/src-noconflict/theme-monokai' | ||
import AposInputMixin from 'Modules/@apostrophecms/schema/mixins/AposInputMixin'; | ||
export default { | ||
mixins: [AposInputMixin], | ||
name: 'InputMermaid', | ||
props: { | ||
value: { | ||
type: Object, | ||
default: {} | ||
} | ||
}, | ||
data() { | ||
return { | ||
editor: null, | ||
}; | ||
}, | ||
mounted() { | ||
this.editor = ace.edit(this.$refs.editor, { | ||
minLines: 10, | ||
fontSize: 14, | ||
tabSize: 2 | ||
}); // Use ref | ||
this.editor.session.setMode("ace/mode/plain_text"); | ||
this.editor.setTheme("ace/theme/monokai"); | ||
// Set the editor's value to the existing value from the database | ||
if (this.value && this.value.data) { | ||
this.editor.setValue(this.value.data); | ||
} | ||
// Update Apostrophe data on editor change | ||
this.editor.getSession().on('change', () => { | ||
this.update(this.editor.getValue()); | ||
}); | ||
mermaid.initialize({ startOnLoad: false }); | ||
}, | ||
methods: { | ||
validate(value) { | ||
if (this.field.required) { | ||
if (!value) { | ||
return 'required'; | ||
} | ||
} | ||
return false; | ||
}, | ||
update(value) { | ||
// Update the ApostropheCMS data | ||
this.next = value; | ||
}, | ||
async renderDiagram() { | ||
const mermaidCode = this.editor.getValue(); | ||
const mermaidContainer = document.getElementById('mermaidOutput'); | ||
// Wrap the mermaid code in <pre class="mermaid"> | ||
mermaidContainer.innerHTML = `<pre class="mermaid vue-mermaid" style="background: #fff; width: 100%;">${mermaidCode}</pre>`; | ||
// Reinitialize mermaid to process the new diagram | ||
await mermaid.run({ | ||
querySelector: '.vue-mermaid' | ||
}); | ||
}, | ||
clearDiagram() { | ||
this.editor.setValue(''); | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
#preview-editor { | ||
height: 250px; | ||
} | ||
button { | ||
margin: 10px 10px 10px 0; | ||
} | ||
</style> |
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,15 @@ | ||
import mermaid from 'mermaid'; | ||
export default () => { | ||
mermaid.initialize({ startOnLoad: false }); | ||
apos.util.widgetPlayers.mermaid = { | ||
selector: '[data-mermaid-widget]', | ||
player: function (el) { | ||
const target = el.querySelector('.mermaid'); | ||
setTimeout(async () => { | ||
await mermaid.run({ | ||
nodes: document.querySelectorAll('.mermaid') | ||
}); | ||
}, 100); | ||
} | ||
}; | ||
}; |
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,4 @@ | ||
pre .mermaid { | ||
all: initial; | ||
display: block; | ||
} |
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,5 @@ | ||
<section data-mermaid-widget> | ||
<pre class="mermaid" style="background: #fff; width: 100%;"> | ||
{{ data.widget.mermaid }} | ||
</pre> | ||
</section> |