@open-elements/ui 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +153 -18
- package/dist/components/markdown-editor.d.ts +1 -1
- package/dist/components/markdown-editor.d.ts.map +1 -1
- package/dist/components/markdown-editor.js +131 -44
- package/dist/components/markdown-editor.js.map +1 -1
- package/dist/components/markdown-view.d.ts +1 -1
- package/dist/components/markdown-view.d.ts.map +1 -1
- package/dist/components/markdown-view.js +110 -27
- package/dist/components/markdown-view.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/markdown-extensions.d.ts +48 -0
- package/dist/lib/markdown-extensions.d.ts.map +1 -0
- package/dist/lib/markdown-extensions.js +73 -0
- package/dist/lib/markdown-extensions.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/index.d.ts +17 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +41 -24
- package/src/components/__tests__/markdown-editor.test.tsx +226 -22
- package/src/components/__tests__/markdown-view.test.tsx +274 -21
- package/src/components/markdown-editor.tsx +185 -79
- package/src/components/markdown-view.tsx +133 -28
- package/src/index.ts +1 -0
- package/src/lib/__tests__/markdown-extensions.test.ts +290 -0
- package/src/lib/markdown-extensions.ts +107 -0
- package/src/test/setup.ts +29 -0
- package/src/types/__tests__/markdown-toolbar-action.test.ts +34 -0
- package/src/types/index.ts +32 -0
package/README.md
CHANGED
|
@@ -14,6 +14,8 @@ This package contains extracted UI components from the Open CRM frontend, design
|
|
|
14
14
|
- **InputGroup** — Composite input with addons and buttons
|
|
15
15
|
- **Combobox** — Searchable dropdown with chip support (based on Base UI)
|
|
16
16
|
- **TagMultiSelect** — Multi-select tag picker with colored chips
|
|
17
|
+
- **MarkdownEditor** — WYSIWYG Markdown editor that round-trips all supported Markdown constructs without data loss; toolbar actions are configurable per usage via the `toolbar` prop
|
|
18
|
+
- **MarkdownView** — Read-only Markdown renderer with structural output (headings, lists, task lists, blockquotes, code); task-list checkboxes become interactive via an optional `onChange` (optimistic update with rollback)
|
|
17
19
|
|
|
18
20
|
## Usage
|
|
19
21
|
|
|
@@ -30,39 +32,172 @@ Import brand CSS in your app's stylesheet:
|
|
|
30
32
|
@import "@open-elements/ui/src/styles/brand.css";
|
|
31
33
|
```
|
|
32
34
|
|
|
35
|
+
Because this package ships raw `.tsx`, its utility classes reach your app as source text. Tailwind
|
|
36
|
+
only turns them into CSS if it scans the library, so the consuming app must point at it and load the
|
|
37
|
+
typography plugin that `MarkdownView` relies on:
|
|
38
|
+
|
|
39
|
+
```css
|
|
40
|
+
@import "tailwindcss";
|
|
41
|
+
@import "@open-elements/ui/src/styles/brand.css";
|
|
42
|
+
@plugin "@tailwindcss/typography";
|
|
43
|
+
@import "tw-animate-css";
|
|
44
|
+
@source "../node_modules/@open-elements/ui/src";
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Each line is load-bearing:
|
|
48
|
+
|
|
49
|
+
- **`@source`** — this package ships raw `.tsx`, so its utility classes reach your app as source
|
|
50
|
+
text. Without the scan they never become CSS, and components render unstyled: task list
|
|
51
|
+
checkboxes pick up a `prose` bullet, for instance.
|
|
52
|
+
- **`@tailwindcss/typography`** — `MarkdownView` and `MarkdownEditor` hardcode `prose prose-sm`.
|
|
53
|
+
- **`tw-animate-css`** — every overlay (`Dialog`, `AlertDialog`, `Sheet`, `Popover`, `Select`,
|
|
54
|
+
`Combobox`, `Tooltip`) writes its enter and exit states with `animate-in`, `fade-in-0`,
|
|
55
|
+
`zoom-in-95` and `slide-in-from-*`, none of which are Tailwind core utilities.
|
|
56
|
+
|
|
57
|
+
The [component showcase](#component-showcase) uses exactly this configuration and asserts the
|
|
58
|
+
result, so a break in this contract surfaces there.
|
|
59
|
+
|
|
33
60
|
## Translations
|
|
34
61
|
|
|
35
62
|
```typescript
|
|
36
63
|
import { de, en } from "@open-elements/ui";
|
|
37
64
|
```
|
|
38
65
|
|
|
39
|
-
##
|
|
40
|
-
|
|
41
|
-
Every release must be published to npm **and** have a corresponding Git tag and GitHub Release.
|
|
66
|
+
## Component Showcase
|
|
42
67
|
|
|
43
|
-
|
|
68
|
+
Every component can be opened in isolation, with its props adjustable at runtime, in a Storybook
|
|
69
|
+
catalogue:
|
|
44
70
|
|
|
45
71
|
```bash
|
|
46
|
-
|
|
72
|
+
pnpm storybook # dev server on http://localhost:6006
|
|
73
|
+
pnpm build-storybook # static build into storybook-static/
|
|
47
74
|
```
|
|
48
75
|
|
|
49
|
-
|
|
76
|
+
Stories live in [`stories/`](stories/), deliberately outside `src/`: `files` in `package.json`
|
|
77
|
+
publishes all of `src/`, so a colocated story would ship in the tarball and break a consumer's `tsc`
|
|
78
|
+
on unresolvable `@storybook/*` imports. Nothing under `src/` imports Storybook, and Tailwind is a
|
|
79
|
+
devDependency that no build script consumes — the published package is unaffected.
|
|
80
|
+
|
|
81
|
+
Stories carry `play` functions, which run in a real browser and cover what jsdom can only check
|
|
82
|
+
indirectly: the toolbar allowlist, the task-list creation gate under actual keystrokes, the
|
|
83
|
+
checkbox lifecycle after actual clicks, and that the Tailwind utilities the components rely on
|
|
84
|
+
resolve to real styles. They are additive; the vitest suites are unchanged.
|
|
85
|
+
|
|
86
|
+
Every exported component has stories, grouped in the sidebar as Primitives, Forms, Overlays, Data,
|
|
87
|
+
Actions, Navigation and Markdown.
|
|
88
|
+
|
|
89
|
+
Rendering the library for real turned up defects the unit tests cannot see — an undefined
|
|
90
|
+
`--radius`, a non-existent `text-oe-gray`, unlabelled icon buttons and a set of brand tokens that
|
|
91
|
+
miss WCAG AA. They are catalogued in [docs/showcase-findings.md](docs/showcase-findings.md); none
|
|
92
|
+
have been changed in `src/`.
|
|
93
|
+
|
|
94
|
+
Deployment of the showcase is documented in
|
|
95
|
+
[docs/showcase-deployment.md](docs/showcase-deployment.md).
|
|
96
|
+
|
|
97
|
+
## Software Bill of Materials (SBOM)
|
|
98
|
+
|
|
99
|
+
Every release publishes two [CycloneDX](https://cyclonedx.org/) 1.7 SBOMs as assets on its
|
|
100
|
+
[GitHub Release](https://github.com/OpenElementsLabs/open-elements-ui/releases), so a specific
|
|
101
|
+
published version can be obtained without an `npm install`:
|
|
102
|
+
|
|
103
|
+
| Asset | Contents | Authoritative? |
|
|
104
|
+
| ------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------ |
|
|
105
|
+
| `sbom.cdx.json` | Runtime dependencies (transitive) plus the library's peer dependencies | **Yes** — use this for supplier assessments (Cyber Resilience Act) |
|
|
106
|
+
| `sbom-dev.cdx.json` | The build toolchain (`devDependencies`) | No — provided for transparency only |
|
|
107
|
+
|
|
108
|
+
Both are generated locally with the pinned `pnpm` (`pnpm sbom`) and verified in CI on every pull
|
|
109
|
+
request, so a dependency change that breaks the SBOM turns the build red. A release cannot ship without
|
|
110
|
+
a valid SBOM.
|
|
50
111
|
|
|
51
112
|
```bash
|
|
52
|
-
|
|
113
|
+
pnpm sbom # writes sbom/sbom.cdx.json and sbom/sbom-dev.cdx.json (gitignored)
|
|
114
|
+
pnpm sbom:verify # validates both against the CycloneDX 1.7 schema and package.json
|
|
53
115
|
```
|
|
54
116
|
|
|
55
|
-
|
|
117
|
+
**Peer dependencies.** `radix-ui`, `@base-ui/react`, `lucide-react`, `react` and `react-dom` are peer
|
|
118
|
+
dependencies: the consumer supplies them. `pnpm sbom` alone omits them, so they are added to
|
|
119
|
+
`sbom.cdx.json` and marked with a `cdx:npm:peer` property holding the declared range. The **version**
|
|
120
|
+
recorded for each peer is the one resolved in _this repository's_ lockfile, not the one a consumer
|
|
121
|
+
installs — it changes when we bump our own devDependencies, even though nothing changes for the
|
|
122
|
+
consumer. Read a peer's `cdx:npm:peer` range, not its pinned version, as the requirement.
|
|
56
123
|
|
|
57
|
-
|
|
58
|
-
2. Builds and tests the project
|
|
59
|
-
3. Commits, tags (`v<version>`), and pushes to GitHub
|
|
60
|
-
4. Publishes the package to npm
|
|
61
|
-
5. Creates a GitHub Release with auto-generated notes
|
|
62
|
-
6. Sets the next development version in `package.json`, commits, and pushes
|
|
124
|
+
## Releasing a New Version
|
|
63
125
|
|
|
64
|
-
|
|
126
|
+
Every release must be published to npm **and** have a corresponding Git tag and GitHub Release.
|
|
65
127
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
128
|
+
A release is driven entirely by pushing a `v<version>` tag:
|
|
129
|
+
that triggers [`.github/workflows/release.yml`](.github/workflows/release.yml), which verifies,
|
|
130
|
+
builds, generates the SBOMs, _stages_ the npm publish and creates a **draft** GitHub Release.
|
|
131
|
+
Neither the npm version nor the GitHub Release goes live until a maintainer approves it by hand —
|
|
132
|
+
both stay behind the same human 2FA gate.
|
|
133
|
+
|
|
134
|
+
### Steps
|
|
135
|
+
|
|
136
|
+
1. **Add an upgrade guide** for user-visible changes as `docs/upgrade-to-<major.minor>.md`,
|
|
137
|
+
following the existing guides, and commit it. Do this before the bump — `pnpm version` needs a
|
|
138
|
+
clean working tree, and the tag must point at a commit that already contains everything.
|
|
139
|
+
2. **Verify locally** before tagging — the workflow runs the same checks and a failure means
|
|
140
|
+
re-cutting the tag:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
pnpm install --frozen-lockfile
|
|
144
|
+
pnpm run typecheck && pnpm run lint && pnpm run format:check
|
|
145
|
+
pnpm run test && pnpm run build
|
|
146
|
+
pnpm run sbom && pnpm run sbom:verify
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
3. **Bump the version.** On an up-to-date `main`, let `pnpm version` do it. It rewrites
|
|
150
|
+
`package.json`, commits the change and creates the annotated `v<version>` tag in one step, so the
|
|
151
|
+
tag and `package.json` version can never disagree — which is exactly what the workflow verifies:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
pnpm version patch # 0.9.0 -> 0.9.1
|
|
155
|
+
pnpm version minor # 0.9.0 -> 0.10.0
|
|
156
|
+
pnpm version major # 0.9.0 -> 1.0.0
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Pass an exact version (`pnpm version 0.10.0`) when you do not want a relative bump. Only plain
|
|
160
|
+
`A.B.C` releases are supported — the workflow rejects pre-release tags, so avoid
|
|
161
|
+
`premajor`/`preminor`/`prepatch`/`prerelease`. The default commit message is the bare version
|
|
162
|
+
number; add `--message "chore: release %s"` to match the repository's commit style. `v` is
|
|
163
|
+
already pnpm's default tag prefix and matches the workflow's `v*` trigger.
|
|
164
|
+
|
|
165
|
+
4. **Push the commit and the tag** — pushing the tag is what starts the release:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
git push --follow-tags origin main
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
5. **Watch the workflow.** `gh run watch` (or the Actions tab). It ends with the version in the npm
|
|
172
|
+
staging queue and a draft GitHub Release carrying both SBOMs as assets.
|
|
173
|
+
6. **Approve the npm publish** — this is the step that makes the version installable. Either approve
|
|
174
|
+
it on npmjs.com, or:
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
pnpm stage list # find the stage id for the release
|
|
178
|
+
pnpm stage approve <stage-id> # requires 2FA
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
7. **Publish the draft GitHub Release** on GitHub, or with
|
|
182
|
+
`gh release edit v0.10.0 --draft=false`. Review the auto-generated notes first.
|
|
183
|
+
8. **Open the next development version** — bump `package.json` to the next planned version with
|
|
184
|
+
`pnpm version <next-version> --no-git-tag-version`, then commit and push to `main`.
|
|
185
|
+
`--no-git-tag-version` is required here: without it pnpm would tag the development bump and
|
|
186
|
+
trigger another release.
|
|
187
|
+
|
|
188
|
+
### If something goes wrong
|
|
189
|
+
|
|
190
|
+
- **The workflow fails before `pnpm stage publish`** (checks, build, SBOM): nothing was published.
|
|
191
|
+
Delete the tag, fix the problem, then re-cut the tag on the fixed commit. Do not run
|
|
192
|
+
`pnpm version` again — `package.json` already holds the release version, so tag by hand:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
git tag -d v0.10.0 && git push origin :refs/tags/v0.10.0
|
|
196
|
+
# commit the fix, then:
|
|
197
|
+
git tag -a v0.10.0 -m "0.10.0" && git push origin v0.10.0
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
- **The workflow failed after staging**: the version sits unapproved in the npm staging queue. Do
|
|
201
|
+
not approve it — discard the stage on npmjs.com and re-cut the tag as above.
|
|
202
|
+
- **Wrong content already approved on npm**: the version is immutable. Do not attempt to reuse it —
|
|
203
|
+
release a patch version instead.
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { MarkdownEditorProps } from "../types/index.ts";
|
|
2
|
-
export declare function MarkdownEditor({ value, onChange, placeholder }: MarkdownEditorProps): import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare function MarkdownEditor({ value, onChange, placeholder, toolbar }: MarkdownEditorProps): import("react/jsx-runtime").JSX.Element;
|
|
3
3
|
//# sourceMappingURL=markdown-editor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-editor.d.ts","sourceRoot":"","sources":["../../src/components/markdown-editor.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"markdown-editor.d.ts","sourceRoot":"","sources":["../../src/components/markdown-editor.tsx"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,mBAAmB,EAAyB,MAAM,mBAAmB,CAAC;AAuLpF,wBAAgB,cAAc,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,EAAE,mBAAmB,2CAuC5F"}
|
|
@@ -1,58 +1,145 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { useEditor, EditorContent } from "@tiptap/react";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import Placeholder from "@tiptap/extension-placeholder";
|
|
7
|
-
import { Markdown } from "tiptap-markdown";
|
|
8
|
-
import { Bold, Italic, Strikethrough, Link as LinkIcon, Unlink } from "lucide-react";
|
|
9
|
-
import { useEffect, useCallback } from "react";
|
|
4
|
+
import { Bold, Italic, Strikethrough, Code, Link as LinkIcon, Unlink, Heading1, Heading2, Heading3, List, ListOrdered, ListChecks, Quote, SquareCode, Minus, } from "lucide-react";
|
|
5
|
+
import { useEffect } from "react";
|
|
10
6
|
import { cn } from "../lib/utils.js";
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
import { createMarkdownExtensions } from "../lib/markdown-extensions.js";
|
|
8
|
+
const DEFAULT_TOOLBAR = ["bold", "italic"];
|
|
9
|
+
/** Opens a prompt to set, change or clear the link on the current selection. */
|
|
10
|
+
function editLink(editor) {
|
|
11
|
+
var _a;
|
|
12
|
+
const previousUrl = (_a = editor.getAttributes("link").href) !== null && _a !== void 0 ? _a : "";
|
|
13
|
+
const url = window.prompt("URL", previousUrl);
|
|
14
|
+
if (url === null)
|
|
15
|
+
return;
|
|
16
|
+
if (url === "") {
|
|
17
|
+
editor.chain().focus().extendMarkRange("link").unsetLink().run();
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Single mapping from a toolbar action to its icon, accessible label, active-state
|
|
24
|
+
* predicate and command. Adding an action later touches only this record.
|
|
25
|
+
*/
|
|
26
|
+
const ACTIONS = {
|
|
27
|
+
bold: {
|
|
28
|
+
icon: Bold,
|
|
29
|
+
label: "Bold",
|
|
30
|
+
isActive: (e) => e.isActive("bold"),
|
|
31
|
+
run: (e) => e.chain().focus().toggleBold().run(),
|
|
32
|
+
},
|
|
33
|
+
italic: {
|
|
34
|
+
icon: Italic,
|
|
35
|
+
label: "Italic",
|
|
36
|
+
isActive: (e) => e.isActive("italic"),
|
|
37
|
+
run: (e) => e.chain().focus().toggleItalic().run(),
|
|
38
|
+
},
|
|
39
|
+
strike: {
|
|
40
|
+
icon: Strikethrough,
|
|
41
|
+
label: "Strikethrough",
|
|
42
|
+
isActive: (e) => e.isActive("strike"),
|
|
43
|
+
run: (e) => e.chain().focus().toggleStrike().run(),
|
|
44
|
+
},
|
|
45
|
+
code: {
|
|
46
|
+
icon: Code,
|
|
47
|
+
label: "Code",
|
|
48
|
+
isActive: (e) => e.isActive("code"),
|
|
49
|
+
run: (e) => e.chain().focus().toggleCode().run(),
|
|
50
|
+
},
|
|
51
|
+
link: {
|
|
52
|
+
icon: LinkIcon,
|
|
53
|
+
label: "Link",
|
|
54
|
+
isActive: (e) => e.isActive("link"),
|
|
55
|
+
run: editLink,
|
|
56
|
+
},
|
|
57
|
+
h1: {
|
|
58
|
+
icon: Heading1,
|
|
59
|
+
label: "Heading 1",
|
|
60
|
+
isActive: (e) => e.isActive("heading", { level: 1 }),
|
|
61
|
+
run: (e) => e.chain().focus().toggleHeading({ level: 1 }).run(),
|
|
62
|
+
},
|
|
63
|
+
h2: {
|
|
64
|
+
icon: Heading2,
|
|
65
|
+
label: "Heading 2",
|
|
66
|
+
isActive: (e) => e.isActive("heading", { level: 2 }),
|
|
67
|
+
run: (e) => e.chain().focus().toggleHeading({ level: 2 }).run(),
|
|
68
|
+
},
|
|
69
|
+
h3: {
|
|
70
|
+
icon: Heading3,
|
|
71
|
+
label: "Heading 3",
|
|
72
|
+
isActive: (e) => e.isActive("heading", { level: 3 }),
|
|
73
|
+
run: (e) => e.chain().focus().toggleHeading({ level: 3 }).run(),
|
|
74
|
+
},
|
|
75
|
+
bulletList: {
|
|
76
|
+
icon: List,
|
|
77
|
+
label: "Bullet list",
|
|
78
|
+
isActive: (e) => e.isActive("bulletList"),
|
|
79
|
+
run: (e) => e.chain().focus().toggleBulletList().run(),
|
|
80
|
+
},
|
|
81
|
+
orderedList: {
|
|
82
|
+
icon: ListOrdered,
|
|
83
|
+
label: "Numbered list",
|
|
84
|
+
isActive: (e) => e.isActive("orderedList"),
|
|
85
|
+
run: (e) => e.chain().focus().toggleOrderedList().run(),
|
|
86
|
+
},
|
|
87
|
+
taskList: {
|
|
88
|
+
icon: ListChecks,
|
|
89
|
+
label: "Task list",
|
|
90
|
+
isActive: (e) => e.isActive("taskList"),
|
|
91
|
+
run: (e) => e.chain().focus().toggleTaskList().run(),
|
|
92
|
+
},
|
|
93
|
+
blockquote: {
|
|
94
|
+
icon: Quote,
|
|
95
|
+
label: "Blockquote",
|
|
96
|
+
isActive: (e) => e.isActive("blockquote"),
|
|
97
|
+
run: (e) => e.chain().focus().toggleBlockquote().run(),
|
|
98
|
+
},
|
|
99
|
+
codeBlock: {
|
|
100
|
+
icon: SquareCode,
|
|
101
|
+
label: "Code block",
|
|
102
|
+
isActive: (e) => e.isActive("codeBlock"),
|
|
103
|
+
run: (e) => e.chain().focus().toggleCodeBlock().run(),
|
|
104
|
+
},
|
|
105
|
+
horizontalRule: {
|
|
106
|
+
icon: Minus,
|
|
107
|
+
label: "Horizontal rule",
|
|
108
|
+
isActive: () => false,
|
|
109
|
+
run: (e) => e.chain().focus().setHorizontalRule().run(),
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
function ToolbarButton({ active, onClick, icon: Icon, label, }) {
|
|
113
|
+
return (_jsx("button", { type: "button", title: label, "aria-label": label, "aria-pressed": active, onClick: onClick, className: cn("rounded p-1.5 transition-colors", active
|
|
13
114
|
? "bg-oe-dark/10 text-oe-dark"
|
|
14
|
-
: "text-muted-foreground hover:bg-muted hover:text-foreground",
|
|
115
|
+
: "text-muted-foreground hover:bg-muted hover:text-foreground"), children: _jsx(Icon, { className: "h-4 w-4" }) }));
|
|
15
116
|
}
|
|
16
|
-
function Toolbar({ editor }) {
|
|
17
|
-
const setLink = useCallback(() => {
|
|
18
|
-
var _a;
|
|
19
|
-
if (!editor)
|
|
20
|
-
return;
|
|
21
|
-
const previousUrl = (_a = editor.getAttributes("link").href) !== null && _a !== void 0 ? _a : "";
|
|
22
|
-
const url = window.prompt("URL", previousUrl);
|
|
23
|
-
if (url === null)
|
|
24
|
-
return;
|
|
25
|
-
if (url === "") {
|
|
26
|
-
editor.chain().focus().extendMarkRange("link").unsetLink().run();
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run();
|
|
30
|
-
}, [editor]);
|
|
117
|
+
function Toolbar({ editor, actions, }) {
|
|
31
118
|
if (!editor)
|
|
32
119
|
return null;
|
|
33
|
-
|
|
120
|
+
// Preserve declaration order, drop duplicates (first occurrence wins).
|
|
121
|
+
const uniqueActions = actions.filter((action, index) => actions.indexOf(action) === index);
|
|
122
|
+
if (uniqueActions.length === 0)
|
|
123
|
+
return null;
|
|
124
|
+
return (_jsx("div", { className: "flex items-center gap-0.5 border-b px-2 py-1.5", children: uniqueActions.map((action) => {
|
|
125
|
+
const spec = ACTIONS[action];
|
|
126
|
+
return (_jsxs("span", { className: "flex items-center gap-0.5", children: [_jsx(ToolbarButton, { icon: spec.icon, label: spec.label, active: spec.isActive(editor), onClick: () => spec.run(editor) }), action === "link" && editor.isActive("link") && (_jsx(ToolbarButton, { icon: Unlink, label: "Remove link", onClick: () => editor.chain().focus().unsetLink().run() }))] }, action));
|
|
127
|
+
}) }));
|
|
34
128
|
}
|
|
35
|
-
export function MarkdownEditor({ value, onChange, placeholder }) {
|
|
129
|
+
export function MarkdownEditor({ value, onChange, placeholder, toolbar }) {
|
|
130
|
+
const actions = toolbar !== null && toolbar !== void 0 ? toolbar : DEFAULT_TOOLBAR;
|
|
36
131
|
const editor = useEditor({
|
|
37
|
-
extensions:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
horizontalRule: false,
|
|
43
|
-
bulletList: false,
|
|
44
|
-
orderedList: false,
|
|
45
|
-
listItem: false,
|
|
46
|
-
}),
|
|
47
|
-
Link.configure({
|
|
48
|
-
openOnClick: false,
|
|
49
|
-
HTMLAttributes: { class: "text-blue-600 underline" },
|
|
50
|
-
}),
|
|
51
|
-
Placeholder.configure({ placeholder: placeholder !== null && placeholder !== void 0 ? placeholder : "" }),
|
|
52
|
-
Markdown,
|
|
53
|
-
],
|
|
132
|
+
extensions: createMarkdownExtensions({
|
|
133
|
+
placeholder,
|
|
134
|
+
openLinksOnClick: false,
|
|
135
|
+
allowedActions: actions,
|
|
136
|
+
}),
|
|
54
137
|
content: value,
|
|
55
138
|
immediatelyRender: false,
|
|
139
|
+
// Re-render the toolbar on every transaction so active states and the
|
|
140
|
+
// contextual Unlink button track the cursor. TipTap v3 defaults this to
|
|
141
|
+
// false, which would freeze the toolbar's active styling as the user moves.
|
|
142
|
+
shouldRerenderOnTransaction: true,
|
|
56
143
|
onUpdate: ({ editor: ed }) => {
|
|
57
144
|
onChange(ed.storage.markdown.getMarkdown());
|
|
58
145
|
},
|
|
@@ -70,6 +157,6 @@ export function MarkdownEditor({ value, onChange, placeholder }) {
|
|
|
70
157
|
editor.commands.setContent(value);
|
|
71
158
|
}
|
|
72
159
|
}, [value, editor]);
|
|
73
|
-
return (_jsxs("div", { className: "bg-background rounded-md border", children: [_jsx(Toolbar, { editor: editor }), _jsx(EditorContent, { editor: editor })] }));
|
|
160
|
+
return (_jsxs("div", { className: "bg-background rounded-md border", children: [_jsx(Toolbar, { editor: editor, actions: actions }), _jsx(EditorContent, { editor: editor })] }));
|
|
74
161
|
}
|
|
75
162
|
//# sourceMappingURL=markdown-editor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-editor.js","sourceRoot":"","sources":["../../src/components/markdown-editor.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,SAAS,EAAE,aAAa,EAAe,MAAM,eAAe,CAAC;AACtE,OAAO,
|
|
1
|
+
{"version":3,"file":"markdown-editor.js","sourceRoot":"","sources":["../../src/components/markdown-editor.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,SAAS,EAAE,aAAa,EAAe,MAAM,eAAe,CAAC;AACtE,OAAO,EACL,IAAI,EAC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Q,CAAC,WAAW,EAAE,CAAC;QAC9D,IAAI,KAAK,KAAK,eAAe,EAAE,CAAC;YAC9B,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QACpC,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;IAEpB,OAAO,CACL,eAAK,SAAS,EAAC,iCAAiC,aAC9C,KAAC,OAAO,IAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAI,EAC7C,KAAC,aAAa,IAAC,MAAM,EAAE,MAAM,GAAI,IAC7B,CACP,CAAC;AACJ,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { MarkdownViewProps } from "../types/index.ts";
|
|
2
|
-
export declare function MarkdownView({ content }: MarkdownViewProps): import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare function MarkdownView({ content, onChange }: MarkdownViewProps): import("react/jsx-runtime").JSX.Element;
|
|
3
3
|
//# sourceMappingURL=markdown-view.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-view.d.ts","sourceRoot":"","sources":["../../src/components/markdown-view.tsx"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"markdown-view.d.ts","sourceRoot":"","sources":["../../src/components/markdown-view.tsx"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAyC3D,wBAAgB,YAAY,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,iBAAiB,2CAyGpE"}
|
|
@@ -1,32 +1,95 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import { useEffect } from "react";
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
4
|
import { useEditor, EditorContent } from "@tiptap/react";
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
import { cn } from "../lib/utils.js";
|
|
6
|
+
import { createMarkdownExtensions } from "../lib/markdown-extensions.js";
|
|
7
|
+
/**
|
|
8
|
+
* Resolve the document position of the task item whose checkbox was just
|
|
9
|
+
* toggled to `checked`.
|
|
10
|
+
*
|
|
11
|
+
* The `node` handed to `onReadOnlyChecked` cannot be trusted for this: the
|
|
12
|
+
* extension-list node view is reused across updates, so its closure keeps the
|
|
13
|
+
* node from creation and goes stale after the first toggle. Instead we locate
|
|
14
|
+
* the clicked item by DOM: at the moment of the click exactly one `<li>` has a
|
|
15
|
+
* live checkbox that disagrees with its `data-checked` attribute (the document
|
|
16
|
+
* has not been updated yet). DOM order matches document order, so its index
|
|
17
|
+
* among the task-item `<li>`s maps directly to the nth `taskItem` node.
|
|
18
|
+
*/
|
|
19
|
+
function positionOfToggled(editor, checked) {
|
|
20
|
+
const items = Array.from(editor.view.dom.querySelectorAll("li[data-checked]"));
|
|
21
|
+
const clickedIndex = items.findIndex((li) => {
|
|
22
|
+
const input = li.querySelector("input[type=checkbox]");
|
|
23
|
+
return input != null && input.checked === checked && li.dataset.checked !== String(checked);
|
|
24
|
+
});
|
|
25
|
+
if (clickedIndex === -1)
|
|
26
|
+
return -1;
|
|
27
|
+
let seen = 0;
|
|
28
|
+
let position = -1;
|
|
29
|
+
editor.state.doc.descendants((node, pos) => {
|
|
30
|
+
if (position !== -1)
|
|
31
|
+
return false; // already found — stop searching
|
|
32
|
+
if (node.type.name === "taskItem") {
|
|
33
|
+
if (seen === clickedIndex) {
|
|
34
|
+
position = pos;
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
seen += 1;
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
});
|
|
41
|
+
return position;
|
|
42
|
+
}
|
|
43
|
+
export function MarkdownView({ content, onChange }) {
|
|
44
|
+
const [busy, setBusy] = useState(false);
|
|
45
|
+
// Refs let the stable onReadOnlyChecked callback read the latest values
|
|
46
|
+
// without being recreated (which would require rebuilding the editor).
|
|
47
|
+
const busyRef = useRef(false);
|
|
48
|
+
const lastConfirmedRef = useRef(content);
|
|
49
|
+
const editorRef = useRef(null);
|
|
50
|
+
const onChangeRef = useRef(onChange);
|
|
51
|
+
onChangeRef.current = onChange;
|
|
52
|
+
const setBusy2 = useCallback((value) => {
|
|
53
|
+
busyRef.current = value;
|
|
54
|
+
setBusy(value);
|
|
55
|
+
}, []);
|
|
56
|
+
const handleReadOnlyChecked = useCallback((_node, checked) => {
|
|
57
|
+
const editor = editorRef.current;
|
|
58
|
+
// Swallow the click (node view reverts it) when there is no consumer to
|
|
59
|
+
// report to, or while a save is already in flight.
|
|
60
|
+
if (!editor || !onChangeRef.current || busyRef.current)
|
|
61
|
+
return false;
|
|
62
|
+
const position = positionOfToggled(editor, checked);
|
|
63
|
+
if (position === -1)
|
|
64
|
+
return false;
|
|
65
|
+
// Apply the toggle to the document ourselves — the read-only node view
|
|
66
|
+
// does not touch the document, only the DOM checkbox.
|
|
67
|
+
const current = editor.state.doc.nodeAt(position);
|
|
68
|
+
editor.view.dispatch(editor.state.tr.setNodeMarkup(position, undefined, Object.assign(Object.assign({}, current === null || current === void 0 ? void 0 : current.attrs), { checked })));
|
|
69
|
+
const markdown = editor.storage.markdown.getMarkdown();
|
|
70
|
+
const result = onChangeRef.current(markdown);
|
|
71
|
+
if (result instanceof Promise) {
|
|
72
|
+
setBusy2(true);
|
|
73
|
+
result
|
|
74
|
+
.then(() => {
|
|
75
|
+
lastConfirmedRef.current = markdown;
|
|
76
|
+
setBusy2(false);
|
|
77
|
+
})
|
|
78
|
+
.catch(() => {
|
|
79
|
+
editor.commands.setContent(lastConfirmedRef.current);
|
|
80
|
+
setBusy2(false);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
lastConfirmedRef.current = markdown;
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}, [setBusy2]);
|
|
9
88
|
const editor = useEditor({
|
|
10
|
-
extensions:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
blockquote: false,
|
|
15
|
-
horizontalRule: false,
|
|
16
|
-
bulletList: false,
|
|
17
|
-
orderedList: false,
|
|
18
|
-
listItem: false,
|
|
19
|
-
}),
|
|
20
|
-
Link.configure({
|
|
21
|
-
openOnClick: true,
|
|
22
|
-
HTMLAttributes: {
|
|
23
|
-
class: "text-blue-600 underline",
|
|
24
|
-
target: "_blank",
|
|
25
|
-
rel: "noopener noreferrer",
|
|
26
|
-
},
|
|
27
|
-
}),
|
|
28
|
-
Markdown,
|
|
29
|
-
],
|
|
89
|
+
extensions: createMarkdownExtensions({
|
|
90
|
+
openLinksOnClick: true,
|
|
91
|
+
onReadOnlyChecked: handleReadOnlyChecked,
|
|
92
|
+
}),
|
|
30
93
|
content,
|
|
31
94
|
immediatelyRender: false,
|
|
32
95
|
editable: false,
|
|
@@ -36,11 +99,31 @@ export function MarkdownView({ content }) {
|
|
|
36
99
|
},
|
|
37
100
|
},
|
|
38
101
|
});
|
|
102
|
+
editorRef.current = editor;
|
|
103
|
+
// Guarded sync: only rebuild when `content` genuinely differs from what the
|
|
104
|
+
// document already holds, so an echo of what we just reported is a no-op. A
|
|
105
|
+
// genuinely new value also becomes the confirmed baseline, so a later
|
|
106
|
+
// rejection does not roll back to a stale document.
|
|
39
107
|
useEffect(() => {
|
|
40
|
-
if (editor)
|
|
108
|
+
if (!editor)
|
|
109
|
+
return;
|
|
110
|
+
const current = editor.storage.markdown.getMarkdown();
|
|
111
|
+
if (content !== current) {
|
|
41
112
|
editor.commands.setContent(content);
|
|
113
|
+
lastConfirmedRef.current = content;
|
|
42
114
|
}
|
|
43
115
|
}, [content, editor]);
|
|
44
|
-
|
|
116
|
+
// Reflect the busy state on the actual checkboxes. Re-applied whenever the
|
|
117
|
+
// document may have been rebuilt (busy toggled, content changed), because
|
|
118
|
+
// node views recreate the input elements on every transaction.
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (!editor)
|
|
121
|
+
return;
|
|
122
|
+
const inputs = editor.view.dom.querySelectorAll("input[type=checkbox]");
|
|
123
|
+
inputs.forEach((input) => {
|
|
124
|
+
input.disabled = busy;
|
|
125
|
+
});
|
|
126
|
+
}, [busy, editor, content]);
|
|
127
|
+
return (_jsx("div", { "aria-busy": busy, className: cn(busy && "[&_input[type=checkbox]]:cursor-not-allowed [&_input[type=checkbox]]:opacity-50"), children: _jsx(EditorContent, { editor: editor }) }));
|
|
45
128
|
}
|
|
46
129
|
//# sourceMappingURL=markdown-view.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-view.js","sourceRoot":"","sources":["../../src/components/markdown-view.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"markdown-view.js","sourceRoot":"","sources":["../../src/components/markdown-view.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAe,MAAM,eAAe,CAAC;AAEtE,OAAO,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AACrC,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAMzE;;;;;;;;;;;GAWG;AACH,SAAS,iBAAiB,CAAC,MAAc,EAAE,OAAgB;IACzD,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAc,kBAAkB,CAAC,CAAC,CAAC;IAC5F,MAAM,YAAY,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE;QAC1C,MAAM,KAAK,GAAG,EAAE,CAA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AAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAmB,sBAAsB,CAAC,CAAC;QAC1F,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;YACvB,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAE5B,OAAO,CACL,2BACa,IAAI,EACf,SAAS,EAAE,EAAE,CACX,IAAI,IAAI,iFAAiF,CAC1F,YAED,KAAC,aAAa,IAAC,MAAM,EAAE,MAAM,GAAI,GAC7B,CACP,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -50,7 +50,7 @@ export type { TooltipIconButtonProps } from "./components/tooltip-icon-button.ts
|
|
|
50
50
|
export { TablePagination } from "./components/table-pagination.tsx";
|
|
51
51
|
export type { PaginationTranslations } from "./components/table-pagination.tsx";
|
|
52
52
|
export type { TagDto } from "./types/index.ts";
|
|
53
|
-
export type { UserOption, UserAvatarProps, UserMultiSelectProps, UserMultiSelectTranslations, MarkdownEditorProps, MarkdownViewProps, } from "./types/index.ts";
|
|
53
|
+
export type { UserOption, UserAvatarProps, UserMultiSelectProps, UserMultiSelectTranslations, MarkdownEditorProps, MarkdownViewProps, MarkdownToolbarAction, } from "./types/index.ts";
|
|
54
54
|
export { cn } from "./lib/utils.ts";
|
|
55
55
|
export { LanguageProvider, useTranslations, useLanguage } from "./i18n/language-context.tsx";
|
|
56
56
|
export type { Language } from "./i18n/language-context.tsx";
|