Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
5.9 kB · 185 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186import { InputRule, inputRules, wrappingInputRule,} from "prosemirror-inputrules";import { MutableRefObject } from "react";import { Replicache } from "replicache";import type { ReplicacheMutators } from "src/replicache";import { BlockProps } from "../Block";import { focusBlock } from "src/utils/focusBlock";import { schema } from "./schema";import { useUIState } from "src/useUIState";import { flushSync } from "react-dom";export const inputrules = ( propsRef: MutableRefObject<BlockProps & { entity_set: { set: string } }>, repRef: MutableRefObject<Replicache<ReplicacheMutators> | null>,) => inputRules({ //Strikethrough rules: [ new InputRule(/\~\~([^*]+)\~\~$/, (state, match, start, end) => { const [fullMatch, content] = match; const { tr } = state; if (content) { tr.replaceWith(start, end, state.schema.text(content)) .addMark( start, start + content.length, schema.marks.strikethrough.create(), ) .removeStoredMark(schema.marks.strikethrough); return tr; } return null; }),
//Highlight new InputRule(/\=\=([^*]+)\=\=$/, (state, match, start, end) => { const [fullMatch, content] = match; const { tr } = state; if (content) { tr.replaceWith(start, end, state.schema.text(content)) .addMark( start, start + content.length, schema.marks.highlight.create({ color: useUIState.getState().lastUsedHighlight || "1", }), ) .removeStoredMark(schema.marks.highlight); return tr; } return null; }),
//Bold new InputRule(/\*\*([^*]+)\*\*$/, (state, match, start, end) => { const [fullMatch, content] = match; const { tr } = state; if (content) { tr.replaceWith(start, end, state.schema.text(content)) .addMark( start, start + content.length, schema.marks.strong.create(), ) .removeStoredMark(schema.marks.strong); return tr; } return null; }),
//Code new InputRule(/\`([^`]+)\`$/, (state, match, start, end) => { const [fullMatch, content] = match; const { tr } = state; if (content) { const startIndex = start + fullMatch.indexOf("`"); tr.replaceWith(startIndex, end, state.schema.text(content)) .addMark( startIndex, startIndex + content.length, schema.marks.code.create(), ) .removeStoredMark(schema.marks.code); return tr; } return null; }),
//Italic new InputRule(/(?:^|[^*])\*([^*]+)\*$/, (state, match, start, end) => { const [fullMatch, content] = match; const { tr } = state; if (content) { const startIndex = start + fullMatch.indexOf("*"); tr.replaceWith(startIndex, end, state.schema.text(content)) .addMark( startIndex, startIndex + content.length, schema.marks.em.create(), ) .removeStoredMark(schema.marks.em); return tr; } return null; }),
// Code Block new InputRule(/^```\s$/, (state, match) => { flushSync(() => repRef.current?.mutate.assertFact({ entity: propsRef.current.entityID, attribute: "block/type", data: { type: "block-type-union", value: "code" }, }), ); setTimeout(() => { focusBlock({ ...propsRef.current, type: "code" }, { type: "start" }); }, 20); return null; }),
//Checklist new InputRule(/^\-?\[(\ |x)?\]\s$/, (state, match) => { if (!propsRef.current.listData) repRef.current?.mutate.assertFact({ entity: propsRef.current.entityID, attribute: "block/is-list", data: { type: "boolean", value: true }, }); let tr = state.tr; tr.delete(0, match[0].length); repRef.current?.mutate.assertFact({ entity: propsRef.current.entityID, attribute: "block/check-list", data: { type: "boolean", value: match[1] === "x" ? true : false }, }); return tr; }),
// Unordered List new InputRule(/^([-+*])\s$/, (state) => { if (propsRef.current.listData) return null; let tr = state.tr; tr.delete(0, 2); repRef.current?.mutate.assertFact({ entity: propsRef.current.entityID, attribute: "block/is-list", data: { type: "boolean", value: true }, }); return tr; }),
//Blockquote new InputRule(/^([>]{1})\s$/, (state, match) => { let tr = state.tr; tr.delete(0, 2); repRef.current?.mutate.assertFact({ entity: propsRef.current.entityID, attribute: "block/type", data: { type: "block-type-union", value: "blockquote" }, }); return tr; }),
//Header new InputRule(/^([#]{1,3})\s$/, (state, match) => { let tr = state.tr; tr.delete(0, match[0].length); let headingLevel = match[1].length; repRef.current?.mutate.assertFact({ entity: propsRef.current.entityID, attribute: "block/type", data: { type: "block-type-union", value: "heading" }, }); repRef.current?.mutate.assertFact({ entity: propsRef.current.entityID, attribute: "block/heading-level", data: { type: "number", value: headingLevel }, }); return tr; }), ], });