1
0

Fixed a bunch of bugs around page navigation and renaming

This commit is contained in:
Zef Hemel 2022-08-01 15:06:32 +02:00
parent 339a8a9402
commit ca796b9c95
7 changed files with 45 additions and 16 deletions

View File

@ -29,8 +29,12 @@ export function save(): Promise<void> {
return syscall("editor.save"); return syscall("editor.save");
} }
export function navigate(name: string, pos?: number): Promise<void> { export function navigate(
return syscall("editor.navigate", name, pos); name: string,
pos?: number,
replaceState = false
): Promise<void> {
return syscall("editor.navigate", name, pos, replaceState);
} }
export function reloadPage(): Promise<void> { export function reloadPage(): Promise<void> {

View File

@ -12,6 +12,7 @@ import { set as storeSet } from "@plugos/plugos-syscall/store";
import { import {
flashNotification, flashNotification,
getCurrentPage, getCurrentPage,
getCursor,
getText, getText,
matchBefore, matchBefore,
navigate, navigate,
@ -107,6 +108,7 @@ export async function deletePage() {
export async function renamePage() { export async function renamePage() {
const oldName = await getCurrentPage(); const oldName = await getCurrentPage();
const cursor = await getCursor();
console.log("Old name is", oldName); console.log("Old name is", oldName);
const newName = await prompt(`Rename ${oldName} to:`, oldName); const newName = await prompt(`Rename ${oldName} to:`, oldName);
if (!newName) { if (!newName) {
@ -125,7 +127,7 @@ export async function renamePage() {
console.log("Writing new page to space"); console.log("Writing new page to space");
await writePage(newName, text); await writePage(newName, text);
console.log("Navigating to new page"); console.log("Navigating to new page");
await navigate(newName); await navigate(newName, cursor, true);
console.log("Deleting page from space"); console.log("Deleting page from space");
await deletePageSyscall(oldName); await deletePageSyscall(oldName);

View File

@ -12,15 +12,17 @@ export function PageNavigator({
}) { }) {
let options: FilterOption[] = []; let options: FilterOption[] = [];
for (let pageMeta of allPages) { for (let pageMeta of allPages) {
if (currentPage && currentPage === pageMeta.name) {
continue;
}
// Order by last modified date in descending order // Order by last modified date in descending order
let orderId = -pageMeta.lastModified; let orderId = -pageMeta.lastModified;
// Unless it was opened in this session // Unless it was opened in this session
if (pageMeta.lastOpened) { if (pageMeta.lastOpened) {
orderId = -pageMeta.lastOpened; orderId = -pageMeta.lastOpened;
} }
// Or it's the currently open page
if (currentPage && currentPage === pageMeta.name) {
// ... then we put it all the way to the end
orderId = Infinity;
}
options.push({ options.push({
...pageMeta, ...pageMeta,
orderId: orderId, orderId: orderId,

View File

@ -557,8 +557,8 @@ export class Editor {
this.editorView!.focus(); this.editorView!.focus();
} }
async navigate(name: string, pos?: number) { async navigate(name: string, pos?: number, replaceState = false) {
await this.pageNavigator.navigate(name, pos); await this.pageNavigator.navigate(name, pos, replaceState);
} }
async loadPage(pageName: string) { async loadPage(pageName: string) {

View File

@ -13,12 +13,20 @@ export class PathPageNavigator {
constructor(readonly root: string = "") {} constructor(readonly root: string = "") {}
async navigate(page: string, pos?: number) { async navigate(page: string, pos?: number, replaceState = false) {
if (replaceState) {
window.history.replaceState(
{ page, pos },
page,
`${this.root}/${encodePageUrl(page)}`
);
} else {
window.history.pushState( window.history.pushState(
{ page, pos }, { page, pos },
page, page,
`${this.root}/${encodePageUrl(page)}` `${this.root}/${encodePageUrl(page)}`
); );
}
window.dispatchEvent( window.dispatchEvent(
new PopStateEvent("popstate", { new PopStateEvent("popstate", {
state: { page, pos }, state: { page, pos },

View File

@ -43,6 +43,14 @@ export default function reducer(
showPageNavigator: false, showPageNavigator: false,
}; };
case "pages-listed": case "pages-listed":
// Let's move over any "lastOpened" times to the "allPages" list
let oldPageMeta = new Map([...state.allPages].map((pm) => [pm.name, pm]));
for (let pageMeta of action.pages) {
let oldPageMetaItem = oldPageMeta.get(pageMeta.name);
if (oldPageMetaItem && oldPageMetaItem.lastOpened) {
pageMeta.lastOpened = oldPageMetaItem.lastOpened;
}
}
return { return {
...state, ...state,
allPages: action.pages, allPages: action.pages,

View File

@ -43,8 +43,13 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
"editor.save": async () => { "editor.save": async () => {
return editor.save(true); return editor.save(true);
}, },
"editor.navigate": async (ctx, name: string, pos: number) => { "editor.navigate": async (
await editor.navigate(name, pos); ctx,
name: string,
pos: number,
replaceState = false
) => {
await editor.navigate(name, pos, replaceState);
}, },
"editor.reloadPage": async (ctx) => { "editor.reloadPage": async (ctx) => {
await editor.reloadPage(); await editor.reloadPage();