1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-14 13:54:50 +00:00

Extend permalink/permaview to optionally copy URL to the clipboard

Fixes #3255
This commit is contained in:
Jermolene 2018-08-16 19:39:18 +01:00
parent b55a3102be
commit 5a2e87eb09
4 changed files with 68 additions and 33 deletions

View File

@ -126,6 +126,10 @@ Settings/NavigationHistory/Caption: Navigation History
Settings/NavigationHistory/Hint: Update browser history when navigating to a tiddler: Settings/NavigationHistory/Hint: Update browser history when navigating to a tiddler:
Settings/NavigationHistory/No/Description: Do not update history Settings/NavigationHistory/No/Description: Do not update history
Settings/NavigationHistory/Yes/Description: Update history Settings/NavigationHistory/Yes/Description: Update history
Settings/NavigationPermalinkviewMode/Caption: Permalink/permaview Mode
Settings/NavigationPermalinkviewMode/Hint: Choose how permalink/permaview is handled:
Settings/NavigationPermalinkviewMode/CopyToClipboard/Description: Copy permalink/permaview URL to clipboard
Settings/NavigationPermalinkviewMode/UpdateAddressBar/Description: Update address bar with permalink/permaview URL
Settings/PerformanceInstrumentation/Caption: Performance Instrumentation Settings/PerformanceInstrumentation/Caption: Performance Instrumentation
Settings/PerformanceInstrumentation/Hint: Displays performance statistics in the browser developer console. Requires reload to take effect Settings/PerformanceInstrumentation/Hint: Displays performance statistics in the browser developer console. Requires reload to take effect
Settings/PerformanceInstrumentation/Description: Enable performance instrumentation Settings/PerformanceInstrumentation/Description: Enable performance instrumentation

View File

