1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Added initial support for the command macro

Just the EditTiddler command at the moment
This commit is contained in:
Jeremy Ruston 2012-03-29 17:02:05 +01:00
parent b0cdd866ee
commit 70941b07a6
8 changed files with 72 additions and 9 deletions

View File

@ -83,6 +83,7 @@ var App = function() {
}
// Bit of a hack to set up the macros
this.store.installMacro(require("./macros/chooser.js").macro);
this.store.installMacro(require("./macros/command.js").macro);
this.store.installMacro(require("./macros/echo.js").macro);
this.store.installMacro(require("./macros/edit.js").macro);
this.store.installMacro(require("./macros/image.js").macro);

39
js/macros/command.js Normal file
View File

@ -0,0 +1,39 @@
/*\
title: js/macros/command.js
\*/
(function(){
/*jslint node: true */
"use strict";
var Renderer = require("../Renderer.js").Renderer;
exports.macro = {
name: "command",
params: {
name: {byName: "default", type: "text"},
label: {byName: true, type: "text"},
"class": {byName: true, type: "text"}
},
events: {
"click": function(event) {
var commandEvent = document.createEvent("Event");
commandEvent.initEvent("tw-" + this.params.name,true,true);
commandEvent.tiddlerTitle = this.tiddlerTitle;
commandEvent.commandOrigin = this;
event.target.dispatchEvent(commandEvent);
event.preventDefault();
return false;
}
},
execute: function() {
var attributes = {};
if(this.hasParameter("class")) {
attributes["class"] = this.params.class.split(" ");
}
return [Renderer.ElementNode("button",attributes,[Renderer.TextNode(this.params.label)])]
}
};
})();

View File

@ -35,11 +35,17 @@ exports.macro = {
break;
}
}
var type = "div";
switch(field) {
case "text":
type = "pre";
break;
}
var attributes = {
"contenteditable": true,
"class": ["tw-edit-field"]
};
return [Renderer.ElementNode("div",attributes,[Renderer.TextNode(value)])];
return [Renderer.ElementNode(type,attributes,[Renderer.TextNode(value)])];
}
};

View File

@ -16,11 +16,12 @@ exports.macro = {
name: "story",
params: {
story: {byName: "default", type: "tiddler"},
defaultViewTemplate: {byName: true, type: "tiddler"}
defaultViewTemplate: {byName: true, type: "tiddler"},
defaultEditTemplate: {byName: true, type: "tiddler"}
},
events: {
"tw-navigate": function(event) {
var template = this.params.defaultViewTemplate ? this.params.defaultViewTemplate : "SimpleTemplate",
var template = this.hasParameter("defaultViewTemplate") ? this.params.defaultViewTemplate : "SimpleTemplate",
storyTiddler = this.store.getTiddler(this.params.story),
story = {tiddlers: []};
if(storyTiddler && storyTiddler.hasOwnProperty("text")) {
@ -33,6 +34,26 @@ exports.macro = {
}, 400);
event.stopPropagation();
return false;
},
"tw-EditTiddler": function(event) {
var template = this.hasParameter("defaultEditTemplate") ? this.params.defaultEditTemplate : "EditTemplate",
storyTiddler = this.store.getTiddler(this.params.story),
story = {tiddlers: []};
if(storyTiddler && storyTiddler.hasOwnProperty("text")) {
story = JSON.parse(storyTiddler.text);
}
for(var t=0; t<story.tiddlers.length; t++) {
var storyRecord = story.tiddlers[t];
if(storyRecord.title === event.tiddlerTitle && storyRecord.template !== template) {
storyRecord.title = "Draft of " + event.tiddlerTitle + " at " + (new Date());
storyRecord.template = template;
var tiddler = this.store.getTiddler(event.tiddlerTitle);
this.store.addTiddler(new Tiddler(tiddler,{title: storyRecord.title, "draft.title": event.tiddlerTitle}));
}
}
this.store.addTiddler(new Tiddler(storyTiddler,{text: JSON.stringify(story)}));
event.stopPropagation();
return false;
}
},
execute: function() {

View File

@ -3,7 +3,6 @@ type: application/json
{
"tiddlers": [
{"title": "DraftTiddler", "template": "EditTemplate"},
{"title": "HelloThere", "template": "SimpleTemplate"},
{"title": "Introduction", "template": "SimpleTemplate"},
{"title": "NewWikiTextFeatures", "template": "SimpleTemplate"}

View File

@ -1,6 +1,7 @@
title: EditTemplate
modifier: JeremyRuston
<<view title>>
{{title{
<<edit draft.title>>}}}
{{body{

View File

@ -4,6 +4,6 @@ modifier: JeremyRuston
{{title{
<<view title>>}}}
{{small{
<<view modifier link>> <<view modified date>>}}}
<<view modifier link>> <<view modified date>> <<command EditTiddler label:"edit" class:"btn btn-mini btn-success">>}}}
{{body{
<<view text wikified>>}}}

View File

@ -1,4 +0,0 @@
title: DraftTiddler
draft.title: New Tiddler
This is a sample draft tiddler. It is a draft of the tiddler titled in the `draft.title` field, in this case <<view draft.title>>.