rename js obj to froghat, add widget plugin
This commit is contained in:
parent
f21bdbdb0c
commit
27cf36c390
|
|
@ -4,7 +4,7 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div id='content'>{{ page.body }}</div>
|
<div id='froghat'>{{ page.body }}</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -13,14 +13,14 @@
|
||||||
<!-- for converting markdown to html -->
|
<!-- for converting markdown to html -->
|
||||||
<script src="{{ url_for('static', filename='editor/purify.min.js' ) }}"></script>
|
<script src="{{ url_for('static', filename='editor/purify.min.js' ) }}"></script>
|
||||||
<script src="{{ url_for('static', filename='editor/marked.umd.min.js' ) }}"></script>
|
<script src="{{ url_for('static', filename='editor/marked.umd.min.js' ) }}"></script>
|
||||||
<script src="{{ url_for('static', filename='editor/grung.js' ) }}"></script>
|
<script src="{{ url_for('static', filename='editor/froghat.js' ) }}"></script>
|
||||||
{% if user.can_write(page) %}
|
{% if user.can_write(page) %}
|
||||||
<script src="{{ url_for('static', filename='editor/turndown.js' ) }}"></script>
|
<script src="{{ url_for('static', filename='editor/turndown.js' ) }}"></script>
|
||||||
<script src="{{ url_for('static', filename='editor/joplin-turndown-plugin-gfm.js' ) }}"></script>
|
<script src="{{ url_for('static', filename='editor/joplin-turndown-plugin-gfm.js' ) }}"></script>
|
||||||
<script src="{{ url_for('static', filename='editor/grung-editor.js' ) }}"></script>
|
<script src="{{ url_for('static', filename='editor/froghat-editor.js' ) }}"></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<script>
|
<script>
|
||||||
const wiki = new Grung{% if user.can_write(page) %}Editor{% endif %}({plugins: [MacroPlugin]});
|
const wiki = new Froghat{% if user.can_write(page) %}Editor{% endif %}({plugins: [MacroPlugin, WidgetPlugin]});
|
||||||
wiki.view();
|
wiki.view();
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
class GrungEditor extends Grung {
|
class FroghatEditor extends Froghat {
|
||||||
|
|
||||||
constructor(settings) {
|
constructor(settings) {
|
||||||
/*
|
/*
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
class Grung {
|
class Froghat {
|
||||||
constructor(settings) {
|
constructor(settings) {
|
||||||
/*
|
/*
|
||||||
* Create a new Editor instance.
|
* Create a new Editor instance.
|
||||||
*/
|
*/
|
||||||
this.element = document.getElementById(settings.editorId || 'content');
|
this.element = document.getElementById(settings.editorId || 'froghat');
|
||||||
this.source = this.element.textContent;
|
this.source = this.element.textContent;
|
||||||
|
|
||||||
this.marked = marked;
|
this.marked = marked;
|
||||||
|
|
@ -91,7 +91,7 @@ class Grung {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class GrungPlugin {
|
class FroghatPlugin {
|
||||||
|
|
||||||
constructor(settings) {
|
constructor(settings) {
|
||||||
this.name = settings.name;
|
this.name = settings.name;
|
||||||
|
|
@ -112,7 +112,73 @@ class GrungPlugin {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class MacroPlugin extends GrungPlugin {
|
class WidgetPlugin extends FroghatPlugin {
|
||||||
|
|
||||||
|
setEditable() {
|
||||||
|
};
|
||||||
|
|
||||||
|
toMarkdown(html) {
|
||||||
|
return html;
|
||||||
|
};
|
||||||
|
|
||||||
|
toHTML(md) {
|
||||||
|
return md;
|
||||||
|
};
|
||||||
|
|
||||||
|
parseWidgetSource(html) {
|
||||||
|
|
||||||
|
function block(prefix) {
|
||||||
|
return RegExp('##\\s*' + prefix + '.*?```\\w*(.+?)```', 'gims');
|
||||||
|
};
|
||||||
|
|
||||||
|
const template = block("Template").exec(html)[1];
|
||||||
|
const css = block("CSS").exec(html)[1];
|
||||||
|
const processor = block("Processor").exec(html)[1];
|
||||||
|
|
||||||
|
var func;
|
||||||
|
eval("func = " + processor);
|
||||||
|
|
||||||
|
return {
|
||||||
|
template: template,
|
||||||
|
css: css,
|
||||||
|
processor: func
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async processWidgets(html, callback) {
|
||||||
|
|
||||||
|
var widgetPattern = /({{(.+)}})/gm;
|
||||||
|
|
||||||
|
if (!html.match(widgetPattern)) {
|
||||||
|
callback();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
html.matchAll(widgetPattern).forEach(match => {
|
||||||
|
var widgetTag = match[1];
|
||||||
|
var widgetName = match[2];
|
||||||
|
if (Object.values(WIDGETS).indexOf(widgetName) == -1) {
|
||||||
|
APIv1.search("Widget", widgetName, (res) => {
|
||||||
|
if (res.code == 200) {
|
||||||
|
var parts = parseWidgetSource(res.response[0].body);
|
||||||
|
WIDGETS[widgetName] = parts.processor;
|
||||||
|
contents = WIDGETS[widgetName](widgetTag, parts.template, parts.css);
|
||||||
|
} else {
|
||||||
|
contents = `Invalid widget: ${widgetName}`;
|
||||||
|
}
|
||||||
|
var rep = `<span class="widget-${widgetName}" data-source="${widgetTag}">${contents}</span>`;
|
||||||
|
html = html.replaceAll(widgetTag, rep);
|
||||||
|
if (parts) {
|
||||||
|
html = `<style type='text/css'>${parts.css}</style>${html}`;
|
||||||
|
}
|
||||||
|
callback(html);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
class MacroPlugin extends FroghatPlugin {
|
||||||
|
|
||||||
macros = {
|
macros = {
|
||||||
// image: {}
|
// image: {}
|
||||||
|
|
@ -328,27 +328,27 @@ div[data-macro-name="toc"] a:hover {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#content {
|
#froghat {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
#content.loaded {
|
#froghat.loaded {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
#content.wysiwyg {
|
#froghat.wysiwyg {
|
||||||
display: none,
|
display: none,
|
||||||
}
|
}
|
||||||
#content.view {
|
#froghat.view {
|
||||||
}
|
}
|
||||||
|
|
||||||
#content.edit {
|
#froghat.edit {
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
white-space: pre;
|
white-space: pre;
|
||||||
}
|
}
|
||||||
|
|
||||||
#content.wysiwyg {
|
#froghat.wysiwyg {
|
||||||
}
|
}
|
||||||
|
|
||||||
#content.wysiwyg .md {
|
#froghat.wysiwyg .md {
|
||||||
opacity: 0.5;
|
opacity: 0.5;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,57 +56,3 @@ APIv1 = {
|
||||||
})();
|
})();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const WIDGETS = {};
|
|
||||||
|
|
||||||
function parseWidgetSource(html) {
|
|
||||||
|
|
||||||
function block(prefix) {
|
|
||||||
return RegExp('##\\s*' + prefix + '.*?```\\w*(.+?)```', 'gims');
|
|
||||||
};
|
|
||||||
|
|
||||||
const template = block("Template").exec(html)[1];
|
|
||||||
const css = block("CSS").exec(html)[1];
|
|
||||||
const processor = block("Processor").exec(html)[1];
|
|
||||||
|
|
||||||
var func;
|
|
||||||
eval("func = " + processor);
|
|
||||||
|
|
||||||
return {
|
|
||||||
template: template,
|
|
||||||
css: css,
|
|
||||||
processor: func
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function processWidgets(html, callback) {
|
|
||||||
|
|
||||||
var widgetPattern = /({{(.+)}})/gm;
|
|
||||||
|
|
||||||
if (!html.match(widgetPattern)) {
|
|
||||||
callback();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
html.matchAll(widgetPattern).forEach(match => {
|
|
||||||
var widgetTag = match[1];
|
|
||||||
var widgetName = match[2];
|
|
||||||
if (Object.values(WIDGETS).indexOf(widgetName) == -1) {
|
|
||||||
APIv1.search("Widget", widgetName, (res) => {
|
|
||||||
if (res.code == 200) {
|
|
||||||
var parts = parseWidgetSource(res.response[0].body);
|
|
||||||
WIDGETS[widgetName] = parts.processor;
|
|
||||||
contents = WIDGETS[widgetName](widgetTag, parts.template, parts.css);
|
|
||||||
} else {
|
|
||||||
contents = `Invalid widget: ${widgetName}`;
|
|
||||||
}
|
|
||||||
var rep = `<span class="widget-${widgetName}" data-source="${widgetTag}">${contents}</span>`;
|
|
||||||
html = html.replaceAll(widgetTag, rep);
|
|
||||||
if (parts) {
|
|
||||||
html = `<style type='text/css'>${parts.css}</style>${html}`;
|
|
||||||
}
|
|
||||||
callback(html);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user