@ -27,6 +27,9 @@ var DEFAULT_TIDDLERS_TITLE = "$:/DefaultTiddlers";
// Config // Config
var CONFIG_UPDATE_ADDRESS_BAR = "$:/config/Navigation/UpdateAddressBar"; // Can be "no", "permalink", "permaview" var CONFIG_UPDATE_ADDRESS_BAR = "$:/config/Navigation/UpdateAddressBar"; // Can be "no", "permalink", "permaview"
var CONFIG_UPDATE_HISTORY = "$:/config/Navigation/UpdateHistory"; // Can be "yes" or "no" var CONFIG_UPDATE_HISTORY = "$:/config/Navigation/UpdateHistory"; // Can be "yes" or "no"
var CONFIG_PERMALINKVIEW_COPY_TO_CLIPBOARD = "$:/config/Navigation/Permalinkview/CopyToClipboard"; // Can be "yes" (default) or "no"
var CONFIG_PERMALINKVIEW_UPDATE_ADDRESS_BAR = "$:/config/Navigation/Permalinkview/UpdateAddressBar"; // Can be "yes" (default) or "no"
exports.startup = function() { exports.startup = function() {
// Open startup tiddlers // Open startup tiddlers
@ -72,17 +75,19 @@ exports.startup = function() {
// Listen for the tm-permalink message // Listen for the tm-permalink message
$tw.rootWidget.addEventListener("tm-permalink",function(event) { $tw.rootWidget.addEventListener("tm-permalink",function(event) {
updateLocationHash({ updateLocationHash({
updateAddressBar: "permalink", updateAddressBar: $tw.wiki.getTiddlerText(CONFIG_PERMALINKVIEW_UPDATE_ADDRESS_BAR,"yes").trim() === "yes" ? "permalink" : "none",
updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim(), updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim(),
targetTiddler: event.param || event.tiddlerTitle targetTiddler: event.param || event.tiddlerTitle,
copyToClipboard: $tw.wiki.getTiddlerText(CONFIG_PERMALINKVIEW_COPY_TO_CLIPBOARD,"yes").trim() === "yes" ? "permalink" : "none"
}); });
}); });
// Listen for the tm-permaview message // Listen for the tm-permaview message
$tw.rootWidget.addEventListener("tm-permaview",function(event) { $tw.rootWidget.addEventListener("tm-permaview",function(event) {
updateLocationHash({ updateLocationHash({
updateAddressBar: "permaview", updateAddressBar: $tw.wiki.getTiddlerText(CONFIG_PERMALINKVIEW_UPDATE_ADDRESS_BAR,"yes").trim() === "yes" ? "permaview" : "none",
updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim(), updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim(),
targetTiddler: event.param || event.tiddlerTitle targetTiddler: event.param || event.tiddlerTitle,
copyToClipboard: $tw.wiki.getTiddlerText(CONFIG_PERMALINKVIEW_COPY_TO_CLIPBOARD,"yes").trim() === "yes" ? "permaview" : "none"
}); });
}); });
} }
@ -146,10 +151,10 @@ function openStartupTiddlers(options) {
options: See below options: See below
options.updateAddressBar: "permalink", "permaview" or "no" (defaults to "permaview") options.updateAddressBar: "permalink", "permaview" or "no" (defaults to "permaview")
options.updateHistory: "yes" or "no" (defaults to "no") options.updateHistory: "yes" or "no" (defaults to "no")
options.copyToClipboard: "permalink", "permaview" or "no" (defaults to "no")
options.targetTiddler: optional title of target tiddler for permalink options.targetTiddler: optional title of target tiddler for permalink
*/ */
function updateLocationHash(options) { function updateLocationHash(options) {
if(options.updateAddressBar !== "no") {
// Get the story and the history stack // Get the story and the history stack
var storyList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE), var storyList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE),
historyList = $tw.wiki.getTiddlerData(DEFAULT_HISTORY_TITLE,[]), historyList = $tw.wiki.getTiddlerData(DEFAULT_HISTORY_TITLE,[]),
@ -167,10 +172,22 @@ function updateLocationHash(options) {
} }
} }
// Assemble the location hash // Assemble the location hash
if(options.updateAddressBar === "permalink") { switch(options.updateAddressBar) {
case "permalink":
$tw.locationHash = "#" + encodeURIComponent(targetTiddler); $tw.locationHash = "#" + encodeURIComponent(targetTiddler);
} else { break;
case "permaview":
$tw.locationHash = "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList)); $tw.locationHash = "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList));
break;
}
// Copy URL to the clipboard
switch(options.copyToClipboard) {
case "permalink":
$tw.utils.copyToClipboard($tw.utils.getLocationPath() + "#" + encodeURIComponent(targetTiddler));
break;
case "permaview":
$tw.utils.copyToClipboard($tw.utils.getLocationPath() + "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList)));
break;
} }
// Only change the location hash if we must, thus avoiding unnecessary onhashchange events // Only change the location hash if we must, thus avoiding unnecessary onhashchange events
if($tw.utils.getLocationHash() !== $tw.locationHash) { if($tw.utils.getLocationHash() !== $tw.locationHash) {
@ -183,6 +200,5 @@ function updateLocationHash(options) {
} }
} }
} }
}
})(); })();

View File

@ -263,4 +263,9 @@ exports.copyToClipboard = function(text,options) {
document.body.removeChild(textArea); document.body.removeChild(textArea);
}; };
exports.getLocationPath = function() {
return window.location.toString().split("#")[0];
};
})(); })();

View File

@ -0,0 +1,10 @@
title: $:/core/ui/ControlPanel/Settings/NavigationPermalinkviewMode
tags: $:/tags/ControlPanel/Settings
caption: {{$:/language/ControlPanel/Settings/NavigationPermalinkviewMode/Caption}}
\define lingo-base() $:/language/ControlPanel/Settings/NavigationPermalinkviewMode/
<<lingo Hint>>
<$checkbox tiddler="$:/config/Navigation/Permalinkview/CopyToClipboard" field="text" checked="yes" unchecked="no" default="yes"> <$link to="$:/config/Navigation/Permalinkview/CopyToClipboard"><<lingo CopyToClipboard/Description>></$link> </$checkbox>
<$checkbox tiddler="$:/config/Navigation/Permalinkview/UpdateAddressBar" field="text" checked="yes" unchecked="no" default="yes"> <$link to="$:/config/Navigation/Permalinkview/UpdateAddressBar"><<lingo UpdateAddressBar/Description>></$link> </$checkbox>