From 70941b07a6e46c95d04af467b912ed78386f1538 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Thu, 29 Mar 2012 17:02:05 +0100 Subject: [PATCH] Added initial support for the command macro Just the EditTiddler command at the moment --- js/App.js | 1 + js/macros/command.js | 39 +++++++++++++++++++ js/macros/edit.js | 8 +++- js/macros/story.js | 25 +++++++++++- tiddlywiki5/shadows/StoryTiddlers.tid | 1 - .../shadows/templates/EditTemplate.tid | 1 + .../shadows/templates/SimpleTemplate.tid | 2 +- tiddlywiki5/tiddlers/DraftTiddler.tid | 4 -- 8 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 js/macros/command.js delete mode 100644 tiddlywiki5/tiddlers/DraftTiddler.tid diff --git a/js/App.js b/js/App.js index bbc466e4e..8753febe6 100644 --- a/js/App.js +++ b/js/App.js @@ -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); diff --git a/js/macros/command.js b/js/macros/command.js new file mode 100644 index 000000000..e8dd06dd8 --- /dev/null +++ b/js/macros/command.js @@ -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)])] + } +}; + +})(); diff --git a/js/macros/edit.js b/js/macros/edit.js index f723cca71..e11c487c4 100644 --- a/js/macros/edit.js +++ b/js/macros/edit.js @@ -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)])]; } }; diff --git a/js/macros/story.js b/js/macros/story.js index bc137b15e..54a25f5a9 100644 --- a/js/macros/story.js +++ b/js/macros/story.js @@ -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> {{title{ <>}}} {{body{ diff --git a/tiddlywiki5/shadows/templates/SimpleTemplate.tid b/tiddlywiki5/shadows/templates/SimpleTemplate.tid index 1a8d25f2d..01d5c2957 100644 --- a/tiddlywiki5/shadows/templates/SimpleTemplate.tid +++ b/tiddlywiki5/shadows/templates/SimpleTemplate.tid @@ -4,6 +4,6 @@ modifier: JeremyRuston {{title{ <>}}} {{small{ -<> <>}}} +<> <> <>}}} {{body{ <>}}} \ No newline at end of file diff --git a/tiddlywiki5/tiddlers/DraftTiddler.tid b/tiddlywiki5/tiddlers/DraftTiddler.tid deleted file mode 100644 index 8f6db4a7d..000000000 --- a/tiddlywiki5/tiddlers/DraftTiddler.tid +++ /dev/null @@ -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 <>.