From e2d63a03b2024ab81c876cd03815cb5c515b48bf Mon Sep 17 00:00:00 2001 From: Jermolene Date: Thu, 17 Sep 2015 19:41:41 +0100 Subject: [PATCH] Text-slicer: Improvements to image handling --- editions/text-slicer/tiddlers/Sample Text.tid | 2 +- .../tiddlywiki/text-slicer/modules/slicer.js | 14 ++++++- .../text-slicer/modules/slicers/image.js | 42 +++++++++++++++---- .../templates/interactive/image.tid | 15 +++++++ .../templates/interactive/tiddler.tid | 4 ++ .../text-slicer/templates/plain/image.tid | 3 ++ .../text-slicer/templates/plain/tiddler.tid | 4 ++ .../text-slicer/templates/static/image.tid | 3 ++ .../text-slicer/templates/static/tiddler.tid | 6 +++ 9 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 plugins/tiddlywiki/text-slicer/templates/interactive/image.tid create mode 100644 plugins/tiddlywiki/text-slicer/templates/plain/image.tid create mode 100644 plugins/tiddlywiki/text-slicer/templates/static/image.tid diff --git a/editions/text-slicer/tiddlers/Sample Text.tid b/editions/text-slicer/tiddlers/Sample Text.tid index 305e594dc..0c15eb858 100644 --- a/editions/text-slicer/tiddlers/Sample Text.tid +++ b/editions/text-slicer/tiddlers/Sample Text.tid @@ -9,7 +9,7 @@ type: text/html

Introduction to TiddlyWiki

-Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

What is TiddlyWiki used for?

