1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

First pass at external image support

A bunch of little changes that together enable external image support.
Try:

```
tiddlywiki editions/tw5.com --verbose --build externalimages
```

Then open `externalimages.html`, look for the images in the more/types
tab of the sidebar, open them and verify that they are set with an
external SRC attribute, not a data URI.
This commit is contained in:
Jermolene 2014-06-12 08:36:30 +01:00
parent f131c37893
commit 9547a1f01c
8 changed files with 147 additions and 20 deletions

View File

@ -1,5 +1,6 @@
title: $:/language/EditTemplate/
Body/External/Hint: This is an external tiddler stored outside of the main TiddlyWiki file. You can edit the tags and fields but cannot directly edit the content itself
Body/Hint: Use [[wiki text|http://tiddlywiki.com/static/WikiText.html]] to add formatting, images, and dynamic features
Body/Placeholder: Type the text for this tiddler
Body/Preview/Button/Hide: hide preview

View File

@ -19,7 +19,12 @@ var ImageParser = function(type,text,options) {
attributes: {}
},
src;
if(text) {
if(options._canonical_uri) {
element.attributes.src = {type: "string", value: options._canonical_uri};
if(type === "application/pdf" || type === ".pdf") {
element.tag = "embed";
}
} else if(text) {
if(type === "application/pdf" || type === ".pdf") {
element.attributes.src = {type: "string", value: "data:application/pdf;base64," + text};
element.tag = "embed";

View File

@ -56,9 +56,12 @@ ImageWidget.prototype.render = function(parent,nextSibling) {
} else {
// Check if it is an image tiddler
if(this.wiki.isImageTiddler(this.imageSource)) {
// Render the appropriate element for the image type
var type = tiddler.fields.type,
text = tiddler.fields.text;
text = tiddler.fields.text,
_canonical_uri = tiddler.fields._canonical_uri;
// If the tiddler has body text then it doesn't need to be lazily loaded
if(text) {
// Render the appropriate element for the image type
switch(type) {
case "application/pdf":
tag = "embed";
@ -71,6 +74,20 @@ ImageWidget.prototype.render = function(parent,nextSibling) {
src = "data:" + type + ";base64," + text;
break;
}
} else if(_canonical_uri) {
switch(type) {
case "application/pdf":
tag = "embed";
src = _canonical_uri;
break;
case "image/svg+xml":
src = _canonical_uri;
break;
default:
src = _canonical_uri;
break;
}
}
}
}
// Create the element and assign the attributes

View File

@ -711,6 +711,7 @@ Parse a block of text of a specified MIME type
options: see below
Options include:
parseAsInline: if true, the text of the tiddler will be parsed as an inline run
_canonical_uri: optional string of the canonical URI of this content
*/
exports.old_parseText = function(type,text,options) {
options = options || {};
@ -728,7 +729,8 @@ exports.old_parseText = function(type,text,options) {
// Return the parser instance
return new Parser(type,text,{
parseAsInline: options.parseAsInline,
wiki: this
wiki: this,
_canonical_uri: options._canonical_uri
});
};
@ -736,11 +738,14 @@ exports.old_parseText = function(type,text,options) {
Parse a tiddler according to its MIME type
*/
exports.old_parseTiddler = function(title,options) {
options = options || {};
options = $tw.utils.extend({},options);
var cacheType = options.parseAsInline ? "newInlineParseTree" : "newBlockParseTree",
tiddler = this.getTiddler(title),
self = this;
return tiddler ? this.getCacheForTiddler(title,cacheType,function() {
if(tiddler.hasField("_canonical_uri")) {
options._canonical_uri = tiddler.fields._canonical_uri;
}
return self.old_parseText(tiddler.fields.type,tiddler.fields.text,options);
}) : null;
};

View File

@ -1,10 +1,26 @@
title: $:/core/ui/EditTemplate/body
tags: $:/tags/EditTemplate
\define lingo-base() $:/language/EditTemplate/
\define lingo-base() $:/language/EditTemplate/Body/
<$list filter="[is[current]has[_canonical_uri]]">
<div class="tw-message-box">
<<lingo External/Hint>>
<a href={{!!_canonical_uri}}><$text text={{!!_canonical_uri}}/></a>
<$edit-text field="_canonical_uri" class="tw-edit-fields"></$edit-text>
</div>
</$list>
<$list filter="[is[current]!has[_canonical_uri]]">
<$reveal state="$:/state/showeditpreview" type="match" text="yes">
<em class="tw-edit"><<lingo Body/Hint>></em> <$button type="set" set="$:/state/showeditpreview" setTo="no"><<lingo Body/Preview/Button/Hide>></$button>
<em class="tw-edit"><<lingo Hint>></em> <$button type="set" set="$:/state/showeditpreview" setTo="no"><<lingo Preview/Button/Hide>></$button>
<div class="tw-tiddler-preview">
<div class="tw-tiddler-preview-preview">
@ -24,7 +40,9 @@ tags: $:/tags/EditTemplate
<$reveal state="$:/state/showeditpreview" type="nomatch" text="yes">
<em class="tw-edit"><<lingo Body/Hint>></em> <$button type="set" set="$:/state/showeditpreview" setTo="yes"><<lingo Body/Preview/Button/Show>></$button>
<em class="tw-edit"><<lingo Hint>></em> <$button type="set" set="$:/state/showeditpreview" setTo="yes"><<lingo Preview/Button/Show>></$button>
<$edit field="text" class="tw-edit-texteditor" placeholder={{$:/language/EditTemplate/Body/Placeholder}}/>
</$reveal>
</$list>

View File

@ -0,0 +1,68 @@
created: 20140610213500000
modified: 20140610213936575
tags: concepts
title: ExternalImages
type: text/vnd.tiddlywiki
External images in TiddlyWiki are tiddlers that point to the URI of an image, rather than embedding the full image data. They can perform better than embedded images, particularly with large numbers or sizes of images. However, using them breaks the single file pattern of TiddlyWiki.
External images are used in the browser. They can be created by the Node.js configuration when it builds a TiddlyWiki, or they can be created manually within the browser.
! What is an External Image
An external image is an ordinary image tiddler that has a ''_canonical_uri'' field containing the URI of the image. The URI can be absoluate or relative to the HTML document. If the canonical URI is provided then the ''text'' field of the tiddler is ignored and so should be omitted.
! Manually Creating External Images
To manually create an external image just create the tiddler with the appropriate image content type, and add a ''_canonical_uri'' field with a URI pointing to the actual image location.
! Creating external images under Node.js
The following steps are used to create a static HTML file version of a wiki accompanied by an ''images'' folder containing the referenced external images:
# Create image tiddlers in your TiddlyWikiFolders in the usual way
# Save the images as separate files (by convention, in a subfolder named ''images'')
# Externalise the image tiddlers by giving them a ''_canonical_uri'' field
# Save the main HTML file
Note the image files must be saved before they are externalised. Externalising them destroys the ''text'' field within the in-memory copy of the wiki store, meaning that attempts to save them will fail.
For an example see the ''externalimages'' build target of the demo ''tw5.com'' wiki:
```
--savetiddlers [is[image]] images
--setfield [is[image]] _canonical_uri $:/core/templates/canonical-uri text/plain
--setfield [is[image]] text "" text/plain
--rendertiddler $:/core/save/all externalimages.html text/plain
```
!! Saving Separate Image Files
The following `--savetiddlers` command can be used to save the images of a wiki into an ''images'' subfolder:
```
--savetiddlers [is[image]] images
```
!! Externalising Image Tiddlers
Two `--setfield` commands are used: the first sets the ''_canonical_uri'' field to a URI derived from the title of the tiddler, and the second clears the text field.
```
--setfield [is[image]] _canonical_uri $:/core/templates/canonical-uri text/plain
--setfield [is[image]] text "" text/plain
```
The template tiddler [[$:/core/templates/canonical-uri]] contains:
<pre>
<$view tiddler="$:/core/templates/canonical-uri" field="text" format="text"/>
</pre>
Note that these operations modify the tiddlers in the wiki store and so may affect the operation of subsequent commands.
! Using External Images
You can't edit an external image directly in the browser except by changing the URI field to point to a different image.

View File

@ -1,6 +1,6 @@
title: ImageWidget
created: 20140416160234142
modified: 20140416160234142
modified: 20140611160234142
tags: widget
! Introduction
@ -18,3 +18,11 @@ Any content of the `<$image>` widget is ignored.
|tooltip |The tooltip to be displayed over the image |
|class |CSS classes to be assigned to the `<img>` element |
! External Images and the ''_canonical_uri'' field
When used to display tiddler-based images, the image widget operates in two distinct modes:
* If the ''_canonical_uri'' field is present then it is used as the ''src'' attribute of the generated `<img>` element and the ''text'' field is ignored
* Without the ''_canonical_uri'' field, the image widget generates an `<img>` element that embeds the image data directly using a `data:` URI.
See ExternalImages for more details.

View File

@ -30,6 +30,11 @@
"build": {
"index": [
"--rendertiddler","$:/core/save/all","index.html","text/plain"],
"externalimages": [
"--savetiddlers","[is[image]] [type[text/html]]","images",
"--setfield","[is[image]] [type[text/html]]","_canonical_uri","$:/core/templates/canonical-uri","text/plain",
"--setfield","[is[image]]","text","","text/plain",
"--rendertiddler","$:/core/save/all","externalimages.html","text/plain"],
"encrypted": [
"--password", "password",
"--rendertiddler", "$:/core/save/all", "encrypted.html", "text/plain",