From 475da6dbf2d5d135df0c14cec57e486416032692 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 18 Sep 2016 12:56:31 +0000 Subject: [PATCH] Auto-scroll containing terminal if an iframe resizes. Other oEmbed tweaks. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3336 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/client.js | 7 +++++++ packages/cory/libunfurl/libunfurl.js | 31 +++++++++++++++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/core/client.js b/core/client.js index 938db2ae..9f41448a 100644 --- a/core/client.js +++ b/core/client.js @@ -527,6 +527,13 @@ function onMessage(event) { var iframe = document.getElementById("iframe_" + event.data.name); iframe.setAttribute("width", event.data.width); iframe.setAttribute("height", event.data.height); + var node = iframe.parentElement; + while (node && !node.classList.contains("terminal")) { + node = node.parentElement; + } + if (node) { + autoScroll(node); + } } else { send({event: "onWindowMessage", message: event.data}); } diff --git a/packages/cory/libunfurl/libunfurl.js b/packages/cory/libunfurl/libunfurl.js index bb50298f..7ead793f 100644 --- a/packages/cory/libunfurl/libunfurl.js +++ b/packages/cory/libunfurl/libunfurl.js @@ -6,6 +6,7 @@ let libxml = require("libxml"); let libhttp = require("libhttp"); let gEmbedIndex = 0; +let gCache = {}; async function unfurl(url) { let result = {href: url}; @@ -26,10 +27,8 @@ async function unfurl(url) { result = [{href: url}, "\n", { name: "oEmbed" + gEmbedIndex, iframe: ` - - ${oEmbed.html} - - -`, + `, width: oEmbed.width || 320, height: oEmbed.height || 120, style: "margin: 0; padding: 0; border: 0; overflow: hidden", @@ -62,6 +60,23 @@ async function test() { //test().catch(terminal.print); -core.register("onMessage", function(sender, message) { - return unfurl(message).catch(error => [message, "(error retrieving: ", error, ")"]); +core.register("onMessage", async function(sender, message) { + let result = message; + if (gCache[message] && new Date().valueOf() < gCache[message].expires) { + result = gCache[message].result; + } else { + if (gCache[message]) { + print("Didn't use", gCache[message]); + } + try { + result = await unfurl(message); + } catch (error) { + result = [message, "(error retrieving: ", error, ")"]; + } + gCache[message] = { + expires: new Date().valueOf() + 7 * 24 * 60 * 60 * 1000, + result: result, + }; + } + return result; }); \ No newline at end of file