@ilamanownuk/blog-kit 0.1.0 → 0.2.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/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import * as react from 'react';
1
+ import * as React from 'react';
2
2
  import { ReactNode } from 'react';
3
3
 
4
4
  /** All UI strings. Persian by default; pass `labels` to the editor to override any of them. */
@@ -71,6 +71,9 @@ type Labels = {
71
71
  [K in keyof typeof defaultLabels]: string;
72
72
  };
73
73
 
74
+ /** Extra class names per UI part; set through the editor's `classNames` prop. */
75
+ type EditorClassNames = Partial<Record<"root" | "toolbar" | "separator" | "button" | "content" | "tableTools" | "popover" | "dialog" | "input" | "label", string>>;
76
+
74
77
  type EmbedProvider = "aparat" | "youtube";
75
78
  /** Watch link (Aparat / YouTube) to a safe embed URL; anything else returns null. */
76
79
  declare function toVideoEmbedUrl(input: string, allowed?: readonly EmbedProvider[]): string | null;
@@ -126,6 +129,8 @@ type MediaPickerRenderProps = {
126
129
  close: () => void;
127
130
  };
128
131
 
132
+ /** Toolbar entries; "|" is a separator. */
133
+ type ToolbarItem = "bold" | "italic" | "underline" | "strike" | "textColor" | "highlight" | "h2" | "h3" | "h4" | "right" | "center" | "left" | "justify" | "bullet" | "ordered" | "quote" | "code" | "hr" | "link" | "unlink" | "image" | "video" | "table" | "undo" | "redo" | "|";
129
134
  type RichTextEditorProps = {
130
135
  /** HTML string. */
131
136
  value: string;
@@ -134,7 +139,12 @@ type RichTextEditorProps = {
134
139
  placeholder?: string;
135
140
  invalid?: boolean;
136
141
  id?: string;
142
+ /** Extra class on the root element (same as `classNames.root`). */
137
143
  className?: string;
144
+ /** Extra class names per part: root, toolbar, separator, button, content, tableTools, popover, dialog, input, label. Read once on mount for `content`. */
145
+ classNames?: EditorClassNames;
146
+ /** Which toolbar items to show, in order; "|" is a separator. Default: `defaultToolbar`. */
147
+ toolbar?: readonly ToolbarItem[];
138
148
  /** Enables the "Upload" button in the image dialog. Throw / reject to show an error. */
139
149
  uploadImage?: (file: File) => Promise<PickedImage>;
140
150
  /** Built-in media library picker. */
@@ -153,6 +163,9 @@ type RichTextEditorProps = {
153
163
  * Tiptap rich-text editor. Output is an HTML string; ALWAYS sanitize it on the server
154
164
  * (`@ilamanownuk/blog-kit/server`). Extensions are created once on mount.
155
165
  */
156
- declare function RichTextEditor({ value, onChange, placeholder, invalid, id, className, uploadImage, mediaApi, renderMediaPicker, resolveImageSrc, allowedEmbeds, labels, dir, }: RichTextEditorProps): react.JSX.Element;
166
+ declare function RichTextEditor({ value, onChange, placeholder, invalid, id, className, classNames, toolbar, uploadImage, mediaApi, renderMediaPicker, resolveImageSrc, allowedEmbeds, labels, dir, }: RichTextEditorProps): React.JSX.Element;
167
+
168
+ /** Default toolbar layout; copy and edit it to pass your own `toolbar` prop. */
169
+ declare const defaultToolbar: readonly ToolbarItem[];
157
170
 
158
- export { type EmbedProvider, type Labels, type MediaApi, type MediaItem, type MediaListResult, type MediaPickerRenderProps, type MediaUploadResult, type MediaVariant, type PickedImage, RichTextEditor, type RichTextEditorProps, defaultLabels, isAllowedEmbedSrc, normalizeLinkUrl, toVideoEmbedUrl };
171
+ export { type EditorClassNames, type EmbedProvider, type Labels, type MediaApi, type MediaItem, type MediaListResult, type MediaPickerRenderProps, type MediaUploadResult, type MediaVariant, type PickedImage, RichTextEditor, type RichTextEditorProps, type ToolbarItem, defaultLabels, defaultToolbar, isAllowedEmbedSrc, normalizeLinkUrl, toVideoEmbedUrl };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as react from 'react';
1
+ import * as React from 'react';
2
2
  import { ReactNode } from 'react';
3
3
 
4
4
  /** All UI strings. Persian by default; pass `labels` to the editor to override any of them. */
@@ -71,6 +71,9 @@ type Labels = {
71
71
  [K in keyof typeof defaultLabels]: string;
72
72
  };
73
73
 
74
+ /** Extra class names per UI part; set through the editor's `classNames` prop. */
75
+ type EditorClassNames = Partial<Record<"root" | "toolbar" | "separator" | "button" | "content" | "tableTools" | "popover" | "dialog" | "input" | "label", string>>;
76
+
74
77
  type EmbedProvider = "aparat" | "youtube";
75
78
  /** Watch link (Aparat / YouTube) to a safe embed URL; anything else returns null. */
76
79
  declare function toVideoEmbedUrl(input: string, allowed?: readonly EmbedProvider[]): string | null;
@@ -126,6 +129,8 @@ type MediaPickerRenderProps = {
126
129
  close: () => void;
127
130
  };
128
131
 
132
+ /** Toolbar entries; "|" is a separator. */
133
+ type ToolbarItem = "bold" | "italic" | "underline" | "strike" | "textColor" | "highlight" | "h2" | "h3" | "h4" | "right" | "center" | "left" | "justify" | "bullet" | "ordered" | "quote" | "code" | "hr" | "link" | "unlink" | "image" | "video" | "table" | "undo" | "redo" | "|";
129
134
  type RichTextEditorProps = {
130
135
  /** HTML string. */
131
136
  value: string;
@@ -134,7 +139,12 @@ type RichTextEditorProps = {
134
139
  placeholder?: string;
135
140
  invalid?: boolean;
136
141
  id?: string;
142
+ /** Extra class on the root element (same as `classNames.root`). */
137
143
  className?: string;
144
+ /** Extra class names per part: root, toolbar, separator, button, content, tableTools, popover, dialog, input, label. Read once on mount for `content`. */
145
+ classNames?: EditorClassNames;
146
+ /** Which toolbar items to show, in order; "|" is a separator. Default: `defaultToolbar`. */
147
+ toolbar?: readonly ToolbarItem[];
138
148
  /** Enables the "Upload" button in the image dialog. Throw / reject to show an error. */
139
149
  uploadImage?: (file: File) => Promise<PickedImage>;
140
150
  /** Built-in media library picker. */
@@ -153,6 +163,9 @@ type RichTextEditorProps = {
153
163
  * Tiptap rich-text editor. Output is an HTML string; ALWAYS sanitize it on the server
154
164
  * (`@ilamanownuk/blog-kit/server`). Extensions are created once on mount.
155
165
  */
156
- declare function RichTextEditor({ value, onChange, placeholder, invalid, id, className, uploadImage, mediaApi, renderMediaPicker, resolveImageSrc, allowedEmbeds, labels, dir, }: RichTextEditorProps): react.JSX.Element;
166
+ declare function RichTextEditor({ value, onChange, placeholder, invalid, id, className, classNames, toolbar, uploadImage, mediaApi, renderMediaPicker, resolveImageSrc, allowedEmbeds, labels, dir, }: RichTextEditorProps): React.JSX.Element;
167
+
168
+ /** Default toolbar layout; copy and edit it to pass your own `toolbar` prop. */
169
+ declare const defaultToolbar: readonly ToolbarItem[];
157
170
 
158
- export { type EmbedProvider, type Labels, type MediaApi, type MediaItem, type MediaListResult, type MediaPickerRenderProps, type MediaUploadResult, type MediaVariant, type PickedImage, RichTextEditor, type RichTextEditorProps, defaultLabels, isAllowedEmbedSrc, normalizeLinkUrl, toVideoEmbedUrl };
171
+ export { type EditorClassNames, type EmbedProvider, type Labels, type MediaApi, type MediaItem, type MediaListResult, type MediaPickerRenderProps, type MediaUploadResult, type MediaVariant, type PickedImage, RichTextEditor, type RichTextEditorProps, type ToolbarItem, defaultLabels, defaultToolbar, isAllowedEmbedSrc, normalizeLinkUrl, toVideoEmbedUrl };
package/dist/index.js CHANGED
@@ -257,22 +257,27 @@ import {
257
257
  import * as React from "react";
258
258
  import { createPortal } from "react-dom";
259
259
  import { jsx, jsxs } from "react/jsx-runtime";
260
+ var ClassNamesContext = React.createContext({});
261
+ var useClassNames = () => React.useContext(ClassNamesContext);
260
262
  var cx = (...parts) => parts.filter(Boolean).join(" ");
261
263
  function Button({ variant = "ghost", size = "md", pressed, className, type = "button", ...rest }) {
264
+ const slots = useClassNames();
262
265
  return /* @__PURE__ */ jsx(
263
266
  "button",
264
267
  {
265
268
  type,
266
- className: cx("bk-btn", `bk-btn-${variant}`, `bk-btn-${size}`, pressed && "bk-pressed", className),
269
+ className: cx("bk-btn", `bk-btn-${variant}`, `bk-btn-${size}`, pressed && "bk-pressed", slots.button, className),
267
270
  ...rest
268
271
  }
269
272
  );
270
273
  }
271
274
  function Input(props) {
272
- return /* @__PURE__ */ jsx("input", { ...props, className: cx("bk-input", props.className) });
275
+ const slots = useClassNames();
276
+ return /* @__PURE__ */ jsx("input", { ...props, className: cx("bk-input", slots.input, props.className) });
273
277
  }
274
278
  function Label(props) {
275
- return /* @__PURE__ */ jsx("label", { ...props, className: cx("bk-label", props.className) });
279
+ const slots = useClassNames();
280
+ return /* @__PURE__ */ jsx("label", { ...props, className: cx("bk-label", slots.label, props.className) });
276
281
  }
277
282
  function Dialog({ open, ...rest }) {
278
283
  if (!open || typeof document === "undefined") return null;
@@ -280,6 +285,7 @@ function Dialog({ open, ...rest }) {
280
285
  }
281
286
  function DialogShell({ onClose, title, dir, wide, children }) {
282
287
  const ref = React.useRef(null);
288
+ const slots = useClassNames();
283
289
  React.useEffect(() => {
284
290
  const dialog = ref.current;
285
291
  if (dialog && !dialog.open) dialog.showModal();
@@ -290,7 +296,7 @@ function DialogShell({ onClose, title, dir, wide, children }) {
290
296
  ref,
291
297
  dir,
292
298
  "aria-label": title,
293
- className: cx("bk-dialog", wide && "bk-dialog-wide"),
299
+ className: cx("bk-dialog", wide && "bk-dialog-wide", slots.dialog),
294
300
  onClose,
295
301
  onClick: (event) => {
296
302
  if (event.target === event.currentTarget) onClose();
@@ -302,6 +308,7 @@ function DialogShell({ onClose, title, dir, wide, children }) {
302
308
  function Popover({ trigger, children }) {
303
309
  const [open, setOpen] = React.useState(false);
304
310
  const ref = React.useRef(null);
311
+ const slots = useClassNames();
305
312
  React.useEffect(() => {
306
313
  if (!open) return;
307
314
  const onDown = (event) => {
@@ -317,7 +324,7 @@ function Popover({ trigger, children }) {
317
324
  }, [open]);
318
325
  return /* @__PURE__ */ jsxs("span", { ref, className: "bk-popover", children: [
319
326
  trigger({ onClick: () => setOpen((value) => !value), open }),
320
- open ? /* @__PURE__ */ jsx("div", { className: "bk-popover-content", children }) : null
327
+ open ? /* @__PURE__ */ jsx("div", { className: cx("bk-popover-content", slots.popover), children }) : null
321
328
  ] });
322
329
  }
323
330
 
@@ -701,7 +708,7 @@ function UrlForm({
701
708
  }
702
709
 
703
710
  // src/editor/toolbar.tsx
704
- import { Fragment as Fragment3, jsx as jsx8, jsxs as jsxs8 } from "react/jsx-runtime";
711
+ import { jsx as jsx8, jsxs as jsxs8 } from "react/jsx-runtime";
705
712
  var chain = (editor) => editor.chain().focus();
706
713
  var GROUPS = [
707
714
  [
@@ -748,8 +755,53 @@ var TABLE_ACTIONS = [
748
755
  { key: "table-delete", label: "deleteTable", run: (e) => chain(e).deleteTable().run() }
749
756
  ];
750
757
  var ALL_BUTTONS = GROUPS.flat();
751
- function RichTextToolbar({ editor, dir, allowedEmbeds, ...imageProps }) {
758
+ var BUTTONS_BY_KEY = new Map(ALL_BUTTONS.map((b) => [b.key, b]));
759
+ var defaultToolbar = [
760
+ "bold",
761
+ "italic",
762
+ "underline",
763
+ "strike",
764
+ "textColor",
765
+ "highlight",
766
+ "|",
767
+ "h2",
768
+ "h3",
769
+ "h4",
770
+ "|",
771
+ "right",
772
+ "center",
773
+ "left",
774
+ "justify",
775
+ "|",
776
+ "bullet",
777
+ "ordered",
778
+ "quote",
779
+ "code",
780
+ "hr",
781
+ "|",
782
+ "link",
783
+ "unlink",
784
+ "image",
785
+ "video",
786
+ "table",
787
+ "|",
788
+ "undo",
789
+ "redo"
790
+ ];
791
+ function normalizeItems(items) {
792
+ const out = [];
793
+ for (const item of items) {
794
+ const valid = item === "|" || item === "textColor" || item === "highlight" || BUTTONS_BY_KEY.has(item);
795
+ if (!valid || item === "|" && (out.length === 0 || out[out.length - 1] === "|")) continue;
796
+ out.push(item);
797
+ }
798
+ while (out[out.length - 1] === "|") out.pop();
799
+ return out;
800
+ }
801
+ function RichTextToolbar({ editor, items = defaultToolbar, dir, allowedEmbeds, ...imageProps }) {
752
802
  const labels = useLabels();
803
+ const slots = useClassNames();
804
+ const entries = React6.useMemo(() => normalizeItems(items), [items]);
753
805
  const [dialog, setDialog] = React6.useState(null);
754
806
  const state = useEditorState({
755
807
  editor,
@@ -763,34 +815,31 @@ function RichTextToolbar({ editor, dir, allowedEmbeds, ...imageProps }) {
763
815
  })
764
816
  });
765
817
  const closeDialog = (open) => !open && setDialog(null);
766
- return /* @__PURE__ */ jsxs8("div", { className: "bk-toolbar", children: [
767
- GROUPS.map((group, index) => /* @__PURE__ */ jsxs8(React6.Fragment, { children: [
768
- index > 0 ? /* @__PURE__ */ jsx8("span", { className: "bk-sep", "aria-hidden": true }) : null,
769
- group.map((button) => {
770
- const label = labels[button.label];
771
- const active = state.active[button.key] ?? false;
772
- return /* @__PURE__ */ jsx8(
773
- Button,
774
- {
775
- size: "icon",
776
- pressed: active,
777
- "aria-label": label,
778
- title: label,
779
- "aria-pressed": button.isActive ? active : void 0,
780
- disabled: state.disabled[button.key],
781
- onMouseDown: (event) => event.preventDefault(),
782
- onClick: () => button.run(editor, setDialog),
783
- children: /* @__PURE__ */ jsx8(button.icon, { size: 16 })
784
- },
785
- button.key
786
- );
787
- }),
788
- index === 0 ? /* @__PURE__ */ jsxs8(Fragment3, { children: [
789
- /* @__PURE__ */ jsx8(ColorMenu, { editor, mode: "text", icon: Palette, active: state.color }),
790
- /* @__PURE__ */ jsx8(ColorMenu, { editor, mode: "highlight", icon: Highlighter, active: state.highlight })
791
- ] }) : null
792
- ] }, group[0].key)),
793
- state.inTable ? /* @__PURE__ */ jsx8("div", { className: "bk-table-tools", children: TABLE_ACTIONS.map((action) => /* @__PURE__ */ jsx8(Button, { variant: "outline", size: "sm", onMouseDown: (event) => event.preventDefault(), onClick: () => action.run(editor), children: labels[action.label] }, action.key)) }) : null,
818
+ return /* @__PURE__ */ jsxs8("div", { className: cx("bk-toolbar", slots.toolbar), children: [
819
+ entries.map((item, index) => {
820
+ if (item === "|") return /* @__PURE__ */ jsx8("span", { className: cx("bk-sep", slots.separator), "aria-hidden": true }, `sep-${index}`);
821
+ if (item === "textColor") return /* @__PURE__ */ jsx8(ColorMenu, { editor, mode: "text", icon: Palette, active: state.color }, item);
822
+ if (item === "highlight") return /* @__PURE__ */ jsx8(ColorMenu, { editor, mode: "highlight", icon: Highlighter, active: state.highlight }, item);
823
+ const button = BUTTONS_BY_KEY.get(item);
824
+ const label = labels[button.label];
825
+ const active = state.active[button.key] ?? false;
826
+ return /* @__PURE__ */ jsx8(
827
+ Button,
828
+ {
829
+ size: "icon",
830
+ pressed: active,
831
+ "aria-label": label,
832
+ title: label,
833
+ "aria-pressed": button.isActive ? active : void 0,
834
+ disabled: state.disabled[button.key],
835
+ onMouseDown: (event) => event.preventDefault(),
836
+ onClick: () => button.run(editor, setDialog),
837
+ children: /* @__PURE__ */ jsx8(button.icon, { size: 16 })
838
+ },
839
+ button.key
840
+ );
841
+ }),
842
+ state.inTable ? /* @__PURE__ */ jsx8("div", { className: cx("bk-table-tools", slots.tableTools), children: TABLE_ACTIONS.map((action) => /* @__PURE__ */ jsx8(Button, { variant: "outline", size: "sm", onMouseDown: (event) => event.preventDefault(), onClick: () => action.run(editor), children: labels[action.label] }, action.key)) }) : null,
794
843
  /* @__PURE__ */ jsx8(
795
844
  UrlDialog,
796
845
  {
@@ -835,6 +884,7 @@ function RichTextToolbar({ editor, dir, allowedEmbeds, ...imageProps }) {
835
884
 
836
885
  // src/editor/rich-text-editor.tsx
837
886
  import { jsx as jsx9, jsxs as jsxs9 } from "react/jsx-runtime";
887
+ var EMPTY = {};
838
888
  function RichTextEditor({
839
889
  value,
840
890
  onChange,
@@ -842,6 +892,8 @@ function RichTextEditor({
842
892
  invalid,
843
893
  id,
844
894
  className,
895
+ classNames,
896
+ toolbar,
845
897
  uploadImage,
846
898
  mediaApi,
847
899
  renderMediaPicker,
@@ -859,7 +911,7 @@ function RichTextEditor({
859
911
  immediatelyRender: false,
860
912
  // SSR-safe
861
913
  editorProps: {
862
- attributes: { class: "bk-rich bk-editor-content", dir, ...id ? { id } : {} }
914
+ attributes: { class: cx("bk-rich bk-editor-content", classNames?.content), dir, ...id ? { id } : {} }
863
915
  },
864
916
  onUpdate: ({ editor: e }) => onChangeRef.current(e.isEmpty ? "" : e.getHTML())
865
917
  });
@@ -868,12 +920,13 @@ function RichTextEditor({
868
920
  const current = editor.isEmpty ? "" : editor.getHTML();
869
921
  if (value !== current) editor.commands.setContent(value, { emitUpdate: false });
870
922
  }, [editor, value]);
871
- return /* @__PURE__ */ jsx9(LabelsContext.Provider, { value: merged, children: /* @__PURE__ */ jsxs9("div", { className: cx("bk-editor", invalid && "bk-invalid", className), dir, children: [
923
+ return /* @__PURE__ */ jsx9(LabelsContext.Provider, { value: merged, children: /* @__PURE__ */ jsx9(ClassNamesContext.Provider, { value: classNames ?? EMPTY, children: /* @__PURE__ */ jsxs9("div", { className: cx("bk-editor", invalid && "bk-invalid", classNames?.root, className), dir, children: [
872
924
  editor ? /* @__PURE__ */ jsx9(
873
925
  RichTextToolbar,
874
926
  {
875
927
  editor,
876
928
  dir,
929
+ items: toolbar,
877
930
  allowedEmbeds,
878
931
  uploadImage,
879
932
  mediaApi,
@@ -882,11 +935,12 @@ function RichTextEditor({
882
935
  }
883
936
  ) : null,
884
937
  /* @__PURE__ */ jsx9(EditorContent, { editor })
885
- ] }) });
938
+ ] }) }) });
886
939
  }
887
940
  export {
888
941
  RichTextEditor,
889
942
  defaultLabels,
943
+ defaultToolbar,
890
944
  isAllowedEmbedSrc,
891
945
  normalizeLinkUrl,
892
946
  toVideoEmbedUrl