2024-02-01 17:54:04 +08:00
|
|
|
var bricks = window.bricks || {};
|
2023-09-14 10:28:07 +08:00
|
|
|
/*
|
|
|
|
reply on "https://github.com/markedjs/marked"
|
|
|
|
add following lines before 'bricks.js'
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
|
|
|
|
*/
|
|
|
|
|
2024-04-07 17:33:24 +08:00
|
|
|
bricks.MdWidget = class extends bricks.JsWidget {
|
2023-09-14 10:28:07 +08:00
|
|
|
/* options
|
|
|
|
{
|
2024-04-07 17:33:24 +08:00
|
|
|
"mdtext":
|
2023-09-14 10:28:07 +08:00
|
|
|
"md_url":
|
|
|
|
"method":"GET"
|
|
|
|
"params":{}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
constructor(options){
|
|
|
|
super(options);
|
2024-04-07 17:33:24 +08:00
|
|
|
if (this.mdtext){
|
|
|
|
this.md_content = this.mdtext;
|
|
|
|
this._build1();
|
|
|
|
} else {
|
|
|
|
var f = this.build.bind(this);
|
|
|
|
this.load_event = new Event('loaded');
|
|
|
|
this.dom_element.style.overFlow='auto';
|
|
|
|
window.addEventListener('scroll', this.show_scroll.bind(this));
|
|
|
|
schedule_once(f, 0.01);
|
|
|
|
}
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
2024-04-08 09:57:50 +08:00
|
|
|
set_content(content){
|
|
|
|
this.md_content = content;
|
|
|
|
this._build1();
|
|
|
|
}
|
2023-09-14 10:28:07 +08:00
|
|
|
show_scroll(event){
|
2024-03-08 10:54:16 +08:00
|
|
|
bricks.debug('scrollY=', window.scrollY);
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
build = async function(){
|
|
|
|
this._build(this.opts.md_url);
|
|
|
|
}
|
|
|
|
_build = async function(md_url){
|
2024-02-01 17:54:04 +08:00
|
|
|
var md_content = await (bricks.tget(md_url));
|
2024-04-07 17:33:24 +08:00
|
|
|
this.md_content = md_content;
|
|
|
|
this._build1();
|
|
|
|
this.dispatch('loaded', {'url':md_url});
|
|
|
|
}
|
|
|
|
_build1(){
|
|
|
|
this.dom_element.innerHTML = marked.parse(this.md_content);
|
2023-09-14 10:28:07 +08:00
|
|
|
/* change links in markdown to a bricks action */
|
|
|
|
var links = this.dom_element.getElementsByTagName('a');
|
|
|
|
for (var i=0; i<links.length; i ++){
|
|
|
|
var url = links[i].href;
|
|
|
|
links[i].href = '#';
|
|
|
|
links[i].onclick=this._build.bind(this, url);
|
|
|
|
}
|
2024-04-07 17:33:24 +08:00
|
|
|
}
|
|
|
|
getname(){
|
|
|
|
return this.name || 'mdtext';
|
|
|
|
}
|
|
|
|
getValue(){
|
2024-04-07 20:53:18 +08:00
|
|
|
var name = this.getname();
|
|
|
|
var d = {}
|
|
|
|
d[name] = this.md_content
|
|
|
|
return d;
|
2024-04-07 17:33:24 +08:00
|
|
|
}
|
|
|
|
setValue(v){
|
|
|
|
this.md_content = v;
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
}
|
2024-02-01 16:40:31 +08:00
|
|
|
bricks.MarkdownViewer = class extends bricks.VBox {
|
2023-09-14 10:28:07 +08:00
|
|
|
/* options
|
|
|
|
{
|
|
|
|
navigator:true
|
|
|
|
recommentable:false
|
|
|
|
md_url:
|
2024-04-07 17:33:24 +08:00
|
|
|
mdtext:
|
2023-09-14 10:28:07 +08:00
|
|
|
method:"GET",
|
|
|
|
params:{}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
constructor(options){
|
|
|
|
super(options);
|
|
|
|
this.back_stack = [];
|
2024-05-16 17:53:31 +08:00
|
|
|
this.md_url = bricks.absurl(this.opts.md_url);
|
2023-09-14 10:28:07 +08:00
|
|
|
if (this.opts.navigator){
|
|
|
|
this.createBackButton();
|
|
|
|
}
|
2024-04-07 17:33:24 +08:00
|
|
|
this.mdtext = new bricks.MdWidget({
|
|
|
|
mdtext:this.mdtext,
|
|
|
|
method:this.method,
|
|
|
|
params:this.params,
|
2023-09-14 10:28:07 +08:00
|
|
|
md_url:this.md_url
|
|
|
|
});
|
|
|
|
this.add_widget(this.mdtext);
|
|
|
|
this.mdtext.bind('loaded', this.add_back_stack.bind(this));
|
|
|
|
this.dom_element.style.overflow='auto';
|
|
|
|
this.dom_element.style.height='100%';
|
|
|
|
this.bind('scroll', this.show_scroll.bind(this));
|
|
|
|
}
|
|
|
|
show_scroll(event){
|
2024-03-08 10:54:16 +08:00
|
|
|
bricks.debug('scrollY=', window.scrollY);
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
createBackButton = async function(){
|
|
|
|
var desc = {
|
|
|
|
"widgettype":"HBox",
|
|
|
|
"options":{},
|
|
|
|
"subwidgets":[
|
|
|
|
{
|
|
|
|
"widgettype":"Text",
|
|
|
|
"options":{
|
|
|
|
"text":"<<<<<<<"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2024-02-20 15:56:35 +08:00
|
|
|
var w = await bricks.widgetBuild(desc);
|
2024-03-08 10:54:16 +08:00
|
|
|
bricks.debug('createBackButton():error, desc=', desc, 'w=', w);
|
2023-09-14 10:28:07 +08:00
|
|
|
var t = w.children[0];
|
2024-03-08 10:54:16 +08:00
|
|
|
bricks.debug('createBackButton():text=',t);
|
2023-09-14 10:28:07 +08:00
|
|
|
t.bind('click',this.go_back.bind(this));
|
|
|
|
this.add_widget(w);
|
2024-03-08 10:54:16 +08:00
|
|
|
bricks.debug('createBackButton():desc=',desc, 'w=', w);
|
2023-09-14 10:28:07 +08:00
|
|
|
}
|
|
|
|
add_back_stack(event){
|
2024-03-08 10:54:16 +08:00
|
|
|
bricks.debug('go_back_stack():event=', event);
|
2023-09-14 10:28:07 +08:00
|
|
|
var url = event.params.url;
|
|
|
|
this.back_stack.push(url);
|
|
|
|
}
|
|
|
|
go_back = async function(event){
|
|
|
|
if (this.back_stack.length < 2){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// ignore the current text url
|
|
|
|
this.back_stack.pop();
|
|
|
|
// get the back url
|
|
|
|
var url = this.back_stack.pop();
|
|
|
|
await this.mdtext._build(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
build = async function(){
|
|
|
|
this._build(this.opts.md_url);
|
|
|
|
}
|
|
|
|
_build = async function(md_url){
|
2024-02-01 17:54:04 +08:00
|
|
|
var md_content = await (bricks.tget(md_url));
|
2023-09-14 10:28:07 +08:00
|
|
|
this.md_el.innerHTML = marked.parse(md_content);
|
|
|
|
// this.md_el.baseURI = md_url;
|
|
|
|
|
|
|
|
/* change links in markdown to a bricks action */
|
|
|
|
var links = this.md_el.getElementsByTagName('a');
|
|
|
|
for (var i=0; i<links.length; i ++){
|
|
|
|
var url = links[i].href;
|
|
|
|
links[i].href = '#';
|
|
|
|
links[i].onclick=this._build.bind(this, url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:40:31 +08:00
|
|
|
bricks.Factory.register('MarkdownViewer', bricks.MarkdownViewer);
|
2024-04-07 17:33:24 +08:00
|
|
|
bricks.Factory.register('MdWidget', bricks.MdWidget);
|