2022-11-18 15:04:37 +00:00
|
|
|
|
// Third party web dependencies
|
2022-04-25 08:33:38 +00:00
|
|
|
|
import {
|
|
|
|
|
autocompletion,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
cLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
closeBrackets,
|
|
|
|
|
closeBracketsKeymap,
|
2022-12-21 13:55:24 +00:00
|
|
|
|
CompletionContext,
|
2022-04-25 08:33:38 +00:00
|
|
|
|
completionKeymap,
|
|
|
|
|
CompletionResult,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
cppLanguage,
|
|
|
|
|
csharpLanguage,
|
|
|
|
|
dartLanguage,
|
2022-04-04 13:25:07 +00:00
|
|
|
|
drawSelection,
|
|
|
|
|
dropCursor,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
EditorSelection,
|
|
|
|
|
EditorState,
|
2022-04-04 13:25:07 +00:00
|
|
|
|
EditorView,
|
2023-05-29 07:53:49 +00:00
|
|
|
|
gitIgnoreCompiler,
|
2022-04-04 13:25:07 +00:00
|
|
|
|
highlightSpecialChars,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
history,
|
|
|
|
|
historyKeymap,
|
|
|
|
|
indentOnInput,
|
|
|
|
|
indentWithTab,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
javaLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
javascriptLanguage,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
jsonLanguage,
|
2022-04-04 13:25:07 +00:00
|
|
|
|
KeyBinding,
|
|
|
|
|
keymap,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
kotlinLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
LanguageDescription,
|
|
|
|
|
LanguageSupport,
|
2022-12-21 15:08:51 +00:00
|
|
|
|
markdown,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
objectiveCLanguage,
|
|
|
|
|
objectiveCppLanguage,
|
|
|
|
|
postgresqlLanguage,
|
|
|
|
|
protobufLanguage,
|
|
|
|
|
pythonLanguage,
|
2022-05-17 09:53:17 +00:00
|
|
|
|
runScopeHandlers,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
rustLanguage,
|
|
|
|
|
scalaLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
searchKeymap,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
shellLanguage,
|
2023-01-08 11:24:12 +00:00
|
|
|
|
sqlLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
standardKeymap,
|
|
|
|
|
StreamLanguage,
|
|
|
|
|
syntaxHighlighting,
|
|
|
|
|
syntaxTree,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
tomlLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
typescriptLanguage,
|
2022-04-04 13:25:07 +00:00
|
|
|
|
ViewPlugin,
|
2022-04-25 08:33:38 +00:00
|
|
|
|
ViewUpdate,
|
2023-01-22 17:45:01 +00:00
|
|
|
|
xmlLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
yamlLanguage,
|
|
|
|
|
} from "../common/deps.ts";
|
|
|
|
|
import { SilverBulletHooks } from "../common/manifest.ts";
|
2022-12-14 19:04:20 +00:00
|
|
|
|
import {
|
|
|
|
|
loadMarkdownExtensions,
|
|
|
|
|
MDExt,
|
|
|
|
|
} from "../common/markdown_parser/markdown_ext.ts";
|
|
|
|
|
import buildMarkdown from "../common/markdown_parser/parser.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
|
import { Space } from "./space.ts";
|
|
|
|
|
import { markdownSyscalls } from "./syscalls/markdown.ts";
|
|
|
|
|
import { FilterOption, PageMeta } from "./types.ts";
|
|
|
|
|
import { isMacLike, parseYamlSettings, safeRun } from "../common/util.ts";
|
2022-10-21 15:06:14 +00:00
|
|
|
|
import { createSandbox } from "../plugos/environments/webworker_sandbox.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
import { EventHook } from "../plugos/hooks/event.ts";
|
2022-12-21 15:08:51 +00:00
|
|
|
|
import assetSyscalls from "../plugos/syscalls/asset.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
import { eventSyscalls } from "../plugos/syscalls/event.ts";
|
|
|
|
|
import { System } from "../plugos/system.ts";
|
2022-12-21 15:08:51 +00:00
|
|
|
|
import { cleanModePlugins } from "./cm_plugins/clean.ts";
|
|
|
|
|
import { CollabState } from "./cm_plugins/collab.ts";
|
|
|
|
|
import {
|
|
|
|
|
attachmentExtension,
|
|
|
|
|
pasteLinkExtension,
|
|
|
|
|
} from "./cm_plugins/editor_paste.ts";
|
|
|
|
|
import { inlineImagesPlugin } from "./cm_plugins/inline_image.ts";
|
|
|
|
|
import { lineWrapper } from "./cm_plugins/line_wrapper.ts";
|
|
|
|
|
import { smartQuoteKeymap } from "./cm_plugins/smart_quotes.ts";
|
|
|
|
|
import { Confirm, Prompt } from "./components/basic_modals.tsx";
|
|
|
|
|
import { CommandPalette } from "./components/command_palette.tsx";
|
|
|
|
|
import { FilterList } from "./components/filter.tsx";
|
|
|
|
|
import { PageNavigator } from "./components/page_navigator.tsx";
|
|
|
|
|
import { Panel } from "./components/panel.tsx";
|
|
|
|
|
import { TopBar } from "./components/top_bar.tsx";
|
|
|
|
|
import {
|
|
|
|
|
BookIcon,
|
|
|
|
|
HomeIcon,
|
|
|
|
|
preactRender,
|
|
|
|
|
TerminalIcon,
|
|
|
|
|
useEffect,
|
|
|
|
|
useReducer,
|
|
|
|
|
vim,
|
|
|
|
|
yUndoManagerKeymap,
|
|
|
|
|
} from "./deps.ts";
|
2023-01-04 15:37:09 +00:00
|
|
|
|
import { AppCommand, CommandHook } from "./hooks/command.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
import { SlashCommandHook } from "./hooks/slash_command.ts";
|
2022-12-21 15:08:51 +00:00
|
|
|
|
import { PathPageNavigator } from "./navigator.ts";
|
|
|
|
|
import reducer from "./reducer.ts";
|
|
|
|
|
import customMarkdownStyle from "./style.ts";
|
|
|
|
|
import { collabSyscalls } from "./syscalls/collab.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
import { editorSyscalls } from "./syscalls/editor.ts";
|
|
|
|
|
import { spaceSyscalls } from "./syscalls/space.ts";
|
|
|
|
|
import { systemSyscalls } from "./syscalls/system.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
|
import {
|
|
|
|
|
Action,
|
|
|
|
|
AppViewState,
|
|
|
|
|
BuiltinSettings,
|
|
|
|
|
initialViewState,
|
|
|
|
|
} from "./types.ts";
|
2022-11-18 15:04:37 +00:00
|
|
|
|
|
2022-12-21 13:55:24 +00:00
|
|
|
|
import type {
|
|
|
|
|
AppEvent,
|
|
|
|
|
ClickEvent,
|
|
|
|
|
CompleteEvent,
|
|
|
|
|
} from "../plug-api/app_event.ts";
|
2022-12-22 15:20:05 +00:00
|
|
|
|
import { CodeWidgetHook } from "./hooks/code_widget.ts";
|
2023-01-14 17:51:00 +00:00
|
|
|
|
import { throttle } from "../common/async_util.ts";
|
2023-01-16 10:28:59 +00:00
|
|
|
|
import { readonlyMode } from "./cm_plugins/readonly.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
|
import { PageNamespaceHook } from "../common/hooks/page_namespace.ts";
|
|
|
|
|
import { CronHook } from "../plugos/hooks/cron.ts";
|
|
|
|
|
import { pageIndexSyscalls } from "./syscalls/index.ts";
|
|
|
|
|
import { storeSyscalls } from "../plugos/syscalls/store.dexie_browser.ts";
|
|
|
|
|
import { PlugSpacePrimitives } from "../common/spaces/plug_space_primitives.ts";
|
|
|
|
|
import { IndexedDBSpacePrimitives } from "../common/spaces/indexeddb_space_primitives.ts";
|
|
|
|
|
import { FileMetaSpacePrimitives } from "../common/spaces/file_meta_space_primitives.ts";
|
|
|
|
|
import { EventedSpacePrimitives } from "../common/spaces/evented_space_primitives.ts";
|
|
|
|
|
import { clientStoreSyscalls } from "./syscalls/clientStore.ts";
|
|
|
|
|
import { sandboxFetchSyscalls } from "./syscalls/fetch.ts";
|
|
|
|
|
import { shellSyscalls } from "./syscalls/shell.ts";
|
|
|
|
|
import { SyncService } from "./sync_service.ts";
|
|
|
|
|
import { yamlSyscalls } from "./syscalls/yaml.ts";
|
|
|
|
|
import { simpleHash } from "../common/crypto.ts";
|
|
|
|
|
import { DexieKVStore } from "../plugos/lib/kv_store.dexie.ts";
|
|
|
|
|
import { SyncStatus } from "../common/spaces/sync.ts";
|
|
|
|
|
import { HttpSpacePrimitives } from "../common/spaces/http_space_primitives.ts";
|
|
|
|
|
import { FallbackSpacePrimitives } from "../common/spaces/fallback_space_primitives.ts";
|
|
|
|
|
import { syncSyscalls } from "./syscalls/sync.ts";
|
2023-05-29 07:53:49 +00:00
|
|
|
|
import { FilteredSpacePrimitives } from "../common/spaces/filtered_space_primitives.ts";
|
|
|
|
|
import { globToRegExp } from "https://deno.land/std@0.189.0/path/glob.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
|
|
const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/;
|
2022-11-24 15:55:30 +00:00
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
|
class PageState {
|
2022-04-26 17:04:36 +00:00
|
|
|
|
constructor(
|
|
|
|
|
readonly scrollTop: number,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
readonly selection: EditorSelection,
|
2022-04-26 17:04:36 +00:00
|
|
|
|
) {}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 13:21:30 +00:00
|
|
|
|
const saveInterval = 1000;
|
2022-03-31 12:28:07 +00:00
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
|
declare global {
|
|
|
|
|
interface Window {
|
|
|
|
|
// Injected via index.html
|
|
|
|
|
silverBulletConfig: {
|
|
|
|
|
spaceFolderPath: string;
|
|
|
|
|
syncEndpoint: string;
|
|
|
|
|
};
|
|
|
|
|
editor: Editor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Oh my god, need to refactor this
|
2022-04-19 14:54:47 +00:00
|
|
|
|
export class Editor {
|
2022-03-29 10:13:46 +00:00
|
|
|
|
readonly commandHook: CommandHook;
|
|
|
|
|
readonly slashCommandHook: SlashCommandHook;
|
2022-03-23 14:41:12 +00:00
|
|
|
|
openPages = new Map<string, PageState>();
|
2022-03-20 08:56:28 +00:00
|
|
|
|
editorView?: EditorView;
|
2023-05-23 18:53:53 +00:00
|
|
|
|
viewState: AppViewState = initialViewState;
|
|
|
|
|
viewDispatch: (action: Action) => void = () => {};
|
2022-04-07 13:21:30 +00:00
|
|
|
|
space: Space;
|
2023-05-23 18:53:53 +00:00
|
|
|
|
remoteSpacePrimitives: HttpSpacePrimitives;
|
|
|
|
|
|
|
|
|
|
pageNavigator?: PathPageNavigator;
|
2022-03-29 09:21:32 +00:00
|
|
|
|
eventHook: EventHook;
|
2022-12-22 15:20:05 +00:00
|
|
|
|
codeWidgetHook: CodeWidgetHook;
|
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
|
saveTimeout: any;
|
2022-04-04 13:25:07 +00:00
|
|
|
|
debouncedUpdateEvent = throttle(() => {
|
2022-04-19 14:54:47 +00:00
|
|
|
|
this.eventHook
|
|
|
|
|
.dispatchEvent("editor:updated")
|
|
|
|
|
.catch((e) => console.error("Error dispatching editor:updated event", e));
|
2022-04-04 13:25:07 +00:00
|
|
|
|
}, 1000);
|
2023-01-16 09:40:16 +00:00
|
|
|
|
system: System<SilverBulletHooks>;
|
|
|
|
|
mdExtensions: MDExt[] = [];
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
|
|
// Track if plugs have been updated since sync cycle
|
|
|
|
|
private plugsUpdated = false;
|
|
|
|
|
fullSyncCompleted = false;
|
2022-12-16 11:44:04 +00:00
|
|
|
|
|
|
|
|
|
// Runtime state (that doesn't make sense in viewState)
|
2022-10-25 16:50:07 +00:00
|
|
|
|
collabState?: CollabState;
|
2023-05-23 18:53:53 +00:00
|
|
|
|
syncService: SyncService;
|
|
|
|
|
settings?: BuiltinSettings;
|
|
|
|
|
kvStore: DexieKVStore;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
2022-08-02 10:43:39 +00:00
|
|
|
|
constructor(
|
|
|
|
|
parent: Element,
|
|
|
|
|
) {
|
2023-05-23 18:53:53 +00:00
|
|
|
|
const runtimeConfig = window.silverBulletConfig;
|
|
|
|
|
|
|
|
|
|
// Instantiate a PlugOS system
|
|
|
|
|
const system = new System<SilverBulletHooks>();
|
2022-11-24 11:04:00 +00:00
|
|
|
|
this.system = system;
|
2022-03-25 11:03:06 +00:00
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
|
// Generate a semi-unique prefix for the database so not to reuse databases for different space paths
|
|
|
|
|
const dbPrefix = "" + simpleHash(runtimeConfig.spaceFolderPath);
|
|
|
|
|
|
|
|
|
|
// Attach the page namespace hook
|
|
|
|
|
const namespaceHook = new PageNamespaceHook();
|
|
|
|
|
system.addHook(namespaceHook);
|
|
|
|
|
|
|
|
|
|
// Event hook
|
|
|
|
|
this.eventHook = new EventHook();
|
|
|
|
|
system.addHook(this.eventHook);
|
|
|
|
|
|
|
|
|
|
// Cron hook
|
|
|
|
|
const cronHook = new CronHook(system);
|
|
|
|
|
system.addHook(cronHook);
|
|
|
|
|
|
|
|
|
|
const indexSyscalls = pageIndexSyscalls(
|
|
|
|
|
`${dbPrefix}_page_index`,
|
|
|
|
|
globalThis.indexedDB,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.kvStore = new DexieKVStore(
|
|
|
|
|
`${dbPrefix}_store`,
|
|
|
|
|
"data",
|
|
|
|
|
globalThis.indexedDB,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const storeCalls = storeSyscalls(this.kvStore);
|
|
|
|
|
|
|
|
|
|
// Setup space
|
|
|
|
|
this.remoteSpacePrimitives = new HttpSpacePrimitives(
|
|
|
|
|
runtimeConfig.syncEndpoint,
|
|
|
|
|
runtimeConfig.spaceFolderPath,
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const plugSpacePrimitives = new PlugSpacePrimitives(
|
|
|
|
|
// Using fallback space primitives here to allow (by default) local reads to "fall through" to HTTP when files aren't synced yet
|
|
|
|
|
new FallbackSpacePrimitives(
|
|
|
|
|
new IndexedDBSpacePrimitives(
|
|
|
|
|
`${dbPrefix}_space`,
|
|
|
|
|
globalThis.indexedDB,
|
|
|
|
|
),
|
|
|
|
|
this.remoteSpacePrimitives,
|
|
|
|
|
),
|
|
|
|
|
namespaceHook,
|
|
|
|
|
);
|
|
|
|
|
|
2023-05-29 07:53:49 +00:00
|
|
|
|
let fileFilterFn: (s: string) => boolean = () => true;
|
|
|
|
|
const localSpacePrimitives = new FilteredSpacePrimitives(
|
|
|
|
|
new FileMetaSpacePrimitives(
|
|
|
|
|
new EventedSpacePrimitives(
|
|
|
|
|
plugSpacePrimitives,
|
|
|
|
|
this.eventHook,
|
|
|
|
|
),
|
|
|
|
|
indexSyscalls,
|
2023-05-23 18:53:53 +00:00
|
|
|
|
),
|
2023-05-29 07:53:49 +00:00
|
|
|
|
(meta) => fileFilterFn(meta.name),
|
|
|
|
|
async () => {
|
|
|
|
|
await this.loadSettings();
|
|
|
|
|
if (typeof this.settings?.spaceIgnore === "string") {
|
|
|
|
|
fileFilterFn = gitIgnoreCompiler(this.settings.spaceIgnore).accepts;
|
|
|
|
|
} else {
|
|
|
|
|
fileFilterFn = () => true;
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-05-23 18:53:53 +00:00
|
|
|
|
);
|
|
|
|
|
|
2023-05-29 08:26:56 +00:00
|
|
|
|
this.space = new Space(localSpacePrimitives, this.kvStore);
|
2023-05-23 18:53:53 +00:00
|
|
|
|
this.space.watch();
|
|
|
|
|
|
|
|
|
|
this.syncService = new SyncService(
|
|
|
|
|
localSpacePrimitives,
|
|
|
|
|
this.remoteSpacePrimitives,
|
|
|
|
|
this.kvStore,
|
|
|
|
|
this.eventHook,
|
|
|
|
|
(path) => {
|
|
|
|
|
// TODO: At some point we should remove the data.db exception here
|
|
|
|
|
return path !== "data.db" && !plugSpacePrimitives.isLikelyHandled(path);
|
|
|
|
|
},
|
|
|
|
|
);
|
2022-03-29 09:21:32 +00:00
|
|
|
|
|
2022-12-22 15:20:05 +00:00
|
|
|
|
// Code widget hook
|
|
|
|
|
this.codeWidgetHook = new CodeWidgetHook();
|
|
|
|
|
this.system.addHook(this.codeWidgetHook);
|
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
|
// Command hook
|
|
|
|
|
this.commandHook = new CommandHook();
|
|
|
|
|
this.commandHook.on({
|
2022-06-20 16:30:45 +00:00
|
|
|
|
commandsUpdated: (commandMap) => {
|
2022-03-29 09:21:32 +00:00
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "update-commands",
|
|
|
|
|
commands: commandMap,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
this.system.addHook(this.commandHook);
|
|
|
|
|
|
|
|
|
|
// Slash command hook
|
|
|
|
|
this.slashCommandHook = new SlashCommandHook(this);
|
|
|
|
|
this.system.addHook(this.slashCommandHook);
|
2022-03-25 11:03:06 +00:00
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
|
this.render(parent);
|
2022-10-25 16:50:07 +00:00
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
|
this.editorView = new EditorView({
|
2023-01-16 10:28:59 +00:00
|
|
|
|
state: this.createEditorState("", "", false),
|
2022-07-22 11:44:28 +00:00
|
|
|
|
parent: document.getElementById("sb-editor")!,
|
2022-03-20 08:56:28 +00:00
|
|
|
|
});
|
2022-12-21 13:55:24 +00:00
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
|
// Syscalls available to all plugs
|
2022-04-11 18:34:09 +00:00
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
|
[],
|
2022-07-04 13:07:27 +00:00
|
|
|
|
eventSyscalls(this.eventHook),
|
2022-05-09 12:59:12 +00:00
|
|
|
|
editorSyscalls(this),
|
|
|
|
|
spaceSyscalls(this),
|
2022-10-28 14:17:40 +00:00
|
|
|
|
systemSyscalls(this, this.system),
|
2022-05-09 12:59:12 +00:00
|
|
|
|
markdownSyscalls(buildMarkdown(this.mdExtensions)),
|
2022-10-10 12:50:21 +00:00
|
|
|
|
assetSyscalls(this.system),
|
2022-10-25 16:50:07 +00:00
|
|
|
|
collabSyscalls(this),
|
2023-05-23 18:53:53 +00:00
|
|
|
|
yamlSyscalls(),
|
|
|
|
|
storeCalls,
|
|
|
|
|
indexSyscalls,
|
|
|
|
|
syncSyscalls(this.syncService),
|
|
|
|
|
// LEGACY
|
|
|
|
|
clientStoreSyscalls(storeCalls),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Syscalls that require some additional permissions
|
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
|
["fetch"],
|
|
|
|
|
sandboxFetchSyscalls(this.remoteSpacePrimitives),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
|
["shell"],
|
|
|
|
|
shellSyscalls(this.remoteSpacePrimitives),
|
2022-04-11 18:34:09 +00:00
|
|
|
|
);
|
2022-05-13 12:36:26 +00:00
|
|
|
|
|
2022-05-17 09:53:17 +00:00
|
|
|
|
// Make keyboard shortcuts work even when the editor is in read only mode or not focused
|
2022-10-10 12:50:21 +00:00
|
|
|
|
globalThis.addEventListener("keydown", (ev) => {
|
2022-05-17 09:53:17 +00:00
|
|
|
|
if (!this.editorView?.hasFocus) {
|
2022-12-21 13:55:24 +00:00
|
|
|
|
if ((ev.target as any).closest(".cm-editor")) {
|
|
|
|
|
// In some cm element, let's back out
|
2022-07-19 11:49:54 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-05-17 09:53:17 +00:00
|
|
|
|
if (runScopeHandlers(this.editorView!, ev, "editor")) {
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-07-04 14:28:22 +00:00
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
|
globalThis.addEventListener("touchstart", (ev) => {
|
2023-05-23 18:53:53 +00:00
|
|
|
|
// Launch the page picker on a two-finger tap
|
|
|
|
|
if (ev.touches.length === 2) {
|
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
this.viewDispatch({ type: "start-navigate" });
|
|
|
|
|
}
|
2022-07-04 14:28:22 +00:00
|
|
|
|
// Launch the command palette using a three-finger tap
|
2023-05-23 18:53:53 +00:00
|
|
|
|
if (ev.touches.length === 3) {
|
2022-07-04 14:28:22 +00:00
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
ev.preventDefault();
|
2022-12-16 15:35:05 +00:00
|
|
|
|
this.viewDispatch({ type: "show-palette", context: this.getContext() });
|
2022-07-04 14:28:22 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
|
|
this.eventHook.addLocalListener("plug:changed", async (fileName) => {
|
|
|
|
|
console.log("Plug updated, reloading:", fileName);
|
|
|
|
|
system.unload(fileName);
|
|
|
|
|
await system.load(
|
|
|
|
|
// await this.space.readFile(fileName, "utf8"),
|
|
|
|
|
new URL(`/.fs/${fileName}`, location.href),
|
|
|
|
|
createSandbox,
|
|
|
|
|
);
|
|
|
|
|
this.plugsUpdated = true;
|
|
|
|
|
});
|
2022-04-03 16:42:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get currentPage(): string | undefined {
|
|
|
|
|
return this.viewState.currentPage;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
|
this.focus();
|
|
|
|
|
|
2022-11-24 11:04:00 +00:00
|
|
|
|
this.space.on({
|
|
|
|
|
pageChanged: (meta) => {
|
|
|
|
|
if (this.currentPage === meta.name) {
|
2023-05-23 18:53:53 +00:00
|
|
|
|
console.log("Page changed elsewhere, reloading");
|
|
|
|
|
this.flashNotification("Page changed elsewhere, reloading");
|
2022-11-24 11:04:00 +00:00
|
|
|
|
this.reloadPage();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
pageListUpdated: (pages) => {
|
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "pages-listed",
|
|
|
|
|
pages: pages,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
|
// Load settings
|
|
|
|
|
this.settings = await this.loadSettings();
|
|
|
|
|
|
|
|
|
|
this.pageNavigator = new PathPageNavigator(
|
|
|
|
|
this.settings.indexPage,
|
|
|
|
|
);
|
|
|
|
|
|
2022-11-24 11:04:00 +00:00
|
|
|
|
await this.reloadPlugs();
|
|
|
|
|
|
2022-08-30 08:44:20 +00:00
|
|
|
|
this.pageNavigator.subscribe(async (pageName, pos: number | string) => {
|
2022-09-13 06:41:01 +00:00
|
|
|
|
console.log("Now navigating to", pageName);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
if (!this.editorView) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const stateRestored = await this.loadPage(pageName);
|
2022-03-28 13:25:05 +00:00
|
|
|
|
if (pos) {
|
2022-08-30 08:44:20 +00:00
|
|
|
|
if (typeof pos === "string") {
|
2022-10-24 17:40:52 +00:00
|
|
|
|
console.log("Navigating to anchor", pos);
|
2022-08-30 08:44:20 +00:00
|
|
|
|
|
|
|
|
|
// We're going to look up the anchor through a direct page store query...
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const posLookup = await this.system.localSyscall(
|
|
|
|
|
"core",
|
|
|
|
|
"index.get",
|
|
|
|
|
[
|
|
|
|
|
pageName,
|
2022-10-24 17:40:52 +00:00
|
|
|
|
`a:${pageName}:${pos}`,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
],
|
|
|
|
|
);
|
2022-08-30 08:44:20 +00:00
|
|
|
|
|
|
|
|
|
if (!posLookup) {
|
|
|
|
|
return this.flashNotification(
|
|
|
|
|
`Could not find anchor @${pos}`,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
"error",
|
2022-08-30 08:44:20 +00:00
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
pos = +posLookup;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-28 13:25:05 +00:00
|
|
|
|
this.editorView.dispatch({
|
|
|
|
|
selection: { anchor: pos },
|
2022-08-30 08:44:20 +00:00
|
|
|
|
scrollIntoView: true,
|
2022-03-28 13:25:05 +00:00
|
|
|
|
});
|
2022-09-06 14:33:00 +00:00
|
|
|
|
} else if (!stateRestored) {
|
2022-11-24 15:55:30 +00:00
|
|
|
|
// Somewhat ad-hoc way to determine if the document contains frontmatter and if so, putting the cursor _after it_.
|
|
|
|
|
const pageText = this.editorView.state.sliceDoc();
|
|
|
|
|
|
|
|
|
|
// Default the cursor to be at position 0
|
|
|
|
|
let initialCursorPos = 0;
|
|
|
|
|
const match = frontMatterRegex.exec(pageText);
|
|
|
|
|
if (match) {
|
2022-11-29 08:17:40 +00:00
|
|
|
|
// Frontmatter found, put cursor after it
|
2022-12-19 11:35:58 +00:00
|
|
|
|
initialCursorPos = match[0].length;
|
2022-11-24 15:55:30 +00:00
|
|
|
|
}
|
2022-11-28 15:42:54 +00:00
|
|
|
|
// By default scroll to the top
|
|
|
|
|
this.editorView.scrollDOM.scrollTop = 0;
|
2022-09-06 14:33:00 +00:00
|
|
|
|
this.editorView.dispatch({
|
2022-11-24 15:55:30 +00:00
|
|
|
|
selection: { anchor: initialCursorPos },
|
2022-11-28 15:42:54 +00:00
|
|
|
|
// And then scroll down if required
|
2022-09-06 14:33:00 +00:00
|
|
|
|
scrollIntoView: true,
|
|
|
|
|
});
|
2022-03-28 13:25:05 +00:00
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
});
|
|
|
|
|
|
2023-02-28 09:07:20 +00:00
|
|
|
|
this.loadCustomStyles().catch(console.error);
|
2023-01-22 14:48:12 +00:00
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
|
// Kick off background sync
|
|
|
|
|
this.syncService.start();
|
|
|
|
|
|
|
|
|
|
this.eventHook.addLocalListener("sync:success", async (operations) => {
|
2023-05-29 07:53:49 +00:00
|
|
|
|
// console.log("Operations", operations);
|
2023-05-23 18:53:53 +00:00
|
|
|
|
if (operations > 0) {
|
|
|
|
|
// Update the page list
|
|
|
|
|
await this.space.updatePageList();
|
|
|
|
|
}
|
|
|
|
|
if (operations !== undefined) {
|
|
|
|
|
// "sync:success" is called with a number of operations only from syncSpace(), not from syncing individual pages
|
|
|
|
|
this.fullSyncCompleted = true;
|
|
|
|
|
}
|
|
|
|
|
if (this.plugsUpdated) {
|
|
|
|
|
// To register new commands, update editor state based on new plugs
|
|
|
|
|
this.rebuildEditorState();
|
2023-05-24 04:47:39 +00:00
|
|
|
|
this.dispatchAppEvent("editor:pageLoaded", this.currentPage);
|
2023-05-23 18:53:53 +00:00
|
|
|
|
if (operations) {
|
|
|
|
|
// Likely initial sync so let's show visually that we're synced now
|
|
|
|
|
this.flashNotification(`Synced ${operations} files`, "info");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Reset for next sync cycle
|
|
|
|
|
this.plugsUpdated = false;
|
|
|
|
|
|
|
|
|
|
this.viewDispatch({ type: "sync-change", synced: true });
|
|
|
|
|
});
|
|
|
|
|
this.eventHook.addLocalListener("sync:error", (name) => {
|
|
|
|
|
this.viewDispatch({ type: "sync-change", synced: false });
|
|
|
|
|
});
|
|
|
|
|
this.eventHook.addLocalListener("sync:conflict", (name) => {
|
|
|
|
|
this.flashNotification(
|
|
|
|
|
`Sync: conflict detected for ${name} - conflict copy created`,
|
|
|
|
|
"error",
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
this.eventHook.addLocalListener("sync:progress", (status: SyncStatus) => {
|
|
|
|
|
this.flashNotification(
|
|
|
|
|
`Sync: ${
|
|
|
|
|
Math.round(status.filesProcessed / status.totalFiles * 10000) /
|
|
|
|
|
100
|
|
|
|
|
}% — processed ${status.filesProcessed} out of ${status.totalFiles}`,
|
|
|
|
|
"info",
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-11 07:08:22 +00:00
|
|
|
|
await this.dispatchAppEvent("editor:init");
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
|
async loadSettings(): Promise<BuiltinSettings> {
|
|
|
|
|
let settingsText: string | undefined;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
settingsText = (await this.space.readPage("SETTINGS")).text;
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
console.log("No SETTINGS page, falling back to default");
|
|
|
|
|
settingsText = "```yaml\nindexPage: index\n```\n";
|
|
|
|
|
}
|
|
|
|
|
const settings = parseYamlSettings(settingsText!) as BuiltinSettings;
|
|
|
|
|
|
|
|
|
|
if (!settings.indexPage) {
|
|
|
|
|
settings.indexPage = "index";
|
|
|
|
|
}
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
|
save(immediate = false): Promise<void> {
|
2022-03-31 12:28:07 +00:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
if (this.saveTimeout) {
|
|
|
|
|
clearTimeout(this.saveTimeout);
|
|
|
|
|
}
|
|
|
|
|
this.saveTimeout = setTimeout(
|
|
|
|
|
() => {
|
|
|
|
|
if (this.currentPage) {
|
2022-12-21 13:55:24 +00:00
|
|
|
|
if (
|
|
|
|
|
!this.viewState.unsavedChanges ||
|
|
|
|
|
this.viewState.uiOptions.forcedROMode
|
|
|
|
|
) {
|
2022-10-22 18:23:54 +00:00
|
|
|
|
// No unsaved changes, or read-only mode, not gonna save
|
2022-10-21 17:02:00 +00:00
|
|
|
|
return resolve();
|
|
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
|
console.log("Saving page", this.currentPage);
|
|
|
|
|
this.space
|
|
|
|
|
.writePage(
|
|
|
|
|
this.currentPage,
|
|
|
|
|
this.editorView!.state.sliceDoc(0),
|
2022-10-10 12:50:21 +00:00
|
|
|
|
true,
|
2022-03-31 12:28:07 +00:00
|
|
|
|
)
|
|
|
|
|
.then(() => {
|
|
|
|
|
this.viewDispatch({ type: "page-saved" });
|
|
|
|
|
resolve();
|
|
|
|
|
})
|
2022-07-19 15:21:11 +00:00
|
|
|
|
.catch((e) => {
|
|
|
|
|
this.flashNotification(
|
|
|
|
|
"Could not save page, retrying again in 10 seconds",
|
2022-10-10 12:50:21 +00:00
|
|
|
|
"error",
|
2022-07-19 15:21:11 +00:00
|
|
|
|
);
|
|
|
|
|
this.saveTimeout = setTimeout(this.save.bind(this), 10000);
|
|
|
|
|
reject(e);
|
|
|
|
|
});
|
2022-03-31 12:28:07 +00:00
|
|
|
|
} else {
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-10-10 12:50:21 +00:00
|
|
|
|
immediate ? 0 : saveInterval,
|
2022-03-31 12:28:07 +00:00
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 11:32:28 +00:00
|
|
|
|
flashNotification(message: string, type: "info" | "error" = "info") {
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const id = Math.floor(Math.random() * 1000000);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "show-notification",
|
|
|
|
|
notification: {
|
2022-07-14 11:32:28 +00:00
|
|
|
|
id,
|
|
|
|
|
type,
|
|
|
|
|
message,
|
2022-03-20 08:56:28 +00:00
|
|
|
|
date: new Date(),
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-07-14 11:32:28 +00:00
|
|
|
|
setTimeout(
|
|
|
|
|
() => {
|
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "dismiss-notification",
|
|
|
|
|
id: id,
|
|
|
|
|
});
|
|
|
|
|
},
|
2023-05-23 18:53:53 +00:00
|
|
|
|
type === "info" ? 4000 : 5000,
|
2022-07-14 11:32:28 +00:00
|
|
|
|
);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 12:46:52 +00:00
|
|
|
|
filterBox(
|
|
|
|
|
label: string,
|
|
|
|
|
options: FilterOption[],
|
2022-10-10 12:50:21 +00:00
|
|
|
|
helpText = "",
|
|
|
|
|
placeHolder = "",
|
2022-04-13 12:46:52 +00:00
|
|
|
|
): Promise<FilterOption | undefined> {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "show-filterbox",
|
2022-04-21 09:46:33 +00:00
|
|
|
|
label,
|
2022-04-13 12:46:52 +00:00
|
|
|
|
options,
|
|
|
|
|
placeHolder,
|
|
|
|
|
helpText,
|
2022-12-08 16:04:07 +00:00
|
|
|
|
onSelect: (option: any) => {
|
2022-04-13 12:46:52 +00:00
|
|
|
|
this.viewDispatch({ type: "hide-filterbox" });
|
2022-04-19 14:54:47 +00:00
|
|
|
|
this.focus();
|
2022-04-13 12:46:52 +00:00
|
|
|
|
resolve(option);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 15:08:51 +00:00
|
|
|
|
prompt(
|
|
|
|
|
message: string,
|
|
|
|
|
defaultValue = "",
|
|
|
|
|
): Promise<string | undefined> {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "show-prompt",
|
|
|
|
|
message,
|
|
|
|
|
defaultValue,
|
|
|
|
|
callback: (value: string | undefined) => {
|
|
|
|
|
this.viewDispatch({ type: "hide-prompt" });
|
|
|
|
|
this.focus();
|
|
|
|
|
resolve(value);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
confirm(
|
|
|
|
|
message: string,
|
|
|
|
|
): Promise<boolean> {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "show-confirm",
|
|
|
|
|
message,
|
|
|
|
|
callback: (value: boolean) => {
|
|
|
|
|
this.viewDispatch({ type: "hide-confirm" });
|
|
|
|
|
this.focus();
|
|
|
|
|
resolve(value);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
|
dispatchAppEvent(name: AppEvent, data?: any): Promise<any[]> {
|
2022-03-29 09:21:32 +00:00
|
|
|
|
return this.eventHook.dispatchEvent(name, data);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 10:28:59 +00:00
|
|
|
|
createEditorState(
|
|
|
|
|
pageName: string,
|
|
|
|
|
text: string,
|
|
|
|
|
readOnly: boolean,
|
|
|
|
|
): EditorState {
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const commandKeyBindings: KeyBinding[] = [];
|
|
|
|
|
for (const def of this.commandHook.editorCommands.values()) {
|
2022-03-20 08:56:28 +00:00
|
|
|
|
if (def.command.key) {
|
|
|
|
|
commandKeyBindings.push({
|
|
|
|
|
key: def.command.key,
|
|
|
|
|
mac: def.command.mac,
|
|
|
|
|
run: (): boolean => {
|
2022-04-21 09:46:33 +00:00
|
|
|
|
if (def.command.contexts) {
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const context = this.getContext();
|
2022-04-21 09:46:33 +00:00
|
|
|
|
if (!context || !def.command.contexts.includes(context)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
Promise.resolve()
|
2022-03-28 13:25:05 +00:00
|
|
|
|
.then(def.run)
|
|
|
|
|
.catch((e: any) => {
|
|
|
|
|
console.error(e);
|
2022-07-14 11:32:28 +00:00
|
|
|
|
this.flashNotification(
|
|
|
|
|
`Error running command: ${e.message}`,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
"error",
|
2022-07-14 11:32:28 +00:00
|
|
|
|
);
|
2022-07-30 10:57:52 +00:00
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
// Always be focusing the editor after running a command
|
|
|
|
|
editor.focus();
|
2022-03-28 13:25:05 +00:00
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
|
// deno-lint-ignore no-this-alias
|
2022-03-31 12:28:07 +00:00
|
|
|
|
const editor = this;
|
2023-01-08 11:24:12 +00:00
|
|
|
|
let touchCount = 0;
|
2022-11-25 12:05:41 +00:00
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
|
return EditorState.create({
|
2022-10-25 16:50:07 +00:00
|
|
|
|
doc: this.collabState ? this.collabState.ytext.toString() : text,
|
2022-03-20 08:56:28 +00:00
|
|
|
|
extensions: [
|
2022-12-21 13:55:24 +00:00
|
|
|
|
// Not using CM theming right now, but some extensions depend on the "dark" thing
|
|
|
|
|
EditorView.theme({}, { dark: this.viewState.uiOptions.darkMode }),
|
2022-12-15 14:55:50 +00:00
|
|
|
|
// Enable vim mode, or not
|
2022-12-21 13:55:24 +00:00
|
|
|
|
[...editor.viewState.uiOptions.vimMode ? [vim({ status: true })] : []],
|
2023-01-16 10:28:59 +00:00
|
|
|
|
[
|
|
|
|
|
...readOnly || editor.viewState.uiOptions.forcedROMode
|
|
|
|
|
? [readonlyMode()]
|
|
|
|
|
: [],
|
|
|
|
|
],
|
2022-12-15 14:55:50 +00:00
|
|
|
|
// The uber markdown mode
|
2022-06-14 07:45:22 +00:00
|
|
|
|
markdown({
|
|
|
|
|
base: buildMarkdown(this.mdExtensions),
|
2022-10-10 12:50:21 +00:00
|
|
|
|
codeLanguages: [
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "yaml",
|
2023-01-21 12:44:11 +00:00
|
|
|
|
alias: ["meta", "data", "embed"],
|
2022-10-10 12:50:21 +00:00
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(yamlLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "javascript",
|
|
|
|
|
alias: ["js"],
|
|
|
|
|
support: new LanguageSupport(javascriptLanguage),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "typescript",
|
|
|
|
|
alias: ["ts"],
|
|
|
|
|
support: new LanguageSupport(typescriptLanguage),
|
|
|
|
|
}),
|
2023-01-06 17:17:39 +00:00
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "sql",
|
|
|
|
|
alias: ["sql"],
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(sqlLanguage)),
|
|
|
|
|
}),
|
2023-01-22 17:45:01 +00:00
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "postgresql",
|
|
|
|
|
alias: ["pgsql", "postgres"],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(postgresqlLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "rust",
|
|
|
|
|
alias: ["rs"],
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(rustLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "css",
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(sqlLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "python",
|
|
|
|
|
alias: ["py"],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(pythonLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "protobuf",
|
|
|
|
|
alias: ["proto"],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(protobufLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "shell",
|
|
|
|
|
alias: ["sh", "bash", "zsh", "fish"],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(shellLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "swift",
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(rustLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "toml",
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(tomlLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "json",
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(jsonLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "xml",
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(xmlLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "c",
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(cLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "cpp",
|
|
|
|
|
alias: ["c++", "cxx"],
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(cppLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "java",
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(javaLanguage)),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "csharp",
|
|
|
|
|
alias: ["c#", "cs"],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(csharpLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "scala",
|
|
|
|
|
alias: ["sc"],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(scalaLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "kotlin",
|
|
|
|
|
alias: ["kt", "kts"],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(kotlinLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "objc",
|
|
|
|
|
alias: ["objective-c", "objectivec"],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(objectiveCLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "objcpp",
|
|
|
|
|
alias: [
|
|
|
|
|
"objc++",
|
|
|
|
|
"objective-cpp",
|
|
|
|
|
"objectivecpp",
|
|
|
|
|
"objective-c++",
|
|
|
|
|
"objectivec++",
|
|
|
|
|
],
|
|
|
|
|
support: new LanguageSupport(
|
|
|
|
|
StreamLanguage.define(objectiveCppLanguage),
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
LanguageDescription.of({
|
|
|
|
|
name: "dart",
|
|
|
|
|
support: new LanguageSupport(StreamLanguage.define(dartLanguage)),
|
|
|
|
|
}),
|
2022-10-10 12:50:21 +00:00
|
|
|
|
],
|
|
|
|
|
addKeymap: true,
|
2022-06-14 07:45:22 +00:00
|
|
|
|
}),
|
2022-06-13 16:31:36 +00:00
|
|
|
|
syntaxHighlighting(customMarkdownStyle(this.mdExtensions)),
|
2022-03-20 08:56:28 +00:00
|
|
|
|
autocompletion({
|
|
|
|
|
override: [
|
2022-12-21 13:55:24 +00:00
|
|
|
|
this.editorComplete.bind(this),
|
2022-03-29 09:21:32 +00:00
|
|
|
|
this.slashCommandHook.slashCommandCompleter.bind(
|
2022-10-10 12:50:21 +00:00
|
|
|
|
this.slashCommandHook,
|
2022-03-29 09:21:32 +00:00
|
|
|
|
),
|
2022-03-20 08:56:28 +00:00
|
|
|
|
],
|
|
|
|
|
}),
|
2023-01-08 11:24:12 +00:00
|
|
|
|
inlineImagesPlugin(this.space),
|
2022-09-13 06:41:01 +00:00
|
|
|
|
highlightSpecialChars(),
|
|
|
|
|
history(),
|
|
|
|
|
drawSelection(),
|
|
|
|
|
dropCursor(),
|
2022-10-10 12:50:21 +00:00
|
|
|
|
indentOnInput(),
|
2022-11-18 15:04:37 +00:00
|
|
|
|
...cleanModePlugins(this),
|
2022-03-20 08:56:28 +00:00
|
|
|
|
EditorView.lineWrapping,
|
|
|
|
|
lineWrapper([
|
2022-08-02 12:40:04 +00:00
|
|
|
|
{ selector: "ATXHeading1", class: "sb-line-h1" },
|
|
|
|
|
{ selector: "ATXHeading2", class: "sb-line-h2" },
|
|
|
|
|
{ selector: "ATXHeading3", class: "sb-line-h3" },
|
2022-11-18 15:04:37 +00:00
|
|
|
|
{ selector: "ATXHeading4", class: "sb-line-h4" },
|
2022-08-02 12:40:04 +00:00
|
|
|
|
{ selector: "ListItem", class: "sb-line-li", nesting: true },
|
|
|
|
|
{ selector: "Blockquote", class: "sb-line-blockquote" },
|
|
|
|
|
{ selector: "Task", class: "sb-line-task" },
|
|
|
|
|
{ selector: "CodeBlock", class: "sb-line-code" },
|
|
|
|
|
{ selector: "FencedCode", class: "sb-line-fenced-code" },
|
|
|
|
|
{ selector: "Comment", class: "sb-line-comment" },
|
|
|
|
|
{ selector: "BulletList", class: "sb-line-ul" },
|
|
|
|
|
{ selector: "OrderedList", class: "sb-line-ol" },
|
|
|
|
|
{ selector: "TableHeader", class: "sb-line-tbl-header" },
|
2022-10-17 13:48:21 +00:00
|
|
|
|
{ selector: "FrontMatter", class: "sb-frontmatter" },
|
2022-03-20 08:56:28 +00:00
|
|
|
|
]),
|
|
|
|
|
keymap.of([
|
|
|
|
|
...smartQuoteKeymap,
|
|
|
|
|
...closeBracketsKeymap,
|
|
|
|
|
...standardKeymap,
|
|
|
|
|
...searchKeymap,
|
|
|
|
|
...historyKeymap,
|
|
|
|
|
...completionKeymap,
|
2022-10-25 16:50:07 +00:00
|
|
|
|
...(this.collabState ? yUndoManagerKeymap : []),
|
2022-03-20 08:56:28 +00:00
|
|
|
|
indentWithTab,
|
|
|
|
|
...commandKeyBindings,
|
|
|
|
|
{
|
|
|
|
|
key: "Ctrl-k",
|
|
|
|
|
mac: "Cmd-k",
|
|
|
|
|
run: (): boolean => {
|
|
|
|
|
this.viewDispatch({ type: "start-navigate" });
|
2022-04-26 17:04:36 +00:00
|
|
|
|
this.space.updatePageList();
|
2022-03-20 08:56:28 +00:00
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-04-09 20:43:46 +00:00
|
|
|
|
key: "Ctrl-/",
|
|
|
|
|
mac: "Cmd-/",
|
2022-03-20 08:56:28 +00:00
|
|
|
|
run: (): boolean => {
|
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "show-palette",
|
2022-10-10 12:50:21 +00:00
|
|
|
|
context: this.getContext(),
|
2022-03-20 08:56:28 +00:00
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-05-08 07:59:29 +00:00
|
|
|
|
{
|
|
|
|
|
key: "Ctrl-.",
|
|
|
|
|
mac: "Cmd-.",
|
|
|
|
|
run: (): boolean => {
|
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "show-palette",
|
|
|
|
|
context: this.getContext(),
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-03-20 08:56:28 +00:00
|
|
|
|
]),
|
|
|
|
|
EditorView.domEventHandlers({
|
2023-01-08 11:24:12 +00:00
|
|
|
|
// This may result in duplicated touch events on mobile devices
|
|
|
|
|
touchmove: (event: TouchEvent, view: EditorView) => {
|
|
|
|
|
touchCount++;
|
|
|
|
|
},
|
|
|
|
|
touchend: (event: TouchEvent, view: EditorView) => {
|
|
|
|
|
if (touchCount === 0) {
|
|
|
|
|
safeRun(async () => {
|
|
|
|
|
const touch = event.changedTouches.item(0)!;
|
|
|
|
|
const clickEvent: ClickEvent = {
|
|
|
|
|
page: pageName,
|
|
|
|
|
ctrlKey: event.ctrlKey,
|
|
|
|
|
metaKey: event.metaKey,
|
|
|
|
|
altKey: event.altKey,
|
|
|
|
|
pos: view.posAtCoords({
|
|
|
|
|
x: touch.clientX,
|
|
|
|
|
y: touch.clientY,
|
|
|
|
|
})!,
|
|
|
|
|
};
|
|
|
|
|
await this.dispatchAppEvent("page:click", clickEvent);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
touchCount = 0;
|
|
|
|
|
},
|
2023-01-13 15:33:36 +00:00
|
|
|
|
mousedown: (event: MouseEvent, view: EditorView) => {
|
|
|
|
|
// Make sure <a> tags are clicked without moving the cursor there
|
|
|
|
|
if (!event.altKey && event.target instanceof Element) {
|
|
|
|
|
const parentA = event.target.closest("a");
|
|
|
|
|
if (parentA) {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const clickEvent: ClickEvent = {
|
|
|
|
|
page: pageName,
|
|
|
|
|
ctrlKey: event.ctrlKey,
|
|
|
|
|
metaKey: event.metaKey,
|
|
|
|
|
altKey: event.altKey,
|
|
|
|
|
pos: view.posAtCoords({
|
|
|
|
|
x: event.x,
|
|
|
|
|
y: event.y,
|
|
|
|
|
})!,
|
|
|
|
|
};
|
|
|
|
|
this.dispatchAppEvent("page:click", clickEvent).catch(
|
|
|
|
|
console.error,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-03-20 08:56:28 +00:00
|
|
|
|
click: (event: MouseEvent, view: EditorView) => {
|
|
|
|
|
safeRun(async () => {
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const clickEvent: ClickEvent = {
|
2022-03-28 13:25:05 +00:00
|
|
|
|
page: pageName,
|
2022-03-20 08:56:28 +00:00
|
|
|
|
ctrlKey: event.ctrlKey,
|
|
|
|
|
metaKey: event.metaKey,
|
|
|
|
|
altKey: event.altKey,
|
|
|
|
|
pos: view.posAtCoords(event)!,
|
|
|
|
|
};
|
|
|
|
|
await this.dispatchAppEvent("page:click", clickEvent);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
}),
|
2022-03-31 12:28:07 +00:00
|
|
|
|
ViewPlugin.fromClass(
|
|
|
|
|
class {
|
|
|
|
|
update(update: ViewUpdate): void {
|
|
|
|
|
if (update.docChanged) {
|
|
|
|
|
editor.viewDispatch({ type: "page-changed" });
|
2022-04-04 13:25:07 +00:00
|
|
|
|
editor.debouncedUpdateEvent();
|
2022-04-19 14:54:47 +00:00
|
|
|
|
editor.save().catch((e) => console.error("Error saving", e));
|
2022-03-31 12:28:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
|
},
|
2022-03-31 12:28:07 +00:00
|
|
|
|
),
|
2022-03-30 13:16:22 +00:00
|
|
|
|
pasteLinkExtension,
|
2022-09-05 11:11:03 +00:00
|
|
|
|
attachmentExtension(this),
|
2022-06-14 07:45:22 +00:00
|
|
|
|
closeBrackets(),
|
2022-10-25 16:50:07 +00:00
|
|
|
|
...[this.collabState ? this.collabState.collabExtension() : []],
|
2022-03-20 08:56:28 +00:00
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 17:04:36 +00:00
|
|
|
|
async reloadPlugs() {
|
2022-06-15 15:54:11 +00:00
|
|
|
|
console.log("Loading plugs");
|
2022-04-26 17:04:36 +00:00
|
|
|
|
await this.space.updatePageList();
|
|
|
|
|
await this.system.unloadAll();
|
|
|
|
|
console.log("(Re)loading plugs");
|
2022-10-21 14:48:48 +00:00
|
|
|
|
await Promise.all((await this.space.listPlugs()).map(async (plugName) => {
|
2023-05-23 18:53:53 +00:00
|
|
|
|
try {
|
|
|
|
|
await this.system.load(
|
|
|
|
|
new URL(`/.fs/${plugName}`, location.href),
|
|
|
|
|
createSandbox,
|
|
|
|
|
);
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
console.error("Could not load plug", plugName, "error:", e.message);
|
|
|
|
|
}
|
2022-10-21 14:48:48 +00:00
|
|
|
|
}));
|
2022-04-26 17:04:36 +00:00
|
|
|
|
this.rebuildEditorState();
|
2022-07-11 07:08:22 +00:00
|
|
|
|
await this.dispatchAppEvent("plugs:loaded");
|
2022-04-26 17:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-31 15:25:34 +00:00
|
|
|
|
rebuildEditorState() {
|
|
|
|
|
const editorView = this.editorView;
|
2022-06-14 14:55:50 +00:00
|
|
|
|
console.log("Rebuilding editor state");
|
2022-04-11 18:34:09 +00:00
|
|
|
|
|
2022-11-25 12:05:41 +00:00
|
|
|
|
// Load all syntax extensions
|
|
|
|
|
this.mdExtensions = loadMarkdownExtensions(this.system);
|
|
|
|
|
// And reload the syscalls to use the new syntax extensions
|
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
|
[],
|
|
|
|
|
markdownSyscalls(buildMarkdown(this.mdExtensions)),
|
|
|
|
|
);
|
2022-04-11 18:34:09 +00:00
|
|
|
|
|
2022-11-25 12:05:41 +00:00
|
|
|
|
if (editorView && this.currentPage) {
|
|
|
|
|
// And update the editor if a page is loaded
|
2022-09-06 14:33:00 +00:00
|
|
|
|
this.saveState(this.currentPage);
|
2022-04-26 17:04:36 +00:00
|
|
|
|
|
2022-03-31 15:25:34 +00:00
|
|
|
|
editorView.setState(
|
2023-01-16 10:28:59 +00:00
|
|
|
|
this.createEditorState(
|
|
|
|
|
this.currentPage,
|
|
|
|
|
editorView.state.sliceDoc(),
|
|
|
|
|
this.viewState.currentPageMeta?.perm === "ro",
|
|
|
|
|
),
|
2022-03-31 15:25:34 +00:00
|
|
|
|
);
|
2022-04-20 08:56:43 +00:00
|
|
|
|
if (editorView.contentDOM) {
|
2022-05-17 09:53:17 +00:00
|
|
|
|
this.tweakEditorDOM(
|
|
|
|
|
editorView.contentDOM,
|
|
|
|
|
);
|
2022-04-20 08:56:43 +00:00
|
|
|
|
}
|
2022-04-26 17:04:36 +00:00
|
|
|
|
|
|
|
|
|
this.restoreState(this.currentPage);
|
2022-03-31 15:25:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 13:55:24 +00:00
|
|
|
|
// Code completion support
|
|
|
|
|
private async completeWithEvent(
|
|
|
|
|
context: CompletionContext,
|
|
|
|
|
eventName: AppEvent,
|
|
|
|
|
): Promise<CompletionResult | null> {
|
|
|
|
|
const editorState = context.state;
|
|
|
|
|
const selection = editorState.selection.main;
|
|
|
|
|
const line = editorState.doc.lineAt(selection.from);
|
|
|
|
|
const linePrefix = line.text.slice(0, selection.from - line.from);
|
|
|
|
|
|
|
|
|
|
const results = await this.dispatchAppEvent(eventName, {
|
|
|
|
|
linePrefix,
|
|
|
|
|
pos: selection.from,
|
|
|
|
|
} as CompleteEvent);
|
2022-04-19 14:54:47 +00:00
|
|
|
|
let actualResult = null;
|
|
|
|
|
for (const result of results) {
|
|
|
|
|
if (result) {
|
|
|
|
|
if (actualResult) {
|
|
|
|
|
console.error(
|
2022-10-10 12:50:21 +00:00
|
|
|
|
"Got completion results from multiple sources, cannot deal with that",
|
2022-04-19 14:54:47 +00:00
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
actualResult = result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return actualResult;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 13:55:24 +00:00
|
|
|
|
editorComplete(
|
|
|
|
|
context: CompletionContext,
|
|
|
|
|
): Promise<CompletionResult | null> {
|
|
|
|
|
return this.completeWithEvent(context, "editor:complete");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
miniEditorComplete(
|
|
|
|
|
context: CompletionContext,
|
|
|
|
|
): Promise<CompletionResult | null> {
|
|
|
|
|
return this.completeWithEvent(context, "minieditor:complete");
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 16:19:08 +00:00
|
|
|
|
async reloadPage() {
|
2022-03-20 08:56:28 +00:00
|
|
|
|
console.log("Reloading page");
|
2022-10-10 16:19:08 +00:00
|
|
|
|
clearTimeout(this.saveTimeout);
|
|
|
|
|
await this.loadPage(this.currentPage!);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
|
this.editorView!.focus();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 07:23:12 +00:00
|
|
|
|
async navigate(
|
|
|
|
|
name: string,
|
|
|
|
|
pos?: number | string,
|
|
|
|
|
replaceState = false,
|
|
|
|
|
newWindow = false,
|
|
|
|
|
) {
|
2022-08-02 10:43:39 +00:00
|
|
|
|
if (!name) {
|
2023-05-23 18:53:53 +00:00
|
|
|
|
name = this.settings!.indexPage;
|
2022-08-02 10:43:39 +00:00
|
|
|
|
}
|
2022-10-29 07:23:12 +00:00
|
|
|
|
|
|
|
|
|
if (newWindow) {
|
|
|
|
|
const win = window.open(`${location.origin}/${name}`, "_blank");
|
|
|
|
|
if (win) {
|
|
|
|
|
win.focus();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
|
await this.pageNavigator!.navigate(name, pos, replaceState);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 14:33:00 +00:00
|
|
|
|
async loadPage(pageName: string): Promise<boolean> {
|
2022-05-17 13:54:55 +00:00
|
|
|
|
const loadingDifferentPage = pageName !== this.currentPage;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
const editorView = this.editorView;
|
|
|
|
|
if (!editorView) {
|
2022-09-06 14:33:00 +00:00
|
|
|
|
return false;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 14:33:00 +00:00
|
|
|
|
const previousPage = this.currentPage;
|
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
|
// Persist current page state and nicely close page
|
2022-09-06 14:33:00 +00:00
|
|
|
|
if (previousPage) {
|
|
|
|
|
this.saveState(previousPage);
|
|
|
|
|
this.space.unwatchPage(previousPage);
|
2022-10-10 16:19:08 +00:00
|
|
|
|
if (previousPage !== pageName) {
|
|
|
|
|
await this.save(true);
|
2022-10-25 16:50:07 +00:00
|
|
|
|
// And stop the collab session
|
|
|
|
|
if (this.collabState) {
|
|
|
|
|
this.collabState.stop();
|
|
|
|
|
this.collabState = undefined;
|
|
|
|
|
}
|
2022-10-10 16:19:08 +00:00
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "page-loading",
|
|
|
|
|
name: pageName,
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
|
// Fetch next page to open
|
2022-04-06 13:39:20 +00:00
|
|
|
|
let doc;
|
|
|
|
|
try {
|
|
|
|
|
doc = await this.space.readPage(pageName);
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
// Not found, new page
|
|
|
|
|
console.log("Creating new page", pageName);
|
|
|
|
|
doc = {
|
|
|
|
|
text: "",
|
2022-05-17 09:53:17 +00:00
|
|
|
|
meta: { name: pageName, lastModified: 0, perm: "rw" } as PageMeta,
|
2022-04-06 13:39:20 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 10:28:59 +00:00
|
|
|
|
const editorState = this.createEditorState(
|
|
|
|
|
pageName,
|
|
|
|
|
doc.text,
|
|
|
|
|
doc.meta.perm === "ro",
|
|
|
|
|
);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
editorView.setState(editorState);
|
2022-04-20 08:56:43 +00:00
|
|
|
|
if (editorView.contentDOM) {
|
2023-01-16 10:28:59 +00:00
|
|
|
|
this.tweakEditorDOM(editorView.contentDOM);
|
2022-04-20 08:56:43 +00:00
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const stateRestored = this.restoreState(pageName);
|
2022-03-31 12:28:07 +00:00
|
|
|
|
this.space.watchPage(pageName);
|
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
|
this.viewDispatch({
|
|
|
|
|
type: "page-loaded",
|
2022-05-17 09:53:17 +00:00
|
|
|
|
meta: doc.meta,
|
2022-03-20 08:56:28 +00:00
|
|
|
|
});
|
2022-04-04 13:25:07 +00:00
|
|
|
|
|
2022-11-24 15:55:30 +00:00
|
|
|
|
// Note: these events are dispatched asynchronously deliberately (not waiting for results)
|
2022-05-17 13:54:55 +00:00
|
|
|
|
if (loadingDifferentPage) {
|
2022-11-24 15:55:30 +00:00
|
|
|
|
this.eventHook.dispatchEvent("editor:pageLoaded", pageName).catch(
|
|
|
|
|
console.error,
|
|
|
|
|
);
|
2022-07-18 14:48:36 +00:00
|
|
|
|
} else {
|
2022-11-24 15:55:30 +00:00
|
|
|
|
this.eventHook.dispatchEvent("editor:pageReloaded", pageName).catch(
|
|
|
|
|
console.error,
|
|
|
|
|
);
|
2022-05-17 13:54:55 +00:00
|
|
|
|
}
|
2022-09-06 14:33:00 +00:00
|
|
|
|
|
|
|
|
|
return stateRestored;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 10:28:59 +00:00
|
|
|
|
tweakEditorDOM(contentDOM: HTMLElement) {
|
2022-05-09 08:45:36 +00:00
|
|
|
|
contentDOM.spellcheck = true;
|
|
|
|
|
contentDOM.setAttribute("autocorrect", "on");
|
|
|
|
|
contentDOM.setAttribute("autocapitalize", "on");
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 09:07:20 +00:00
|
|
|
|
async loadCustomStyles() {
|
|
|
|
|
try {
|
|
|
|
|
const { text: stylesText } = await this.space.readPage("STYLES");
|
|
|
|
|
const cssBlockRegex = /```css([^`]+)```/;
|
|
|
|
|
const match = cssBlockRegex.exec(stylesText);
|
|
|
|
|
if (!match) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const css = match[1];
|
|
|
|
|
document.getElementById("custom-styles")!.innerHTML = css;
|
|
|
|
|
} catch {
|
|
|
|
|
// Nuthin'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 14:33:00 +00:00
|
|
|
|
private restoreState(pageName: string): boolean {
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const pageState = this.openPages.get(pageName);
|
2022-04-26 17:04:36 +00:00
|
|
|
|
const editorView = this.editorView!;
|
|
|
|
|
if (pageState) {
|
|
|
|
|
// Restore state
|
2022-07-18 10:10:31 +00:00
|
|
|
|
editorView.scrollDOM.scrollTop = pageState!.scrollTop;
|
2022-04-26 17:04:36 +00:00
|
|
|
|
editorView.dispatch({
|
|
|
|
|
selection: pageState.selection,
|
2022-07-06 10:18:33 +00:00
|
|
|
|
scrollIntoView: true,
|
2022-04-26 17:04:36 +00:00
|
|
|
|
});
|
2022-07-18 10:10:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
editorView.scrollDOM.scrollTop = 0;
|
2022-09-06 09:03:37 +00:00
|
|
|
|
editorView.dispatch({
|
|
|
|
|
selection: { anchor: 0 },
|
|
|
|
|
scrollIntoView: true,
|
|
|
|
|
});
|
2022-04-26 17:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
editorView.focus();
|
2022-09-06 14:33:00 +00:00
|
|
|
|
return !!pageState;
|
2022-04-26 17:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 14:33:00 +00:00
|
|
|
|
private saveState(currentPage: string) {
|
2022-04-26 17:04:36 +00:00
|
|
|
|
this.openPages.set(
|
2022-09-06 14:33:00 +00:00
|
|
|
|
currentPage,
|
2022-04-26 17:04:36 +00:00
|
|
|
|
new PageState(
|
|
|
|
|
this.editorView!.scrollDOM.scrollTop,
|
2022-10-10 12:50:21 +00:00
|
|
|
|
this.editorView!.state.selection,
|
|
|
|
|
),
|
2022-04-26 17:04:36 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
|
ViewComponent() {
|
2022-03-20 08:56:28 +00:00
|
|
|
|
const [viewState, dispatch] = useReducer(reducer, initialViewState);
|
|
|
|
|
this.viewState = viewState;
|
|
|
|
|
this.viewDispatch = dispatch;
|
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
|
// deno-lint-ignore no-this-alias
|
2022-06-23 15:08:42 +00:00
|
|
|
|
const editor = this;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (viewState.currentPage) {
|
|
|
|
|
document.title = viewState.currentPage;
|
|
|
|
|
}
|
|
|
|
|
}, [viewState.currentPage]);
|
|
|
|
|
|
2022-09-16 12:26:47 +00:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (editor.editorView) {
|
|
|
|
|
editor.tweakEditorDOM(
|
|
|
|
|
editor.editorView.contentDOM,
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-12-21 13:55:24 +00:00
|
|
|
|
}, [viewState.uiOptions.forcedROMode]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
this.rebuildEditorState();
|
2023-01-23 17:52:17 +00:00
|
|
|
|
this.dispatchAppEvent("editor:modeswitch");
|
2022-12-21 13:55:24 +00:00
|
|
|
|
}, [viewState.uiOptions.vimMode]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
document.documentElement.dataset.theme = viewState.uiOptions.darkMode
|
|
|
|
|
? "dark"
|
|
|
|
|
: "light";
|
|
|
|
|
}, [viewState.uiOptions.darkMode]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// Need to dispatch a resize event so that the top_bar can pick it up
|
|
|
|
|
globalThis.dispatchEvent(new Event("resize"));
|
|
|
|
|
}, [viewState.panels]);
|
2022-09-16 12:26:47 +00:00
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
|
return (
|
2022-04-04 16:33:13 +00:00
|
|
|
|
<>
|
2022-03-20 08:56:28 +00:00
|
|
|
|
{viewState.showPageNavigator && (
|
|
|
|
|
<PageNavigator
|
|
|
|
|
allPages={viewState.allPages}
|
|
|
|
|
currentPage={this.currentPage}
|
2022-12-21 13:55:24 +00:00
|
|
|
|
completer={this.miniEditorComplete.bind(this)}
|
|
|
|
|
vimMode={viewState.uiOptions.vimMode}
|
|
|
|
|
darkMode={viewState.uiOptions.darkMode}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
onNavigate={(page) => {
|
|
|
|
|
dispatch({ type: "stop-navigate" });
|
2023-05-23 18:53:53 +00:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
editor.focus();
|
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
|
if (page) {
|
|
|
|
|
safeRun(async () => {
|
2022-03-29 09:21:32 +00:00
|
|
|
|
await editor.navigate(page);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{viewState.showCommandPalette && (
|
|
|
|
|
<CommandPalette
|
|
|
|
|
onTrigger={(cmd) => {
|
|
|
|
|
dispatch({ type: "hide-palette" });
|
2023-05-23 18:53:53 +00:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
editor.focus();
|
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
|
if (cmd) {
|
2022-05-16 13:09:36 +00:00
|
|
|
|
dispatch({ type: "command-run", command: cmd.command.name });
|
2022-07-04 13:07:27 +00:00
|
|
|
|
cmd
|
|
|
|
|
.run()
|
2022-10-10 12:50:21 +00:00
|
|
|
|
.catch((e: any) => {
|
2022-07-04 13:07:27 +00:00
|
|
|
|
console.error("Error running command", e.message);
|
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
// Always be focusing the editor after running a command
|
|
|
|
|
editor.focus();
|
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
}}
|
2023-01-04 15:37:09 +00:00
|
|
|
|
commands={this.getCommandsByContext(viewState)}
|
2022-12-21 13:55:24 +00:00
|
|
|
|
vimMode={viewState.uiOptions.vimMode}
|
|
|
|
|
darkMode={viewState.uiOptions.darkMode}
|
|
|
|
|
completer={this.miniEditorComplete.bind(this)}
|
2022-05-16 13:09:36 +00:00
|
|
|
|
recentCommands={viewState.recentCommands}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
/>
|
|
|
|
|
)}
|
2022-04-13 12:46:52 +00:00
|
|
|
|
{viewState.showFilterBox && (
|
|
|
|
|
<FilterList
|
2022-04-21 09:46:33 +00:00
|
|
|
|
label={viewState.filterBoxLabel}
|
2022-04-13 12:46:52 +00:00
|
|
|
|
placeholder={viewState.filterBoxPlaceHolder}
|
|
|
|
|
options={viewState.filterBoxOptions}
|
2022-12-21 13:55:24 +00:00
|
|
|
|
vimMode={viewState.uiOptions.vimMode}
|
|
|
|
|
darkMode={viewState.uiOptions.darkMode}
|
2022-04-13 12:46:52 +00:00
|
|
|
|
allowNew={false}
|
2022-12-21 13:55:24 +00:00
|
|
|
|
completer={this.miniEditorComplete.bind(this)}
|
2022-04-13 12:46:52 +00:00
|
|
|
|
helpText={viewState.filterBoxHelpText}
|
|
|
|
|
onSelect={viewState.filterBoxOnSelect}
|
|
|
|
|
/>
|
2022-12-21 15:08:51 +00:00
|
|
|
|
)}
|
|
|
|
|
{viewState.showPrompt && (
|
|
|
|
|
<Prompt
|
|
|
|
|
message={viewState.promptMessage!}
|
|
|
|
|
defaultValue={viewState.promptDefaultValue}
|
|
|
|
|
vimMode={viewState.uiOptions.vimMode}
|
|
|
|
|
darkMode={viewState.uiOptions.darkMode}
|
|
|
|
|
completer={this.miniEditorComplete.bind(this)}
|
|
|
|
|
callback={(value) => {
|
|
|
|
|
dispatch({ type: "hide-prompt" });
|
|
|
|
|
viewState.promptCallback!(value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{viewState.showConfirm && (
|
|
|
|
|
<Confirm
|
|
|
|
|
message={viewState.confirmMessage!}
|
|
|
|
|
callback={(value) => {
|
|
|
|
|
dispatch({ type: "hide-confirm" });
|
|
|
|
|
viewState.confirmCallback!(value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2022-04-13 12:46:52 +00:00
|
|
|
|
)}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
<TopBar
|
|
|
|
|
pageName={viewState.currentPage}
|
|
|
|
|
notifications={viewState.notifications}
|
2023-05-23 18:53:53 +00:00
|
|
|
|
synced={viewState.synced}
|
2022-03-31 12:28:07 +00:00
|
|
|
|
unsavedChanges={viewState.unsavedChanges}
|
2022-09-06 14:21:33 +00:00
|
|
|
|
isLoading={viewState.isLoading}
|
2022-12-21 13:55:24 +00:00
|
|
|
|
vimMode={viewState.uiOptions.vimMode}
|
|
|
|
|
darkMode={viewState.uiOptions.darkMode}
|
|
|
|
|
completer={editor.miniEditorComplete.bind(editor)}
|
|
|
|
|
onRename={async (newName) => {
|
2022-11-24 16:09:05 +00:00
|
|
|
|
if (!newName) {
|
2022-12-21 13:55:24 +00:00
|
|
|
|
// Always move cursor to the start of the page
|
|
|
|
|
editor.editorView?.dispatch({
|
|
|
|
|
selection: { anchor: 0 },
|
|
|
|
|
});
|
|
|
|
|
editor.focus();
|
|
|
|
|
return;
|
2022-11-24 16:09:05 +00:00
|
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
|
console.log("Now renaming page to...", newName);
|
2022-12-21 13:55:24 +00:00
|
|
|
|
await editor.system.loadedPlugs.get("core")!.invoke(
|
2022-11-18 15:04:37 +00:00
|
|
|
|
"renamePage",
|
2022-12-13 08:49:31 +00:00
|
|
|
|
[{ page: newName }],
|
2022-12-21 13:55:24 +00:00
|
|
|
|
);
|
|
|
|
|
editor.focus();
|
2022-06-20 16:30:45 +00:00
|
|
|
|
}}
|
2022-11-18 15:04:37 +00:00
|
|
|
|
actionButtons={[
|
|
|
|
|
{
|
2022-12-08 16:04:07 +00:00
|
|
|
|
icon: HomeIcon,
|
2022-11-18 15:04:37 +00:00
|
|
|
|
description: `Go home (Alt-h)`,
|
|
|
|
|
callback: () => {
|
|
|
|
|
editor.navigate("");
|
|
|
|
|
},
|
2023-06-10 14:15:16 +00:00
|
|
|
|
href: "",
|
2022-11-18 15:04:37 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2022-12-08 16:04:07 +00:00
|
|
|
|
icon: BookIcon,
|
2022-11-18 15:04:37 +00:00
|
|
|
|
description: `Open page (${isMacLike() ? "Cmd-k" : "Ctrl-k"})`,
|
|
|
|
|
callback: () => {
|
|
|
|
|
dispatch({ type: "start-navigate" });
|
2023-01-08 11:24:12 +00:00
|
|
|
|
this.space.updatePageList();
|
2022-11-18 15:04:37 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-12-08 16:04:07 +00:00
|
|
|
|
icon: TerminalIcon,
|
2022-11-18 15:04:37 +00:00
|
|
|
|
description: `Run command (${isMacLike() ? "Cmd-/" : "Ctrl-/"})`,
|
|
|
|
|
callback: () => {
|
2022-12-16 15:35:05 +00:00
|
|
|
|
dispatch({ type: "show-palette", context: this.getContext() });
|
2022-11-18 15:04:37 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]}
|
2022-10-10 12:50:21 +00:00
|
|
|
|
rhs={!!viewState.panels.rhs.mode && (
|
|
|
|
|
<div
|
|
|
|
|
className="panel"
|
|
|
|
|
style={{ flex: viewState.panels.rhs.mode }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
lhs={!!viewState.panels.lhs.mode && (
|
|
|
|
|
<div
|
|
|
|
|
className="panel"
|
|
|
|
|
style={{ flex: viewState.panels.lhs.mode }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
/>
|
2022-07-22 11:44:28 +00:00
|
|
|
|
<div id="sb-main">
|
2022-09-30 14:59:57 +00:00
|
|
|
|
{!!viewState.panels.lhs.mode && (
|
|
|
|
|
<Panel config={viewState.panels.lhs} editor={editor} />
|
2022-04-04 16:33:13 +00:00
|
|
|
|
)}
|
2022-07-22 11:44:28 +00:00
|
|
|
|
<div id="sb-editor" />
|
2022-09-30 14:59:57 +00:00
|
|
|
|
{!!viewState.panels.rhs.mode && (
|
|
|
|
|
<Panel config={viewState.panels.rhs} editor={editor} />
|
2022-04-04 16:33:13 +00:00
|
|
|
|
)}
|
|
|
|
|
</div>
|
2022-09-30 14:59:57 +00:00
|
|
|
|
{!!viewState.panels.modal.mode && (
|
|
|
|
|
<div
|
|
|
|
|
className="sb-modal"
|
|
|
|
|
style={{ inset: `${viewState.panels.modal.mode}px` }}
|
|
|
|
|
>
|
|
|
|
|
<Panel config={viewState.panels.modal} editor={editor} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{!!viewState.panels.bhs.mode && (
|
2022-08-02 12:40:04 +00:00
|
|
|
|
<div className="sb-bhs">
|
2022-09-30 14:59:57 +00:00
|
|
|
|
<Panel config={viewState.panels.bhs} editor={editor} />
|
2022-04-26 17:04:36 +00:00
|
|
|
|
</div>
|
|
|
|
|
)}
|
2022-04-04 16:33:13 +00:00
|
|
|
|
</>
|
2022-03-20 08:56:28 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 15:35:05 +00:00
|
|
|
|
async runCommandByName(name: string, ...args: any[]) {
|
2022-07-11 07:08:22 +00:00
|
|
|
|
const cmd = this.viewState.commands.get(name);
|
|
|
|
|
if (cmd) {
|
|
|
|
|
await cmd.run();
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`Command ${name} not found`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
|
render(container: Element) {
|
2022-03-20 08:56:28 +00:00
|
|
|
|
const ViewComponent = this.ViewComponent.bind(this);
|
2022-10-10 12:50:21 +00:00
|
|
|
|
preactRender(<ViewComponent />, container);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
|
|
2023-01-04 15:37:09 +00:00
|
|
|
|
private getCommandsByContext(
|
|
|
|
|
state: AppViewState,
|
|
|
|
|
): Map<string, AppCommand> {
|
|
|
|
|
const commands = new Map(state.commands);
|
|
|
|
|
for (const [k, v] of state.commands.entries()) {
|
|
|
|
|
if (
|
|
|
|
|
v.command.contexts &&
|
|
|
|
|
(!state.showCommandPaletteContext ||
|
|
|
|
|
!v.command.contexts.includes(state.showCommandPaletteContext))
|
|
|
|
|
) {
|
|
|
|
|
commands.delete(k);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return commands;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 09:46:33 +00:00
|
|
|
|
private getContext(): string | undefined {
|
2022-10-10 12:50:21 +00:00
|
|
|
|
const state = this.editorView!.state;
|
|
|
|
|
const selection = state.selection.main;
|
2022-04-21 09:46:33 +00:00
|
|
|
|
if (selection.empty) {
|
2022-12-16 15:35:05 +00:00
|
|
|
|
return syntaxTree(state).resolveInner(selection.from).type.name;
|
2022-04-21 09:46:33 +00:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-25 16:50:07 +00:00
|
|
|
|
|
|
|
|
|
startCollab(serverUrl: string, token: string, username: string) {
|
|
|
|
|
if (this.collabState) {
|
|
|
|
|
// Clean up old collab state
|
|
|
|
|
this.collabState.stop();
|
|
|
|
|
}
|
|
|
|
|
const initialText = this.editorView!.state.sliceDoc();
|
|
|
|
|
this.collabState = new CollabState(serverUrl, token, username);
|
|
|
|
|
this.collabState.collabProvider.once("sync", (synced: boolean) => {
|
|
|
|
|
if (this.collabState?.ytext.toString() === "") {
|
|
|
|
|
console.log("Synced value is empty, putting back original text");
|
|
|
|
|
this.collabState?.ytext.insert(0, initialText);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.rebuildEditorState();
|
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
}
|