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 CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Scale the same format to the release size:

Authoring rules:

- Keep the version heading in the form `# Welcome to Markdown Viewer X.Y.Z`, matching `APP_VERSION`. The application checks this heading before displaying release details so a stale PWA response cannot appear under a newer version title.
- Use the Markdown syntax supported by the editor: headings, paragraphs, emphasis, inline or fenced code, links, images, blockquotes, GitHub-style alerts, ordered or unordered lists, task lists, tables, definition lists, footnotes, and horizontal rules. Use a construct only when it helps explain the release.
- Do not embed HTML elements, inline styles, `<style>` blocks, or scripts in `RELEASE_NOTES`. Presentation belongs to the scoped release-note renderer and stylesheet.
- Describe user impact instead of copying commit subjects.
Expand Down
70 changes: 57 additions & 13 deletions desktop-app/resources/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ document.addEventListener("DOMContentLoaded", async function () {
}

function markReleaseNotesSeen(tab) {
if (!isReleaseNotesTab(tab)) return;
if (!isReleaseNotesTab(tab) || tab.releaseNotesUnavailable) return;
const version = tab.releaseVersion || APP_VERSION;
saveStorageItem(RELEASE_NOTES_SEEN_VERSION_KEY, version);
if (readReleaseNotesStorage(RELEASE_NOTES_PENDING_VERSION_KEY) === version) {
Expand Down Expand Up @@ -772,12 +772,18 @@ document.addEventListener("DOMContentLoaded", async function () {

function loadCurrentReleaseNotes() {
if (!releaseNotesContentPromise) {
releaseNotesContentPromise = fetch('RELEASE_NOTES').then(function(response) {
// A versioned URL also bypasses an older worker's stale-while-revalidate cache.
releaseNotesContentPromise = fetch('RELEASE_NOTES?v=' + encodeURIComponent(APP_VERSION), { cache: 'no-cache' }).then(function(response) {
if (!response.ok) throw new Error('Release notes returned ' + response.status);
return response.text();
}).then(function(content) {
const heading = content.match(/^# (?:Welcome to )?Markdown Viewer ([^\s]+)\s*$/m);
if (!heading || heading[1] !== APP_VERSION) throw new Error('Release notes do not match ' + APP_VERSION);
return content;
}).catch(function(error) {
console.warn('Failed to load release notes:', error);
return '# Markdown Viewer ' + APP_VERSION + '\n\nThe release notes could not be loaded. Visit the [project releases](https://github.com/ThisIs-Developer/Markdown-Viewer/releases) for the latest changes.';
releaseNotesContentPromise = null;
return null;
});
}
return releaseNotesContentPromise;
Expand All @@ -789,7 +795,7 @@ document.addEventListener("DOMContentLoaded", async function () {
const existing = tabs.find(function(tab) {
return isReleaseNotesTab(tab) && tab.releaseVersion === APP_VERSION;
});
if (existing) {
if (existing && !existing.releaseNotesUnavailable) {
if (shouldActivate) {
await switchTab(existing.id);
updateLiveEditorAccess();
Expand All @@ -799,14 +805,25 @@ document.addEventListener("DOMContentLoaded", async function () {
return existing;
}

const content = await loadCurrentReleaseNotes();
const tab = createTab(content, 'Release Notes: ' + APP_VERSION, 'preview');
const releaseContent = await loadCurrentReleaseNotes();
const content = releaseContent || '# Markdown Viewer ' + APP_VERSION + '\n\nThe release notes could not be loaded. Reopen Release Notes to retry, or visit the [project release](https://github.com/ThisIs-Developer/Markdown-Viewer/releases/tag/v' + APP_VERSION + ') for the changes.';
// The tab can be closed or opened by another request while the fetch is pending.
const tab = tabs.find(function(item) {
return isReleaseNotesTab(item) && item.releaseVersion === APP_VERSION;
}) || createTab(content, 'Release Notes: ' + APP_VERSION, 'preview');
tab.content = content;
tab.kind = RELEASE_NOTES_TAB_KIND;
tab.temporary = true;
tab.releaseVersion = APP_VERSION;
tabs.push(tab);
tab.releaseNotesUnavailable = !releaseContent;
if (!tabs.includes(tab)) tabs.push(tab);
if (shouldActivate) {
await switchTab(tab.id);
if (activeTabId === tab.id) {
markdownEditor.value = content;
renderMarkdown();
} else {
await switchTab(tab.id);
}
updateLiveEditorAccess();
} else {
renderTabBar(tabs, activeTabId);
Expand Down Expand Up @@ -5489,12 +5506,28 @@ document.addEventListener("DOMContentLoaded", async function () {
? requestedWorkspaceId
: DEFAULT_WORKSPACE_ID;
const folder = getFolderById(documentOrganization.ui.lastFolderId);
const folderIsOpen = folder && folder.workspaceId === workspaceId &&
getWorkspaceById(workspaceId).expanded !== false &&
getFolderAncestors(folder.id).every(function(ancestor) { return ancestor.expanded !== false; });
return {
workspaceId: workspaceId,
folderId: folder && folder.workspaceId === workspaceId ? folder.id : null
folderId: folderIsOpen ? folder.id : null
};
}

function revealCreatedDocumentLocation(workspaceId, folderId) {
const workspace = getWorkspaceById(workspaceId);
if (workspace) workspace.expanded = true;
getFolderAncestors(folderId).forEach(function(folder) { folder.expanded = true; });
documentOrganization.ui.lastWorkspaceId = workspaceId;
documentOrganization.ui.lastFolderId = folderId || null;
documentOrganization.ui.filter = 'all';
documentSidebarSearch = '';
const search = document.getElementById('document-sidebar-search');
if (search) search.value = '';
saveDocumentOrganization();
}

function getDocumentActivityTime(tab) {
return Math.max(Number(tab.lastOpenedAt) || 0, Number(tab.lastEditedAt) || 0, Number(tab.createdAt) || 0);
}
Expand Down Expand Up @@ -5632,11 +5665,12 @@ document.addEventListener("DOMContentLoaded", async function () {
createdAt: Date.now()
};
documentOrganization.folders.push(folder);
workspace.expanded = true;
documentOrganization.ui.lastWorkspaceId = workspaceId;
documentOrganization.ui.lastFolderId = folder.id;
saveDocumentOrganization();
revealCreatedDocumentLocation(workspaceId, folder.id);
setSingleDocumentTreeSelection('folder', folder.id);
openDocumentSidebar();
renderDocumentSidebar();
const row = document.querySelector('.document-tree-row[data-tree-id="' + folder.id + '"]');
if (row) row.focus();
announceToScreenReader('Folder created: ' + value);
}
});
Expand Down Expand Up @@ -7880,6 +7914,8 @@ document.addEventListener("DOMContentLoaded", async function () {
return;
}
workspace.expanded = !expanded;
if (workspace.expanded) documentOrganization.ui.lastWorkspaceId = workspace.id;
if (documentOrganization.ui.lastWorkspaceId === workspace.id) documentOrganization.ui.lastFolderId = null;
saveDocumentOrganization();
renderDocumentSidebar();
},
Expand Down Expand Up @@ -7937,6 +7973,12 @@ document.addEventListener("DOMContentLoaded", async function () {
menuActions: getFolderMenuActions(folder),
onToggle: function() {
folder.expanded = !folderExpanded;
if (folder.expanded) {
documentOrganization.ui.lastWorkspaceId = workspace.id;
documentOrganization.ui.lastFolderId = folder.id;
} else if (getFolderAncestors(documentOrganization.ui.lastFolderId).some(function(ancestor) { return ancestor.id === folder.id; })) {
documentOrganization.ui.lastFolderId = null;
}
saveDocumentOrganization();
renderDocumentSidebar();
},
Expand Down Expand Up @@ -8210,6 +8252,7 @@ document.addEventListener("DOMContentLoaded", async function () {
const expandableItems = getExpandableDocumentTreeItems();
const shouldExpand = !expandableItems.some(function(item) { return item.expanded !== false; });
expandableItems.forEach(function(item) { item.expanded = shouldExpand; });
if (!shouldExpand) documentOrganization.ui.lastFolderId = null;
saveDocumentOrganization();
renderDocumentSidebar();
announceToScreenReader(shouldExpand ? 'All workspace folders expanded.' : 'All workspace folders collapsed.');
Expand Down Expand Up @@ -15348,6 +15391,7 @@ document.addEventListener("DOMContentLoaded", async function () {
if (options && options.builtInWorkspaceGuide === true) tab.kind = WORKSPACE_GUIDE_KIND;
normalizeTabDocumentMetadata(tab, { allowSecret: targetLocation.workspaceId === SECRET_WORKSPACE_ID });
tabs.push(tab);
revealCreatedDocumentLocation(tab.workspaceId, tab.folderId);
selectedDocumentId = tab.id;
switchTab(tab.id);
markdownEditor.focus();
Expand Down
70 changes: 57 additions & 13 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ document.addEventListener("DOMContentLoaded", async function () {
}

function markReleaseNotesSeen(tab) {
if (!isReleaseNotesTab(tab)) return;
if (!isReleaseNotesTab(tab) || tab.releaseNotesUnavailable) return;
const version = tab.releaseVersion || APP_VERSION;
saveStorageItem(RELEASE_NOTES_SEEN_VERSION_KEY, version);
if (readReleaseNotesStorage(RELEASE_NOTES_PENDING_VERSION_KEY) === version) {
Expand Down Expand Up @@ -772,12 +772,18 @@ document.addEventListener("DOMContentLoaded", async function () {

function loadCurrentReleaseNotes() {
if (!releaseNotesContentPromise) {
releaseNotesContentPromise = fetch('RELEASE_NOTES').then(function(response) {
// A versioned URL also bypasses an older worker's stale-while-revalidate cache.
releaseNotesContentPromise = fetch('RELEASE_NOTES?v=' + encodeURIComponent(APP_VERSION), { cache: 'no-cache' }).then(function(response) {
if (!response.ok) throw new Error('Release notes returned ' + response.status);
return response.text();
}).then(function(content) {
const heading = content.match(/^# (?:Welcome to )?Markdown Viewer ([^\s]+)\s*$/m);
if (!heading || heading[1] !== APP_VERSION) throw new Error('Release notes do not match ' + APP_VERSION);
return content;
}).catch(function(error) {
console.warn('Failed to load release notes:', error);
return '# Markdown Viewer ' + APP_VERSION + '\n\nThe release notes could not be loaded. Visit the [project releases](https://github.com/ThisIs-Developer/Markdown-Viewer/releases) for the latest changes.';
releaseNotesContentPromise = null;
return null;
});
}
return releaseNotesContentPromise;
Expand All @@ -789,7 +795,7 @@ document.addEventListener("DOMContentLoaded", async function () {
const existing = tabs.find(function(tab) {
return isReleaseNotesTab(tab) && tab.releaseVersion === APP_VERSION;
});
if (existing) {
if (existing && !existing.releaseNotesUnavailable) {
if (shouldActivate) {
await switchTab(existing.id);
updateLiveEditorAccess();
Expand All @@ -799,14 +805,25 @@ document.addEventListener("DOMContentLoaded", async function () {
return existing;
}

const content = await loadCurrentReleaseNotes();
const tab = createTab(content, 'Release Notes: ' + APP_VERSION, 'preview');
const releaseContent = await loadCurrentReleaseNotes();
const content = releaseContent || '# Markdown Viewer ' + APP_VERSION + '\n\nThe release notes could not be loaded. Reopen Release Notes to retry, or visit the [project release](https://github.com/ThisIs-Developer/Markdown-Viewer/releases/tag/v' + APP_VERSION + ') for the changes.';
// The tab can be closed or opened by another request while the fetch is pending.
const tab = tabs.find(function(item) {
return isReleaseNotesTab(item) && item.releaseVersion === APP_VERSION;
}) || createTab(content, 'Release Notes: ' + APP_VERSION, 'preview');
tab.content = content;
tab.kind = RELEASE_NOTES_TAB_KIND;
tab.temporary = true;
tab.releaseVersion = APP_VERSION;
tabs.push(tab);
tab.releaseNotesUnavailable = !releaseContent;
if (!tabs.includes(tab)) tabs.push(tab);
if (shouldActivate) {
await switchTab(tab.id);
if (activeTabId === tab.id) {
markdownEditor.value = content;
renderMarkdown();
} else {
await switchTab(tab.id);
}
updateLiveEditorAccess();
} else {
renderTabBar(tabs, activeTabId);
Expand Down Expand Up @@ -5489,12 +5506,28 @@ document.addEventListener("DOMContentLoaded", async function () {
? requestedWorkspaceId
: DEFAULT_WORKSPACE_ID;
const folder = getFolderById(documentOrganization.ui.lastFolderId);
const folderIsOpen = folder && folder.workspaceId === workspaceId &&
getWorkspaceById(workspaceId).expanded !== false &&
getFolderAncestors(folder.id).every(function(ancestor) { return ancestor.expanded !== false; });
return {
workspaceId: workspaceId,
folderId: folder && folder.workspaceId === workspaceId ? folder.id : null
folderId: folderIsOpen ? folder.id : null
};
}

function revealCreatedDocumentLocation(workspaceId, folderId) {
const workspace = getWorkspaceById(workspaceId);
if (workspace) workspace.expanded = true;
getFolderAncestors(folderId).forEach(function(folder) { folder.expanded = true; });
documentOrganization.ui.lastWorkspaceId = workspaceId;
documentOrganization.ui.lastFolderId = folderId || null;
documentOrganization.ui.filter = 'all';
documentSidebarSearch = '';
const search = document.getElementById('document-sidebar-search');
if (search) search.value = '';
saveDocumentOrganization();
}

function getDocumentActivityTime(tab) {
return Math.max(Number(tab.lastOpenedAt) || 0, Number(tab.lastEditedAt) || 0, Number(tab.createdAt) || 0);
}
Expand Down Expand Up @@ -5632,11 +5665,12 @@ document.addEventListener("DOMContentLoaded", async function () {
createdAt: Date.now()
};
documentOrganization.folders.push(folder);
workspace.expanded = true;
documentOrganization.ui.lastWorkspaceId = workspaceId;
documentOrganization.ui.lastFolderId = folder.id;
saveDocumentOrganization();
revealCreatedDocumentLocation(workspaceId, folder.id);
setSingleDocumentTreeSelection('folder', folder.id);
openDocumentSidebar();
renderDocumentSidebar();
const row = document.querySelector('.document-tree-row[data-tree-id="' + folder.id + '"]');
if (row) row.focus();
announceToScreenReader('Folder created: ' + value);
}
});
Expand Down Expand Up @@ -7880,6 +7914,8 @@ document.addEventListener("DOMContentLoaded", async function () {
return;
}
workspace.expanded = !expanded;
if (workspace.expanded) documentOrganization.ui.lastWorkspaceId = workspace.id;
if (documentOrganization.ui.lastWorkspaceId === workspace.id) documentOrganization.ui.lastFolderId = null;
saveDocumentOrganization();
renderDocumentSidebar();
},
Expand Down Expand Up @@ -7937,6 +7973,12 @@ document.addEventListener("DOMContentLoaded", async function () {
menuActions: getFolderMenuActions(folder),
onToggle: function() {
folder.expanded = !folderExpanded;
if (folder.expanded) {
documentOrganization.ui.lastWorkspaceId = workspace.id;
documentOrganization.ui.lastFolderId = folder.id;
} else if (getFolderAncestors(documentOrganization.ui.lastFolderId).some(function(ancestor) { return ancestor.id === folder.id; })) {
documentOrganization.ui.lastFolderId = null;
}
saveDocumentOrganization();
renderDocumentSidebar();
},
Expand Down Expand Up @@ -8210,6 +8252,7 @@ document.addEventListener("DOMContentLoaded", async function () {
const expandableItems = getExpandableDocumentTreeItems();
const shouldExpand = !expandableItems.some(function(item) { return item.expanded !== false; });
expandableItems.forEach(function(item) { item.expanded = shouldExpand; });
if (!shouldExpand) documentOrganization.ui.lastFolderId = null;
saveDocumentOrganization();
renderDocumentSidebar();
announceToScreenReader(shouldExpand ? 'All workspace folders expanded.' : 'All workspace folders collapsed.');
Expand Down Expand Up @@ -15348,6 +15391,7 @@ document.addEventListener("DOMContentLoaded", async function () {
if (options && options.builtInWorkspaceGuide === true) tab.kind = WORKSPACE_GUIDE_KIND;
normalizeTabDocumentMetadata(tab, { allowSecret: targetLocation.workspaceId === SECRET_WORKSPACE_ID });
tabs.push(tab);
revealCreatedDocumentLocation(tab.workspaceId, tab.folderId);
selectedDocumentId = tab.id;
switchTab(tab.id);
markdownEditor.focus();
Expand Down
10 changes: 8 additions & 2 deletions sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const CACHE_NAME = 'markdown-viewer-cache-v3.10.6';
const APP_VERSION = '3.10.6';
const CACHE_NAME = 'markdown-viewer-cache-v' + APP_VERSION;
const RELEASE_NOTES_PATH = new URL('./RELEASE_NOTES', self.location.href).pathname;

// PERF-011: Split precache into critical (local files) and lazy (CDN libraries)
// Critical assets are precached during SW install for instant offline startup
Expand All @@ -19,6 +21,7 @@ const CRITICAL_ASSETS = [
'./assets/lucide-icons.css',
'./assets/document-toc.js',
'./RELEASE_NOTES',
'./RELEASE_NOTES?v=' + APP_VERSION,
'./sample.md',
'./assets/icon.jpg',
'./manifest.json'
Expand Down Expand Up @@ -100,15 +103,18 @@ self.addEventListener('fetch', event => {
return;
}
const localPath = url.pathname.endsWith('/') ? '/' : url.pathname;
const isReleaseNotes = url.pathname === RELEASE_NOTES_PATH;
const shouldUseNetworkFirst =
event.request.mode === 'navigate' ||
isReleaseNotes ||
NETWORK_FIRST_LOCAL_PATHS.has(localPath) ||
localPath.startsWith('/assets/i18n/');

if (shouldUseNetworkFirst) {
event.respondWith(
caches.open(CACHE_NAME).then(cache => {
return fetch(event.request).then(networkResponse => {
return fetch(event.request, isReleaseNotes ? { cache: 'no-cache' } : undefined).then(networkResponse => {
if (isReleaseNotes && !networkResponse.ok) throw new Error('Release notes unavailable');
if (networkResponse && networkResponse.status === 200) {
cache.put(event.request, networkResponse.clone());
}
Expand Down
Loading
Loading