Files
edgeever/packages/shared/src/document-extensions.ts
T
Andy Jarosz a5bda92e14 feat(editor): add per-image left, center, and right alignment (#654)
Select a standalone image to align it left, center, or right. The choice stays with that image through save, Markdown edits, and reload. Existing images keep their previous layout until an alignment is chosen. Galleries keep their own layout. Native editors apply a saved alignment and do not add a picker in this change.
2026-10-23 18:37:59 +08:00

65 lines
2.5 KiB
TypeScript

import type { AnyExtension } from "@tiptap/core";
import { EdgeEverImage as Image, ImageAlignmentAttributes } from "./image-alignment";
import { TaskList } from "@tiptap/extension-list";
import { EdgeEverTableKit as TableKit } from "./markdown-table";
import { Markdown } from "@tiptap/markdown";
import StarterKit from "@tiptap/starter-kit";
import { createEdgeEverDetailsExtensions } from "./details";
import { FileAttachment } from "./file-attachment";
import { ImageGallery } from "./image-gallery";
import { MergeDivider } from "./merge-divider";
import { PdfAttachment } from "./pdf-attachment";
import { PluginEmbed } from "./plugin-embed";
import { EmptyExternalLink } from "./empty-external-link";
import { EdgeEverLink } from "./editor-link";
import { createEdgeEverTaskItem } from "./task-item-input";
import { MathCodeFence } from "./mathematics-markdown";
export type CreateEdgeEverDocumentExtensionsOptions = {
mathematics: AnyExtension[];
starterKit?: Parameters<typeof StarterKit.configure>[0];
image?: AnyExtension | false;
gallery?: AnyExtension | false;
pdf?: AnyExtension | false;
file?: AnyExtension | false;
pluginEmbed?: AnyExtension | false;
table?: Parameters<typeof TableKit.configure>[0];
markdown?: boolean;
};
const withOptional = (value: AnyExtension | false | undefined, fallback: AnyExtension) => {
if (value === false) return [];
return [value ?? fallback];
};
/**
* Document-schema TipTap extensions shared by Markdown codecs and editors.
* Pass KaTeX math from `@edgeever/shared/mathematics` in browsers; Worker
* codecs must keep using `createEdgeEverMarkdownMathematics()`.
*/
export const createEdgeEverDocumentExtensions = (
options: CreateEdgeEverDocumentExtensionsOptions,
): AnyExtension[] => [
options.markdown
? StarterKit.configure({ ...options.starterKit, link: false })
: options.starterKit === undefined ? StarterKit : StarterKit.configure(options.starterKit),
...(options.markdown ? [EdgeEverLink] : []),
EmptyExternalLink,
TaskList,
createEdgeEverTaskItem(),
options.table === undefined ? TableKit : TableKit.configure(options.table),
...withOptional(options.image, Image),
ImageAlignmentAttributes,
...withOptional(options.gallery, ImageGallery),
...withOptional(options.pdf, PdfAttachment),
...withOptional(options.file, FileAttachment),
MergeDivider,
...createEdgeEverDetailsExtensions(),
...withOptional(options.pluginEmbed, PluginEmbed),
...options.mathematics,
MathCodeFence,
...(options.markdown
? [Markdown.configure({ markedOptions: { gfm: true } })]
: []),
];