@arkitektbedriftene/fe-lib 0.3.9 → 0.3.11

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.
@@ -1,10 +1,13 @@
1
- import { type ReactNode } from "react";
1
+ import { type ReactNode, type ReactElement } from "react";
2
2
  import { type InitialConfigType } from "@lexical/react/LexicalComposer";
3
- export declare const RichTextEditor: ({ isLoading, children, placeholderText, nodes, plugins, toolbar, }: {
3
+ export declare const RichTextEditor: ({ isLoading, children, placeholderText, nodes, plugins, toolbar, content, hideBorder, onBlur, }: {
4
4
  isLoading: boolean;
5
5
  children: ReactNode;
6
6
  placeholderText?: string | undefined;
7
7
  nodes: InitialConfigType["nodes"];
8
8
  plugins?: ReactNode;
9
9
  toolbar?: ReactNode;
10
+ content: ReactElement;
11
+ hideBorder?: boolean | undefined;
12
+ onBlur?: (() => void) | undefined;
10
13
  }) => JSX.Element;
@@ -1,61 +1,60 @@
1
- import { createHeadlessEditor as C } from "@lexical/headless";
2
- import { $generateHtmlFromNodes as N } from "@lexical/html";
3
- import { $getRoot as p, ParagraphNode as T, $createParagraphNode as y, $createTextNode as $ } from "lexical";
4
- import { trimTextContentFromAnchor as S } from "@lexical/selection";
5
- import { AutoLinkNode as E, LinkNode as v } from "@lexical/link";
6
- import { ListNode as w, ListItemNode as j } from "@lexical/list";
7
- import { HeadingNode as F, QuoteNode as L } from "@lexical/rich-text";
8
- import { TableNode as R, TableRowNode as H, TableCellNode as k } from "@lexical/table";
9
- import { j as i } from "./jsx-runtime-d0aa4c5b.js";
10
- import { createContext as P, useState as B, useRef as h, useCallback as u, useContext as I, useMemo as M } from "react";
11
- import { c as m, s as f, B as x, S as z } from "./Popover-2318823a.js";
12
- import { LexicalComposer as q } from "@lexical/react/LexicalComposer";
13
- import { RichTextPlugin as A } from "@lexical/react/LexicalRichTextPlugin";
14
- import { ContentEditable as D } from "@lexical/react/LexicalContentEditable";
15
- import V from "@lexical/react/LexicalErrorBoundary";
1
+ import { createHeadlessEditor as T } from "@lexical/headless";
2
+ import { $generateHtmlFromNodes as y } from "@lexical/html";
3
+ import { $getRoot as p, ParagraphNode as S, $createParagraphNode as E, $createTextNode as $ } from "lexical";
4
+ import { trimTextContentFromAnchor as F } from "@lexical/selection";
5
+ import { AutoLinkNode as L, LinkNode as j } from "@lexical/link";
6
+ import { ListNode as w, ListItemNode as v } from "@lexical/list";
7
+ import { HeadingNode as R, QuoteNode as H } from "@lexical/rich-text";
8
+ import { TableNode as k, TableRowNode as P, TableCellNode as I } from "@lexical/table";
9
+ import { j as c } from "./jsx-runtime-d0aa4c5b.js";
10
+ import { createContext as M, useState as B, useRef as f, useCallback as h, useContext as q, useMemo as z } from "react";
11
+ import { c as d, s as A, B as x, S as D } from "./Popover-2318823a.js";
12
+ import { LexicalComposer as V } from "@lexical/react/LexicalComposer";
13
+ import { RichTextPlugin as W } from "@lexical/react/LexicalRichTextPlugin";
14
+ import J from "@lexical/react/LexicalErrorBoundary";
16
15
  import "react-dom";
17
- const W = ({
16
+ const O = ({
18
17
  text: e,
19
- maxChars: a,
20
- maxLines: n
18
+ maxChars: i,
19
+ maxLines: s
21
20
  }) => {
22
21
  if (e.length === 0)
23
22
  return 0;
24
- const r = typeof n == "number", o = typeof a == "number";
23
+ const r = typeof s == "number", o = typeof i == "number";
25
24
  if (!r && !o)
26
25
  return e.length;
27
- let s = 0, t = 0;
28
- for (; t < e.length && (!r || s < n) && (!o || t < a); )
26
+ let n = 0, t = 0;
27
+ for (; t < e.length && (!r || n < s) && (!o || t < i); )
29
28
  e[t] === `
30
- ` && s++, t++;
29
+ ` && n++, t++;
31
30
  return e.slice(0, t).length;
32
- }, J = ({
31
+ }, Q = ({
33
32
  editor: e,
34
- maxChars: a,
35
- maxLines: n
33
+ maxChars: i,
34
+ maxLines: s
36
35
  }) => {
37
- const r = p(), o = r.getTextContent(), s = W({ text: o, maxChars: a, maxLines: n }), t = o.length - s, l = r.select().anchor;
38
- S(e, l, t);
39
- const d = r.getLastChild();
40
- return d instanceof T && d.getChildrenSize() === 0 && d.remove(), t;
41
- }, O = (e) => e[0] === "{", ge = (e, a, n) => {
36
+ const r = p(), o = r.getTextContent(), n = O({ text: o, maxChars: i, maxLines: s }), t = o.length - n, m = r.select().anchor;
37
+ F(e, m, t);
38
+ const a = r.getLastChild();
39
+ return a instanceof S && a.getChildrenSize() === 0 && a.remove(), t;
40
+ }, G = (e) => e[0] === "{", ge = (e, i, s) => {
42
41
  let r = 0;
43
- const o = C({
44
- nodes: a,
42
+ const o = T({
43
+ nodes: i,
45
44
  editable: !1
46
45
  });
47
46
  if (e)
48
47
  try {
49
- if (typeof e == "string" && !O(e))
48
+ if (typeof e == "string" && !G(e))
50
49
  o.update(() => {
51
- const t = p(), c = y();
52
- c.append($(e.trim())), t.append(c);
50
+ const t = p(), l = E();
51
+ l.append($(e.trim())), t.append(l);
53
52
  });
54
53
  else {
55
54
  const t = o.parseEditorState(e, () => {
56
- r = J({
55
+ r = Q({
57
56
  editor: o,
58
- maxLines: n == null ? void 0 : n.maxLines
57
+ maxLines: s == null ? void 0 : s.maxLines
59
58
  });
60
59
  });
61
60
  t.isEmpty() || o.setEditorState(t);
@@ -63,62 +62,62 @@ const W = ({
63
62
  } catch (t) {
64
63
  console.error(t);
65
64
  }
66
- let s = "";
65
+ let n = "";
67
66
  return o.update(() => {
68
- s = N(o);
69
- }), { html: s, trimCount: r };
67
+ n = y(o);
68
+ }), { html: n, trimCount: r };
70
69
  }, Ce = [
71
- F,
72
- L,
73
- w,
74
- j,
75
- E,
76
- v,
77
70
  R,
78
71
  H,
79
- k
80
- ], Q = m({
72
+ w,
73
+ v,
74
+ L,
75
+ j,
76
+ k,
77
+ P,
78
+ I
79
+ ], K = d({
81
80
  fontWeight: "bold"
82
- }), Y = m({
81
+ }), U = d({
83
82
  fontStyle: "italic"
84
- }), G = m({
83
+ }), X = d({
85
84
  textDecoration: "underline"
86
- }), K = m({
85
+ }), Y = d({
87
86
  listStyleType: "none"
88
- }), U = m({
87
+ }), Z = d({
89
88
  borderLeft: "4px solid #ccc",
90
89
  color: "#666",
91
90
  fontStyle: "italic",
92
91
  margin: "1.5em 10px",
93
92
  padding: "0.5em 10px"
94
- }), X = {
95
- quote: U().className,
93
+ }), _ = {
94
+ quote: Z().className,
96
95
  text: {
97
- bold: Q().className,
98
- italic: Y().className,
99
- underline: G().className
96
+ bold: K().className,
97
+ italic: U().className,
98
+ underline: X().className
100
99
  },
101
100
  list: {
102
101
  nested: {
103
- listitem: K().className
102
+ listitem: Y().className
104
103
  }
105
104
  }
106
- }, b = P({ hasFocus: !1, editorRef: { current: null } }), Z = ({ onBlur: e }) => {
107
- const [a, n] = B(!1), r = h(null), o = u(() => {
108
- n(!0), r.current && window.clearTimeout(r.current);
109
- }, []), s = u(() => {
105
+ }, b = M({ hasFocus: !1, editorRef: { current: null } }), ee = ({ onBlur: e }) => {
106
+ const [i, s] = B(!1), r = f(null), o = h(() => {
107
+ s(!0), r.current && window.clearTimeout(r.current);
108
+ }, []), n = h(() => {
110
109
  r.current = window.setTimeout(() => {
111
- n(!1), e == null || e();
110
+ s(!1), e == null || e();
112
111
  }, 0);
113
112
  }, [e]);
114
113
  return {
115
- hasFocus: a,
114
+ hasFocus: i,
116
115
  attributes: {
117
116
  onFocus: o,
118
- onBlur: s
117
+ onBlur: n
119
118
  }
120
119
  };
121
- }, Ne = () => I(b), _ = f("div", {
120
+ }, Ne = () => q(b), te = A("div", {
122
121
  border: "1px solid $borderDarker",
123
122
  borderRadius: "$md",
124
123
  position: "relative",
@@ -148,33 +147,9 @@ const W = ({
148
147
  }
149
148
  }
150
149
  ]
151
- }), ee = f(D, {}, {
152
- padding: "0 $4",
153
- overflowY: "auto",
154
- maxHeight: "30rem",
155
- // backgroundColor: "white",
156
- "&:focus-visible": {
157
- outline: "none"
158
- },
159
- ".main-content": {
160
- padding: 12,
161
- border: "1px solid $gray100",
162
- borderRadius: "$md",
163
- position: "relative",
164
- "&::before": {
165
- content: "Hovedinnhold",
166
- position: "absolute",
167
- top: -10,
168
- left: 16,
169
- backgroundColor: "white",
170
- padding: "0 $1",
171
- fontSize: 12,
172
- color: "$gray500"
173
- }
174
- }
175
- }), te = ({
150
+ }), oe = ({
176
151
  isLoading: e
177
- }) => /* @__PURE__ */ i.jsx(
152
+ }) => /* @__PURE__ */ c.jsx(
178
153
  x,
179
154
  {
180
155
  css: {
@@ -184,50 +159,46 @@ const W = ({
184
159
  right: "1rem",
185
160
  display: "flex"
186
161
  },
187
- children: /* @__PURE__ */ i.jsx(z, {})
162
+ children: /* @__PURE__ */ c.jsx(D, {})
188
163
  }
189
164
  ), Te = ({
190
165
  isLoading: e,
191
- children: a,
192
- placeholderText: n,
166
+ children: i,
167
+ placeholderText: s,
193
168
  nodes: r,
194
169
  plugins: o,
195
- toolbar: s
170
+ toolbar: n,
171
+ content: t,
172
+ hideBorder: l,
173
+ onBlur: m
196
174
  }) => {
197
- const { hasFocus: t, attributes: c } = Z({}), l = h(null), d = M(() => ({ hasFocus: t, editorRef: l }), [t]);
198
- return /* @__PURE__ */ i.jsx(b.Provider, { value: d, children: /* @__PURE__ */ i.jsxs(
199
- q,
175
+ const { hasFocus: a, attributes: g } = ee({ onBlur: m }), u = f(null), C = z(() => ({ hasFocus: a, editorRef: u }), [a]);
176
+ return /* @__PURE__ */ c.jsx(b.Provider, { value: C, children: /* @__PURE__ */ c.jsxs(
177
+ V,
200
178
  {
201
179
  initialConfig: {
202
180
  namespace: "CommentEditor",
203
- onError: (g) => {
204
- console.error(g);
181
+ onError: (N) => {
182
+ console.error(N);
205
183
  },
206
- theme: X,
184
+ theme: _,
207
185
  nodes: r,
208
186
  editable: !0
209
187
  },
210
188
  children: [
211
- /* @__PURE__ */ i.jsxs(
212
- _,
189
+ /* @__PURE__ */ c.jsxs(
190
+ te,
213
191
  {
214
- ref: l,
215
- hasFocus: t,
216
- ...c,
192
+ ref: u,
193
+ hasFocus: a,
194
+ hideBorder: l,
195
+ ...g,
217
196
  children: [
218
- /* @__PURE__ */ i.jsx(
219
- A,
197
+ /* @__PURE__ */ c.jsx(
198
+ W,
220
199
  {
221
- contentEditable: /* @__PURE__ */ i.jsx(
222
- ee,
223
- {
224
- css: {
225
- paddingTop: 16,
226
- paddingBottom: 16
227
- }
228
- }
229
- ),
230
- placeholder: n ? /* @__PURE__ */ i.jsx(
200
+ contentEditable: t,
201
+ placeholder: s ? /* @__PURE__ */ c.jsx(
231
202
  x,
232
203
  {
233
204
  css: {
@@ -238,19 +209,19 @@ const W = ({
238
209
  pointerEvents: "none",
239
210
  fontSize: "$sm"
240
211
  },
241
- children: n
212
+ children: s
242
213
  }
243
214
  ) : null,
244
- ErrorBoundary: V
215
+ ErrorBoundary: J
245
216
  }
246
217
  ),
247
218
  o,
248
- /* @__PURE__ */ i.jsx(te, { isLoading: e }),
249
- s
219
+ /* @__PURE__ */ c.jsx(oe, { isLoading: e }),
220
+ n
250
221
  ]
251
222
  }
252
223
  ),
253
- /* @__PURE__ */ i.jsx(i.Fragment, { children: a })
224
+ /* @__PURE__ */ c.jsx(c.Fragment, { children: i })
254
225
  ]
255
226
  }
256
227
  ) });
@@ -258,9 +229,9 @@ const W = ({
258
229
  export {
259
230
  Te as RichTextEditor,
260
231
  Ce as defaultNodes,
261
- O as isJSON,
232
+ G as isJSON,
262
233
  b as richTextContext,
263
234
  ge as stateToHTML,
264
- Z as useHasFocusWithin,
235
+ ee as useHasFocusWithin,
265
236
  Ne as useRichTextContext
266
237
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkitektbedriftene/fe-lib",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
4
4
  "type": "module",
5
5
  "main": "./dist/index.umd.cjs",
6
6
  "module": "./dist/index.es.js",
@@ -1,8 +1,7 @@
1
- import { useRef, type ReactNode, useMemo } from "react";
1
+ import { useRef, type ReactNode, useMemo, type ReactElement } from "react";
2
2
  import { Box, Spinner, styled } from "../ui/ui";
3
3
  import { type InitialConfigType, LexicalComposer } from "@lexical/react/LexicalComposer";
4
4
  import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
5
- import { ContentEditable } from "@lexical/react/LexicalContentEditable";
6
5
  import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
7
6
  import { lexicalTheme } from "./theme";
8
7
  import { useHasFocusWithin, richTextContext } from "./editorContext";
@@ -42,34 +41,6 @@ const Container = styled("div", {
42
41
  ],
43
42
  });
44
43
 
45
- const NewText = styled(ContentEditable, {}, {
46
- padding: "0 $4",
47
- overflowY: "auto",
48
- maxHeight: "30rem",
49
- // backgroundColor: "white",
50
- "&:focus-visible": {
51
- outline: "none",
52
- },
53
-
54
- ".main-content": {
55
- padding: 12,
56
- border: "1px solid $gray100",
57
- borderRadius: "$md",
58
- position: "relative",
59
-
60
- "&::before": {
61
- content: "Hovedinnhold",
62
- position: "absolute",
63
- top: -10,
64
- left: 16,
65
- backgroundColor: "white",
66
- padding: "0 $1",
67
- fontSize: 12,
68
- color: "$gray500",
69
- },
70
- },
71
- });
72
-
73
44
  const EditorSpinner = ({
74
45
  isLoading,
75
46
  }: {
@@ -97,6 +68,9 @@ export const RichTextEditor = ({
97
68
  nodes,
98
69
  plugins,
99
70
  toolbar,
71
+ content,
72
+ hideBorder,
73
+ onBlur,
100
74
  }: {
101
75
  isLoading: boolean;
102
76
  children: ReactNode;
@@ -104,8 +78,11 @@ export const RichTextEditor = ({
104
78
  nodes: InitialConfigType["nodes"];
105
79
  plugins?: ReactNode;
106
80
  toolbar?: ReactNode;
81
+ content: ReactElement;
82
+ hideBorder?: boolean;
83
+ onBlur?: () => void;
107
84
  }) => {
108
- const { hasFocus, attributes } = useHasFocusWithin({});
85
+ const { hasFocus, attributes } = useHasFocusWithin({ onBlur });
109
86
  const editorRef = useRef<HTMLDivElement>(null);
110
87
  const contextValue = useMemo(() => ({ hasFocus, editorRef }), [hasFocus]);
111
88
 
@@ -125,17 +102,11 @@ export const RichTextEditor = ({
125
102
  <Container
126
103
  ref={editorRef}
127
104
  hasFocus={hasFocus}
105
+ hideBorder={hideBorder}
128
106
  {...attributes}
129
107
  >
130
108
  <RichTextPlugin
131
- contentEditable={
132
- <NewText
133
- css={{
134
- paddingTop: 16,
135
- paddingBottom: 16,
136
- }}
137
- />
138
- }
109
+ contentEditable={content}
139
110
  placeholder={
140
111
  placeholderText ? (
141
112
  <Box