Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial attempt to fix #560 #905

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions lib/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ export type ActionButton = {
export type EmojiConfig = {
aliases: string[][];
};

export type Decoration = {
tag: string;
prefix: string;
};

34 changes: 34 additions & 0 deletions plugs/editor/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import {
import { cacheFileListing } from "../federation/federation.ts";
import { queryObjects } from "../index/plug_api.ts";
import { folderName } from "$sb/lib/resolve.ts";
import { readSetting } from "$sb/lib/settings_page.ts";
import type { Decoration } from "$lib/web.ts";

let decorations: Decoration[] = [];

// Completion
export async function pageComplete(completeEvent: CompleteEvent) {
updateDecoratorConfig();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe await this so that you can be sure decorations will have a value from the first invocation (and you catch errors, which otherwise would go into the void).


// Try to match [[wikilink]]
let isWikilink = true;
let match = /\[\[([^\]@$#:\{}]*)$/.exec(completeEvent.linePrefix);
Expand Down Expand Up @@ -79,10 +85,19 @@ export async function pageComplete(completeEvent: CompleteEvent) {
from: completeEvent.pos - match[1].length,
options: allPages.map((pageMeta) => {
const completions: any[] = [];
let namePrefix = "";
const decor = decorations?.filter(d => pageMeta.tags?.some((t: any) => d.tag === t));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docorations will always have a value, right? So no need for the ? after it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe use .find instead of .filter?

if (decor === undefined || decor.length == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And without the ? from my previous comment, this can just become a .length check only probably.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And when switching to .find you can just check of not undefined I suppose.

namePrefix = "";
} else {
namePrefix = decor[0].prefix;
}
if (isWikilink) {
if (pageMeta.displayName) {
const decoratedName = namePrefix + pageMeta.displayName;
completions.push({
label: `${pageMeta.displayName}`,
displayLabel: `${decoratedName}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just displayLabel: decoratedName,

boost: new Date(pageMeta.lastModified).getTime(),
apply: pageMeta.tag === "template"
? pageMeta.name
Expand All @@ -93,8 +108,10 @@ export async function pageComplete(completeEvent: CompleteEvent) {
}
if (Array.isArray(pageMeta.aliases)) {
for (const alias of pageMeta.aliases) {
const decoratedName = namePrefix + alias;
completions.push({
label: `${alias}`,
displayLabel: `${decoratedName}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

boost: new Date(pageMeta.lastModified).getTime(),
apply: pageMeta.tag === "template"
? pageMeta.name
Expand All @@ -104,8 +121,10 @@ export async function pageComplete(completeEvent: CompleteEvent) {
});
}
}
const decoratedName = namePrefix + pageMeta.name;
completions.push({
label: `${pageMeta.name}`,
displayLabel: `${decoratedName}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same again

boost: new Date(pageMeta.lastModified).getTime(),
type: "page",
});
Expand All @@ -132,6 +151,7 @@ export async function pageComplete(completeEvent: CompleteEvent) {
};
}


function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
const name = fileMeta.name.substring(0, fileMeta.name.length - 3);
return {
Expand All @@ -143,3 +163,17 @@ function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
lastModified: new Date(fileMeta.lastModified).toISOString(),
} as PageMeta;
}

let lastConfigUpdate = 0;

async function updateDecoratorConfig() {
// Update at most every 5 seconds
if (Date.now() < lastConfigUpdate + 5000) return;
lastConfigUpdate = Date.now();
const decoratorConfig = await readSetting("decorations");
if (!decoratorConfig) {
return;
}

decorations = decoratorConfig;
}
18 changes: 14 additions & 4 deletions plugs/markdown/markdown_render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { encodePageRef, parsePageRef } from "$sb/lib/page_ref.ts";
import { Fragment, renderHtml, Tag } from "./html_render.ts";
import { isLocalPath } from "$sb/lib/resolve.ts";
import { PageMeta } from "$sb/types.ts";

export type MarkdownRenderOptions = {
failOnUnknown?: true;
Expand Down Expand Up @@ -554,25 +555,34 @@ function traverseTag(
export function renderMarkdownToHtml(
t: ParseTree,
options: MarkdownRenderOptions = {},
allPages: PageMeta[] = [],
) {
preprocess(t);
const htmlTree = posPreservingRender(t, options);
if (htmlTree && options.translateUrls) {
if (htmlTree) {
traverseTag(htmlTree, (t) => {
if (typeof t === "string") {
return;
}
if (t.name === "img") {
if (t.name === "img" && options.translateUrls) {
t.attrs!.src = options.translateUrls!(t.attrs!.src!, "image");
}

if (t.name === "a" && t.attrs!.href) {
t.attrs!.href = options.translateUrls!(t.attrs!.href, "link");
if (options.translateUrls) {
t.attrs!.href = options.translateUrls!(t.attrs!.href, "link");
}
if (t.attrs!["data-ref"]?.length) {
const pageMeta = allPages.filter(p => t.attrs!["data-ref"]!.startsWith(p.name));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe .find instead of .filter?

if (pageMeta) {
t.body = [(pageMeta[0]?.namePrefix ?? "") + t.body]
}
}
if (t.body.length === 0) {
t.body = [t.attrs!.href];
}
}
});
}
}
return renderHtml(htmlTree);
}
3 changes: 3 additions & 0 deletions type/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { defaultSettings } from "$common/settings.ts";
import {
ActionButton,
EmojiConfig,
Decoration,
FilterOption,
Notification,
PanelMode,
Expand All @@ -23,6 +24,7 @@ export type BuiltinSettings = {
// Format: compatible with docker ignore
spaceIgnore?: string;
emoji?: EmojiConfig;
decorations?: Decoration[];
};

export type PanelConfig = {
Expand Down Expand Up @@ -79,6 +81,7 @@ export type AppViewState = {
showConfirm: boolean;
confirmMessage?: string;
confirmCallback?: (value: boolean) => void;

};

export const initialViewState: AppViewState = {
Expand Down
4 changes: 2 additions & 2 deletions web/cm_plugins/markdown_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class MarkdownWidget extends WidgetType {
extendedMarkdownLanguage,
trimmedMarkdown,
);

const html = renderMarkdownToHtml(mdTree, {
// Annotate every element with its position so we can use it to put
// the cursor there when the user clicks on the table.
Expand All @@ -109,7 +109,7 @@ export class MarkdownWidget extends WidgetType {
return url;
},
preserveAttributes: true,
});
}, this.client.ui.viewState.allPages);

if (cachedHtml === html) {
// HTML still same as in cache, no need to re-render
Expand Down
4 changes: 2 additions & 2 deletions web/cm_plugins/wiki_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export function cleanWikiLinkPlugin(client: Client) {
}
return;
}

const pageMeta = client.ui.viewState.allPages.filter(p => p.name == url);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> .find again

const linkText = alias ||
(url.includes("/") ? url.split("/").pop()! : url);
(pageMeta[0]?.namePrefix ?? "") + (url.includes("/") ? url.split("/").pop()! : url);

// And replace it with a widget
widgets.push(
Expand Down
3 changes: 2 additions & 1 deletion web/components/page_navigator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FilterList } from "./filter.tsx";
import { FilterOption } from "$lib/web.ts";
import { FilterOption, Decoration } from "$lib/web.ts";
import { CompletionContext, CompletionResult } from "@codemirror/autocomplete";
import { PageMeta } from "../../plug-api/types.ts";
import { isFederationPath } from "$sb/lib/resolve.ts";
Expand Down Expand Up @@ -65,6 +65,7 @@ export function PageNavigator({
}
options.push({
...pageMeta,
name: (pageMeta.namePrefix ?? "") + pageMeta.name,
description,
orderId: orderId,
});
Expand Down
3 changes: 3 additions & 0 deletions web/components/top_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function TopBar({
lhs,
onClick,
rhs,
pageNamePrefix,
}: {
pageName?: string;
unsavedChanges: boolean;
Expand All @@ -46,6 +47,7 @@ export function TopBar({
actionButtons: ActionButton[];
lhs?: ComponentChildren;
rhs?: ComponentChildren;
pageNamePrefix?: string;
}) {
return (
<div
Expand All @@ -57,6 +59,7 @@ export function TopBar({
<div className="main">
<div className="inner">
<div className="wrapper">
<div className="sb-page-prefix">{pageNamePrefix}</div>
<span
id="sb-current-page"
className={isLoading
Expand Down
1 change: 1 addition & 0 deletions web/editor_ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export class MainUI {
style={{ flex: viewState.panels.lhs.mode }}
/>
)}
pageNamePrefix={viewState.currentPageMeta?.namePrefix ?? ""}
/>
<div id="sb-main">
{!!viewState.panels.lhs.mode && (
Expand Down
26 changes: 25 additions & 1 deletion web/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PageMeta } from "../plug-api/types.ts";
import { Action, AppViewState } from "../type/web.ts";
import { PageState } from "./navigator.ts";

export default function reducer(
state: AppViewState,
Expand All @@ -20,6 +22,13 @@ export default function reducer(
};
case "page-loaded": {
const mouseDetected = window.matchMedia("(any-pointer:fine)").matches;
const pageMeta = state.allPages.filter(p => p.name == action.meta.name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.find

const decor = state.settings.decorations?.filter(d => pageMeta[0]?.tags?.some(t => d.tag === t));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.find

if (decor === undefined || decor.length == 0) {
// Do nothing
} else {
action.meta.namePrefix = decor[0].prefix;
}
return {
...state,
isLoading: false,
Expand Down Expand Up @@ -58,16 +67,31 @@ export default function reducer(
const oldPageMeta = new Map(
[...state.allPages].map((pm) => [pm.name, pm]),
);
let currPageMeta = oldPageMeta.get(state.currentPage!);
if (currPageMeta === undefined) {
currPageMeta = {} as PageMeta;
}
for (const pageMeta of action.allPages) {
const oldPageMetaItem = oldPageMeta.get(pageMeta.name);
if (oldPageMetaItem && oldPageMetaItem.lastOpened) {
pageMeta.lastOpened = oldPageMetaItem.lastOpened;
}
let namePrefix = "";
const decor = state.settings.decorations?.filter(d => pageMeta.tags?.some((t: any) => d.tag === t));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.find

if (decor === undefined || decor.length == 0) {
namePrefix = "";
} else {
namePrefix = decor[0].prefix;
}
pageMeta.namePrefix = namePrefix;
if (pageMeta.name === state.currentPage) {
currPageMeta!.namePrefix = namePrefix;
}
}

return {
...state,
allPages: action.allPages,
currentPageMeta: currPageMeta,
};
}
case "start-navigate": {
Expand Down
10 changes: 10 additions & 0 deletions web/styles/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ body {
}
}

.sb-page-prefix {
display: flex;
align-items: baseline;
flex: 0 0 auto;
text-align: left;
padding: 1px;
margin-right: 3px;
font-family: var(--ui-font);
}

.sb-panel {
iframe {
background-color: var(--root-background-color);
Expand Down
Loading