Skip to content

Commit

Permalink
Fix file handling with the legacy UWP app #433 (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaifroid authored Jul 21, 2023
1 parent 492c487 commit e6ccfa8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
6 changes: 3 additions & 3 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen" />
<!-- link href="css/bootstrap-theme.min.css" rel="stylesheet" media="screen" /-->
<script type="text/javascript">
// This script is needed to retrieve launch arguments when the app is started by double-clicking on a file
var launchArguments = null;
// This script is needed to retrieve launch arguments when the app is started from cold by double-clicking on a file
var launchArgumentsUWP = null;
if (typeof Windows !== 'undefined' && Windows.UI && Windows.UI.WebUI && Windows.UI.WebUI.WebUIApplication) {
Windows.UI.WebUI.WebUIApplication.addEventListener('activated', function (eventArgs) {
if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.file) {
console.log("App was activated with a file: " + eventArgs.files[0].name);
launchArguments = eventArgs;
launchArgumentsUWP = eventArgs;
}
}, false);
}
Expand Down
15 changes: 15 additions & 0 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ params['webkitdirectory'] = util.webkitdirectorySupported();
// Placeholder for the alert box header element, so it can be displayed and hidden easily
const alertBoxHeader = document.getElementById('alertBoxHeader');

// Retrieve UWP launch arguments when the app is started by double-clicking on a file
if (typeof Windows !== 'undefined' && Windows.UI && Windows.UI.WebUI && Windows.UI.WebUI.WebUIApplication) {
Windows.UI.WebUI.WebUIApplication.addEventListener('activated', function (eventArgs) {
if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.file) {
params.storedFile = eventArgs.files[0].name || '';
if (params.storedFile) {
params.pickedFile = eventArgs.files[0];
params.storedFilePath = eventArgs.files[0].path;
console.log('App was activated with a file: ' + params.storedFile);
processPickedFileUWP(params.pickedFile);
}
}
}, false);
}

// Test caching capability
cache.test(function () {});
// Unique identifier of the article expected to be displayed
Expand Down
16 changes: 7 additions & 9 deletions www/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

'use strict';

/* global Windows, launchArguments */
/* global Windows, launchArgumentsUWP */

// Set a global error handler to prevent app crashes
window.onerror = function (msg, url, line, col, error) {
Expand Down Expand Up @@ -66,10 +66,10 @@ params['cachedStartPages'] = {
wikivoyage_en_all_maxi: 'A/Main_Page'
};
params['kiwixDownloadLink'] = 'https://download.kiwix.org/zim/'; // Include final slash
params['kiwixHiddenDownloadLink'] = 'https://master.download.kiwix.org/zim/';
// params['kiwixHiddenDownloadLink'] = 'https://master.download.kiwix.org/zim/';
/** ***** DEV: ENSURE SERVERS BELOW ARE LISTED IN package.appxmanifest ************/
params['PWAServer'] = 'https://pwa.kiwix.org/'; // Production server
// params['PWAServer'] = "https://kiwix.github.io/kiwix-js-windows/dist/"; // Test server
// params['PWAServer'] = 'https://kiwix.github.io/kiwix-js-windows/dist/'; // Test server
params['storeType'] = getBestAvailableStorageAPI();
params['appType'] = getAppType();
params['keyPrefix'] = 'kiwixjs-'; // Prefix to use for localStorage keys
Expand Down Expand Up @@ -112,15 +112,13 @@ params['cacheAPI'] = 'kiwixjs-assetsCache'; // Set the global Cache API database
params['cacheIDB'] = 'kiwix-assetsCache'; // Set the global IndexedDB database here (Slightly different name to disambiguate)
params['imageDisplayMode'] = params.imageDisplay ? 'progressive' : 'manual';
params['storedFile'] = getSetting('lastSelectedArchive');
params.storedFile = launchArguments ? launchArguments.files[0].name : params.storedFile || params['packagedFile'] || '';
params.storedFile = params.storedFile || params['packagedFile'] || '';
params['lastPageVisit'] = params.rememberLastPage && params.storedFile ? getSetting(params.storedFile.replace(/(\.zim)\w?\w?$/, '$1')) || '' : '';
params.lastPageVisit = params.lastPageVisit ? params.lastPageVisit + '@kiwixKey@' + params.storedFile : '';
params['storedFilePath'] = getSetting('lastSelectedArchivePath');
params.storedFilePath = params.storedFilePath ? decodeURIComponent(params.storedFilePath) : params.archivePath + '/' + params.packagedFile;
params.storedFilePath = launchArguments ? launchArguments.files[0].path || '' : params.storedFilePath;
params.originalPackagedFile = params.packagedFile;
params['localStorage'] = '';
params['pickedFile'] = launchArguments ? launchArguments.files[0] : '';
params['pickedFolder'] = '';
params['themeChanged'] = params['themeChanged'] || false;
params['printIntercept'] = false;
Expand Down Expand Up @@ -178,12 +176,12 @@ if (!/^http/i.test(window.location.protocol) && params.localUWPSettings &&
uriParams += params.fileVersion ? '&fileVersion=' + encodeURIComponent(params.fileVersion) : '';
// Signal failure of PWA until it has successfully launched (in init.js it will be changed to 'success')
params.localUWPSettings.PWA_launch = 'fail';
if (launchArguments && typeof Windows.Storage !== 'undefined') {
if (launchArgumentsUWP && typeof Windows.Storage !== 'undefined') {
// We have to ensure the PWA will have access to the file with which the app was launched
var fal = Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList;
fal.addOrReplace(params.falFileToken, launchArguments.files[0]);
fal.addOrReplace(params.falFileToken, launchArgumentsUWP.files[0]);
if (fal.containsItem(params.falFolderToken)) fal.remove(params.falFolderToken);
uriParams += '&lastSelectedArchive=' + encodeURIComponent(launchArguments.files[0].name);
uriParams += '&lastSelectedArchive=' + encodeURIComponent(launchArgumentsUWP.files[0].name);
}
window.location.href = params.PWAServer + 'www/index.html' + uriParams;
// This will trigger the error catching above, cleanly dematerialize this script and transport us swiftly to PWA land
Expand Down

0 comments on commit e6ccfa8

Please sign in to comment.