super-productivity/packages/plugin-dev/document-mode/scripts/build.js
Johannes Millan d429900665 feat(document-mode): add TipTap-based document-mode plugin (POC)
A bundled plugin that replaces the work-view task list with a TipTap
editor for projects and the TODAY tag. Task references are read-only
chips with a checkbox; everything else is normal ProseMirror content
(paragraphs, headings, dividers) plus a simple slash menu for inserts.

- Iframe plugin scaffolded under packages/plugin-dev/document-mode with
  esbuild bundling. The editor.js bundle is inlined into index.html
  because the blob-URL iframe can't resolve relative scripts.
- background.ts registers a work-context header button with
  showFor: ['PROJECT', 'TODAY']; click calls showInWorkContext().
- editor.ts: TipTap (core + StarterKit + Placeholder) with a custom
  taskRef atom node-view (checkbox + title from getTasks() cache).
  Subscribes to WORK_CONTEXT_CHANGE for nav and ANY_TASK_UPDATE for
  title/done-state refresh. Persists per-ctx ProseMirror JSON inside
  the existing single plugin blob ({ docs: { [ctxId]: doc } }) with
  5 s debounced save and a pagehide flush.
- Plugin auto-bundled by packages/plugin-dev/scripts/build-all.js and
  registered in BUNDLED_PLUGIN_PATHS.

Out of scope for the POC (documented in the plan): inline-editable
task titles, keyed persistDataSynced API, removal of in-tree
document-mode, data migration, Electron beforeUnload hook.
2026-05-21 17:22:31 +02:00

73 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-require-imports */
const fs = require('fs');
const path = require('path');
const { build } = require('esbuild');
const ROOT_DIR = path.join(__dirname, '..');
const SRC_DIR = path.join(ROOT_DIR, 'src');
const DIST_DIR = path.join(ROOT_DIR, 'dist');
async function buildPlugin() {
console.log('Building document-mode plugin with esbuild...');
if (fs.existsSync(DIST_DIR)) {
fs.rmSync(DIST_DIR, { recursive: true });
}
fs.mkdirSync(DIST_DIR);
// Background script — runs in the host page context, registers UI + hooks.
console.log('Building plugin.js (background)...');
await build({
entryPoints: [path.join(SRC_DIR, 'background.ts')],
bundle: true,
outfile: path.join(DIST_DIR, 'plugin.js'),
platform: 'browser',
target: 'es2020',
format: 'iife',
globalName: 'DocumentModePlugin',
logLevel: 'info',
minify: true,
sourcemap: false,
});
// Editor bundle — runs inside the iframe, hosts TipTap.
console.log('Building editor.js (iframe)...');
await build({
entryPoints: [path.join(SRC_DIR, 'ui', 'editor.ts')],
bundle: true,
outfile: path.join(DIST_DIR, 'editor.js'),
platform: 'browser',
target: 'es2020',
format: 'iife',
logLevel: 'info',
minify: true,
sourcemap: false,
});
// The iframe is loaded via blob URL, so relative <script src="editor.js">
// does not resolve. Inline the bundle into index.html instead.
const editorJs = fs.readFileSync(path.join(DIST_DIR, 'editor.js'), 'utf8');
const rawHtml = fs.readFileSync(path.join(SRC_DIR, 'ui', 'index.html'), 'utf8');
// Function-form replacement so `$&`, ``$`` ``, etc. in the bundle aren't
// interpreted as replace() back-references. Also escape any literal
// `</script>` inside the JS so the HTML parser doesn't close the tag early.
const safeEditorJs = editorJs.replace(/<\/script>/gi, '<\\/script>');
const inlinedHtml = rawHtml.replace(
/<script src="editor\.js"><\/script>/,
() => `<script>${safeEditorJs}</script>`,
);
fs.writeFileSync(path.join(DIST_DIR, 'index.html'), inlinedHtml);
fs.copyFileSync(
path.join(SRC_DIR, 'manifest.json'),
path.join(DIST_DIR, 'manifest.json'),
);
fs.copyFileSync(path.join(SRC_DIR, 'icon.svg'), path.join(DIST_DIR, 'icon.svg'));
console.log('\nBuild complete! Output in dist/');
}
buildPlugin().catch((err) => {
console.error('Build failed:', err);
process.exit(1);
});