Fixed a bunch of bugs around page navigation and renaming
This commit is contained in:
parent
339a8a9402
commit
ca796b9c95
@ -29,8 +29,12 @@ export function save(): Promise<void> {
|
||||
return syscall("editor.save");
|
||||
}
|
||||
|
||||
export function navigate(name: string, pos?: number): Promise<void> {
|
||||
return syscall("editor.navigate", name, pos);
|
||||
export function navigate(
|
||||
name: string,
|
||||
pos?: number,
|
||||
replaceState = false
|
||||
): Promise<void> {
|
||||
return syscall("editor.navigate", name, pos, replaceState);
|
||||
}
|
||||
|
||||
export function reloadPage(): Promise<void> {
|
||||
|
@ -12,6 +12,7 @@ import { set as storeSet } from "@plugos/plugos-syscall/store";
|
||||
import {
|
||||
flashNotification,
|
||||
getCurrentPage,
|
||||
getCursor,
|
||||
getText,
|
||||
matchBefore,
|
||||
navigate,
|
||||
@ -107,6 +108,7 @@ export async function deletePage() {
|
||||
|
||||
export async function renamePage() {
|
||||
const oldName = await getCurrentPage();
|
||||
const cursor = await getCursor();
|
||||
console.log("Old name is", oldName);
|
||||
const newName = await prompt(`Rename ${oldName} to:`, oldName);
|
||||
if (!newName) {
|
||||
@ -125,7 +127,7 @@ export async function renamePage() {
|
||||
console.log("Writing new page to space");
|
||||
await writePage(newName, text);
|
||||
console.log("Navigating to new page");
|
||||
await navigate(newName);
|
||||
await navigate(newName, cursor, true);
|
||||
console.log("Deleting page from space");
|
||||
await deletePageSyscall(oldName);
|
||||
|
||||
|
@ -12,15 +12,17 @@ export function PageNavigator({
|
||||
}) {
|
||||
let options: FilterOption[] = [];
|
||||
for (let pageMeta of allPages) {
|
||||
if (currentPage && currentPage === pageMeta.name) {
|
||||
continue;
|
||||
}
|
||||
// Order by last modified date in descending order
|
||||
let orderId = -pageMeta.lastModified;
|
||||
// Unless it was opened in this session
|
||||
if (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({
|
||||
...pageMeta,
|
||||
orderId: orderId,
|
||||
|
@ -557,8 +557,8 @@ export class Editor {
|
||||
this.editorView!.focus();
|
||||
}
|
||||
|
||||
async navigate(name: string, pos?: number) {
|
||||
await this.pageNavigator.navigate(name, pos);
|
||||
async navigate(name: string, pos?: number, replaceState = false) {
|
||||
await this.pageNavigator.navigate(name, pos, replaceState);
|
||||
}
|
||||
|
||||
async loadPage(pageName: string) {
|
||||
|
@ -13,12 +13,20 @@ export class PathPageNavigator {
|
||||
|
||||
constructor(readonly root: string = "") {}
|
||||
|
||||
async navigate(page: string, pos?: number) {
|
||||
window.history.pushState(
|
||||
{ page, pos },
|
||||
page,
|
||||
`${this.root}/${encodePageUrl(page)}`
|
||||
);
|
||||
async navigate(page: string, pos?: number, replaceState = false) {
|
||||
if (replaceState) {
|
||||
window.history.replaceState(
|
||||
{ page, pos },
|
||||
page,
|
||||
`${this.root}/${encodePageUrl(page)}`
|
||||
);
|
||||
} else {
|
||||
window.history.pushState(
|
||||
{ page, pos },
|
||||
page,
|
||||
`${this.root}/${encodePageUrl(page)}`
|
||||
);
|
||||
}
|
||||
window.dispatchEvent(
|
||||
new PopStateEvent("popstate", {
|
||||
state: { page, pos },
|
||||
|
@ -43,6 +43,14 @@ export default function reducer(
|
||||
showPageNavigator: false,
|
||||
};
|
||||
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 {
|
||||
...state,
|
||||
allPages: action.pages,
|
||||
|
@ -43,8 +43,13 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
|
||||
"editor.save": async () => {
|
||||
return editor.save(true);
|
||||
},
|
||||
"editor.navigate": async (ctx, name: string, pos: number) => {
|
||||
await editor.navigate(name, pos);
|
||||
"editor.navigate": async (
|
||||
ctx,
|
||||
name: string,
|
||||
pos: number,
|
||||
replaceState = false
|
||||
) => {
|
||||
await editor.navigate(name, pos, replaceState);
|
||||
},
|
||||
"editor.reloadPage": async (ctx) => {
|
||||
await editor.reloadPage();
|
||||
|
Loading…
Reference in New Issue
Block a user