8000 Show # IMAGE: tags as images in the preview pane by NGdev2 · Pull Request #566 · inkle/inky · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/renderer/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ LiveCompiler.setEvents({

},
compileComplete: (sessionId) => {
PlayerView.setImageBaseDir(InkProject.currentProject.mainInk.projectDir);
PlayerView.prepareForNewPlaythrough(sessionId);
EditorView.clearErrors();
ToolbarView.clearIssueSummary();
Expand Down
9 changes: 9 additions & 0 deletions app/renderer/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,15 @@ img.issue-icon.warning {
display: none;
}

#player p.tagImage {
text-align: center;
}

#player p.tagImage img {
max-width: 100%;
max-height: 60vh;
}

#player p.end {
font-weight: bold;
text-align: center;
Expand Down
72 changes: 71 additions & 1 deletion app/renderer/playerView.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const $ = window.jQuery = require('./jquery-2.2.3.min.js');
const path = require('path');
const url = require('url');
const i18n = require('./i18n.js');

var events = {};
var lastFadeTime = 0;
var $textBuffer = null;
var instructionPrefix = null;
var animationEnabled = true;
var imageBaseDir = null;

document.addEventListener("keyup", function(){
$("#player").removeClass("altKey");
Expand Down Expand Up @@ -179,6 +182,72 @@ function addTags(tags)

if( animationEnabled && shouldAnimate() )
fadeIn($tags);

// Show an image for each "# IMAGE: path" tag
for(var i=0; i<tags.length; i++) {
var splitTag = splitPropertyTag(tags[i]);
if( splitTag && splitTag.property.toUpperCase() == "IMAGE" )
addImage(splitTag.val, $tags);
}
}

// Parses tags of the form "PROPERTY: value"
function splitPropertyTag(tag) {
var propertySplitIdx = tag.indexOf(":");
if( propertySplitIdx == -1 ) return null;

return {
property: tag.substr(0, propertySplitIdx).trim(),
val: tag.substr(propertySplitIdx+1).trim()
};
}

// Relative paths are resolved against the main ink file's folder
function imageSrc(imagePath) {
if( /^(https?|file|data):/i.test(imagePath) )
return imagePath;

if( path.isAbsolute(imagePath) )
return url.pathToFileURL(imagePath).href;

// Unsaved project: no folder to resolve relative paths against
if( !imageBaseDir )
return null;

return url.pathToFileURL(path.join(imageBaseDir, imagePath)).href;
}

function addImage(imagePath, $tags)
{
var src = imageSrc(imagePath);
if( !src ) return;

var $bufferForImage = $textBuffer;
var $imagePara = $("<p class='tagImage'></p>");
var $image = $("<img>");

$image.on("load", () => {
// Re-measure, since the content height was taken before the image loaded
if( $bufferForImage.is($textBuffer) && shouldAnimate() )
contentReady();
});

// File not found: drop the image and explain why in the tag's tooltip
$image.on("error", () => {
$imagePara.remove();
$tags.attr("title", i18n._("Image not found"));
});

$image.attr("src", src);
$imagePara.append($image);
$textBuffer.append($imagePara);

if( animationEnabled && shouldAnimate() )
fadeIn($imagePara);
}

function setImageBaseDir(dir) {
imageBaseDir = dir;
}

function addChoice(choice, callback)
Expand Down Expand Up @@ -314,5 +383,6 @@ exports.PlayerView = {
showSessionView: showSessionView,
previewStepBack: previewStepBack,
setInstructionPrefix: setInstructionPrefix,
setAnimationEnabled: setAnimationEnabled
setAnimationEnabled: setAnimationEnabled,
setImageBaseDir: setImageBaseDir
};
0