Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
111 lines
5.1 KiB
JavaScript
111 lines
5.1 KiB
JavaScript
import {VimWasm, checkBrowserCompatibility} from "./vendor/vimwasm.js"
|
|
|
|
const errorBox = document.getElementById("error")
|
|
const fail = error => {
|
|
errorBox.hidden = false
|
|
errorBox.textContent = error instanceof Error ? error.message : String(error)
|
|
}
|
|
|
|
const compatibilityError = checkBrowserCompatibility()
|
|
if (compatibilityError || !crossOriginIsolated) {
|
|
fail(compatibilityError || "Cross-origin isolation is not active. Check COOP/COEP headers.")
|
|
} else {
|
|
try {
|
|
const response = await fetch("./api/session", {cache: "no-store", credentials: "same-origin"})
|
|
if (!response.ok) throw new Error(`Workspace request failed: HTTP ${response.status}`)
|
|
const workspace = await response.json()
|
|
const files = {}
|
|
const directories = new Set(["/work"])
|
|
for (const file of workspace.files || []) {
|
|
const path = `/work/${file.path}`
|
|
files[path] = file.content
|
|
const parts = path.split("/").slice(1, -1)
|
|
let directory = ""
|
|
for (const part of parts) {
|
|
directory += `/${part}`
|
|
directories.add(directory)
|
|
}
|
|
}
|
|
const vimrcResponse = await fetch("./vimrc", {cache: "no-store"})
|
|
if (!vimrcResponse.ok) throw new Error(`vimrc request failed: HTTP ${vimrcResponse.status}`)
|
|
// The pinned runtime creates files with wx+ and silently skips the
|
|
// bundled /.vim/vimrc. Use a new path and an explicit -u argument.
|
|
files["/pageswiki.vimrc"] = await vimrcResponse.text()
|
|
|
|
// Font metrics must be final before Vim sizes its canvas/cell grid.
|
|
await document.fonts.load('16px PagesWikiHack')
|
|
const pluginResponse = await fetch('./pageswiki.vim', {cache: 'no-store'})
|
|
if (!pluginResponse.ok) throw new Error(`Plugin request failed: HTTP ${pluginResponse.status}`)
|
|
files['/.vim/plugin/pageswiki.vim'] = await pluginResponse.text()
|
|
directories.add('/.vim')
|
|
directories.add('/.vim/plugin')
|
|
|
|
const safeMode = new URLSearchParams(location.search).get('safe') === '1'
|
|
const settings = safeMode ? {} : (workspace.vim_settings || {})
|
|
const fontSize = Number.isInteger(settings.font_size) && settings.font_size >= 10 && settings.font_size <= 32 ? settings.font_size : 16
|
|
let userConfig = `\nset guifont=PagesWikiHack:h${fontSize}\n`
|
|
if (settings.enabled) {
|
|
if (settings.colorscheme) {
|
|
directories.add('/pageswiki-config')
|
|
directories.add('/pageswiki-config/colors')
|
|
files['/pageswiki-config/colors/pageswiki_custom.vim'] = settings.colorscheme
|
|
if (/^[A-Za-z0-9_-]+\.vim$/.test(settings.color_name || '')) {
|
|
files[`/pageswiki-config/colors/${settings.color_name}`] = settings.colorscheme
|
|
}
|
|
userConfig += 'set runtimepath^=/pageswiki-config\n'
|
|
}
|
|
if (settings.vimrc) {
|
|
files['/pageswiki-user.vimrc'] = settings.vimrc
|
|
userConfig += 'source /pageswiki-user.vimrc\n'
|
|
}
|
|
if (settings.colorscheme) userConfig += 'colorscheme pageswiki_custom\n'
|
|
}
|
|
files['/pageswiki.vimrc'] += userConfig
|
|
|
|
const vim = new VimWasm({
|
|
canvas: document.getElementById("vim-canvas"),
|
|
input: document.getElementById("vim-input"),
|
|
workerScriptPath: "./vendor/vim.js"
|
|
})
|
|
vim.onVimInit = () => {
|
|
vim.focus()
|
|
}
|
|
vim.onVimExit = () => {
|
|
window.close()
|
|
document.body.textContent = "Vim exited. You may close this tab."
|
|
}
|
|
vim.onTitleUpdate = title => { document.title = `${title} · vim.wasm` }
|
|
vim.onError = fail
|
|
vim.onFileExport = async (fullpath, contents) => {
|
|
try {
|
|
const bytes = new Uint8Array(contents)
|
|
let binary = ""
|
|
for (let offset = 0; offset < bytes.length; offset += 0x8000) {
|
|
binary += String.fromCharCode(...bytes.subarray(offset, offset + 0x8000))
|
|
}
|
|
const save = await fetch("./api/save", {
|
|
method: "PUT",
|
|
credentials: "same-origin",
|
|
headers: {"Content-Type": "application/json"},
|
|
body: JSON.stringify({path: fullpath, content_base64: btoa(binary)})
|
|
})
|
|
if (!save.ok) throw new Error((await save.text()).trim() || `Save failed: HTTP ${save.status}`)
|
|
} catch (error) {
|
|
fail(error)
|
|
await vim.showError(String(error))
|
|
}
|
|
}
|
|
const paths = (workspace.files || []).map(file => `/work/${file.path}`).sort()
|
|
const initial = `/work/${workspace.initial}`
|
|
const cmdArgs = ['-u', '/pageswiki.vimrc', initial, ...paths.filter(path => path !== initial)]
|
|
vim.start({
|
|
dirs: Array.from(directories).sort((a, b) => a.split("/").length - b.split("/").length),
|
|
files,
|
|
clipboard: Boolean(navigator.clipboard),
|
|
cmdArgs
|
|
})
|
|
} catch (error) {
|
|
fail(error)
|
|
}
|
|
}
|