diff --git a/plugins/tiddlywiki/text-slicer/modules/slicer.js b/plugins/tiddlywiki/text-slicer/modules/slicer.js index 580cb25fe..cc86089b0 100644 --- a/plugins/tiddlywiki/text-slicer/modules/slicer.js +++ b/plugins/tiddlywiki/text-slicer/modules/slicer.js @@ -50,6 +50,18 @@ Slicer.prototype.addToList = function(parent,child) { this.addTiddler($tw.utils.extend({title: parent},parentTiddler,{list: parentList})); }; +Slicer.prototype.insertBeforeListItem = function(parent,child,beforeSibling) { + var parentTiddler = this.tiddlers[parent] || {}, + parentList = parentTiddler.list || [], + parentListSiblingPosition = parentList.indexOf(beforeSibling); + if(parentListSiblingPosition !== -1) { + parentList.splice(parentListSiblingPosition,0,child) + this.addTiddler($tw.utils.extend({title: parent},parentTiddler,{list: parentList})); + } + + else {debugger;} +}; + Slicer.prototype.popParentStackUntil = function(type) { // Pop the stack to remove any entries at the same or lower level var newLevel = this.convertTypeToLevel(type), @@ -173,7 +185,7 @@ Slicer.prototype.processNode = function(domNode) { } } if(!hasProcessed) { - if(domNode.hasChildNodes()) { + if(nodeType === 1 && domNode.hasChildNodes()) { this.processNodeList(domNode.childNodes); } } diff --git a/plugins/tiddlywiki/text-slicer/modules/slicers/image.js b/plugins/tiddlywiki/text-slicer/modules/slicers/image.js index 572efb82c..6280bd07c 100644 --- a/plugins/tiddlywiki/text-slicer/modules/slicers/image.js +++ b/plugins/tiddlywiki/text-slicer/modules/slicers/image.js @@ -17,14 +17,42 @@ exports.processImageNode = function(domNode,tagName) { var src = domNode.getAttribute("src"); if(src && src.substr(0,5) === "data:") { var parts = src.toString().substr(5).split(";base64,"), + type = parts[0], + text = parts[1], + contentTypeInfo = $tw.config.contentTypeInfo[type], containerTitle = this.getTopContainer(), - title = this.makeUniqueTitle("image",containerTitle); - this.addTiddler({ - title: title, - type: parts[0], - text: parts[1] - }); - this.appendToCurrentContainer("[img[" + title + "]]"); + containerTiddler = this.tiddlers[containerTitle], + title = this.makeUniqueTitle("image",containerTitle) + contentTypeInfo.extension, + tiddler = { + title: title, + type: parts[0], + text: parts[1], + "toc-type": "image" + }; + switch(containerTiddler["toc-type"]) { + case "document": + // Make the image be the next child of the document + this.addToList(containerTitle,title); + break; + case "heading": + // Make the image be the older sibling of the heading + var parentTitle = this.parentStack[this.parentStack.length - 2].title; + this.insertBeforeListItem(parentTitle,title,containerTitle); + break; + case "paragraph": + // Make the image be the older sibling of the paragraph + var parentTitle = this.parentStack[this.parentStack.length - 1].title; + this.insertBeforeListItem(parentTitle,title,containerTitle); + break; + case "item": + // Create a new older sibling item to contain the image + var parentTitle = this.parentStack[this.parentStack.length - 1].title; + tiddler["toc-type"] = "item"; + this.insertBeforeListItem(parentTitle,title,containerTitle); + break; + } + this.addTiddler(tiddler); + // this.appendToCurrentContainer("[img[" + title + "]]"); } return true; } diff --git a/plugins/tiddlywiki/text-slicer/templates/interactive/image.tid b/plugins/tiddlywiki/text-slicer/templates/interactive/image.tid new file mode 100644 index 000000000..7c957ef81 --- /dev/null +++ b/plugins/tiddlywiki/text-slicer/templates/interactive/image.tid @@ -0,0 +1,15 @@ +title: $:/plugins/tiddlywiki/text-slicer/templates/interactive/image + +\define body() +<$link tag="div" class="tc-document-tiddler-link tc-document-tiddler"> + +<$list filter="""[all[current]] $(tv-exclude-filter)$ +[limit[1]]""" variable="item"> + +<$transclude/> + + + + +\end + +<> diff --git a/plugins/tiddlywiki/text-slicer/templates/interactive/tiddler.tid b/plugins/tiddlywiki/text-slicer/templates/interactive/tiddler.tid index f02cf6a70..359bec82e 100644 --- a/plugins/tiddlywiki/text-slicer/templates/interactive/tiddler.tid +++ b/plugins/tiddlywiki/text-slicer/templates/interactive/tiddler.tid @@ -23,3 +23,7 @@ title: $:/plugins/tiddlywiki/text-slicer/templates/interactive/tiddler <$reveal type="match" state="!!toc-type" text="item"> <$transclude tiddler="$:/plugins/tiddlywiki/text-slicer/templates/interactive/item" mode="block"/> + +<$reveal type="match" state="!!toc-type" text="image"> +<$transclude tiddler="$:/plugins/tiddlywiki/text-slicer/templates/interactive/image" mode="block"/> + diff --git a/plugins/tiddlywiki/text-slicer/templates/plain/image.tid b/plugins/tiddlywiki/text-slicer/templates/plain/image.tid new file mode 100644 index 000000000..943f1c78c --- /dev/null +++ b/plugins/tiddlywiki/text-slicer/templates/plain/image.tid @@ -0,0 +1,3 @@ +title: $:/plugins/tiddlywiki/text-slicer/templates/plain/image + +<$transclude/> diff --git a/plugins/tiddlywiki/text-slicer/templates/plain/tiddler.tid b/plugins/tiddlywiki/text-slicer/templates/plain/tiddler.tid index bbf0080f2..b5620c6c0 100644 --- a/plugins/tiddlywiki/text-slicer/templates/plain/tiddler.tid +++ b/plugins/tiddlywiki/text-slicer/templates/plain/tiddler.tid @@ -22,4 +22,8 @@ title: $:/plugins/tiddlywiki/text-slicer/templates/plain/tiddler <$transclude tiddler="$:/plugins/tiddlywiki/text-slicer/templates/plain/item" mode="block"/> +<$list filter="[prefix[image]]" variable="item"> +<$transclude tiddler="$:/plugins/tiddlywiki/text-slicer/templates/plain/image" mode="block"/> + + diff --git a/plugins/tiddlywiki/text-slicer/templates/static/image.tid b/plugins/tiddlywiki/text-slicer/templates/static/image.tid new file mode 100644 index 000000000..fd8bbce81 --- /dev/null +++ b/plugins/tiddlywiki/text-slicer/templates/static/image.tid @@ -0,0 +1,3 @@ +title: $:/plugins/tiddlywiki/text-slicer/templates/static/image + +``;base64,`<$view format="text" field="text"/>`">` diff --git a/plugins/tiddlywiki/text-slicer/templates/static/tiddler.tid b/plugins/tiddlywiki/text-slicer/templates/static/tiddler.tid index 724120eda..5bdde1a31 100644 --- a/plugins/tiddlywiki/text-slicer/templates/static/tiddler.tid +++ b/plugins/tiddlywiki/text-slicer/templates/static/tiddler.tid @@ -32,4 +32,10 @@ title: $:/plugins/tiddlywiki/text-slicer/templates/static/tiddler +<$list filter="[prefix[image]]" variable="item"> + +<$transclude tiddler="$:/plugins/tiddlywiki/text-slicer/templates/static/image" mode="block"/> + + +