@arkyn/components 1.3.116 → 1.3.117

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,9 +6,10 @@ type RichTextProps = {
6
6
  enforceCharacterLimit?: boolean;
7
7
  defaultValue?: string;
8
8
  isError?: boolean;
9
+ onChangeCharactersCount: (e: number) => void;
9
10
  onChange?: (value: Descendant[]) => void;
10
11
  onValueChange?: (value: string) => void;
11
12
  };
12
- declare function RichText({ name, defaultValue, enforceCharacterLimit, maxLimit, onValueChange, onChange, isError: baseIsError, }: RichTextProps): import("react/jsx-runtime").JSX.Element;
13
+ declare function RichText({ name, defaultValue, enforceCharacterLimit, onChangeCharactersCount, maxLimit, onValueChange, onChange, isError: baseIsError, }: RichTextProps): import("react/jsx-runtime").JSX.Element;
13
14
  export { RichText };
14
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/RichText/index.tsx"],"names":[],"mappings":"AAeA,OAAO,EAAgB,UAAU,EAAoB,MAAM,OAAO,CAAC;AAanE,OAAO,cAAc,CAAC;AAGtB,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,IAAI,CAAC;IACzC,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC,CAAC;AAEF,iBAAS,QAAQ,CAAC,EAChB,IAAI,EACJ,YAAY,EACZ,qBAA6B,EAC7B,QAAe,EACf,aAAa,EACb,QAAQ,EACR,OAAO,EAAE,WAAW,GACrB,EAAE,aAAa,2CA8Gf;AAED,OAAO,EAAE,QAAQ,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/RichText/index.tsx"],"names":[],"mappings":"AAeA,OAAO,EAAgB,UAAU,EAAoB,MAAM,OAAO,CAAC;AAanE,OAAO,cAAc,CAAC;AAGtB,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,uBAAuB,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7C,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,IAAI,CAAC;IACzC,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC,CAAC;AAEF,iBAAS,QAAQ,CAAC,EAChB,IAAI,EACJ,YAAY,EACZ,qBAA6B,EAC7B,uBAAuB,EACvB,QAAe,EACf,aAAa,EACb,QAAQ,EACR,OAAO,EAAE,WAAW,GACrB,EAAE,aAAa,2CAkHf;AAED,OAAO,EAAE,QAAQ,EAAE,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import isHotkey from "is-hotkey";
2
3
  import { AlignCenter, AlignJustify, AlignLeft, AlignRight, Bold, Code, Heading1, Heading2, Italic, Quote, Underline, } from "lucide-react";
3
4
  import { useCallback, useMemo, useRef, useState } from "react";
4
5
  import { createEditor, Transforms, Node } from "slate";
@@ -9,10 +10,12 @@ import { Element } from "./components/Element";
9
10
  import { Leaf } from "./components/Leaf";
10
11
  import { MarkButton } from "./components/MarkButton";
11
12
  import { Toolbar } from "./components/Toolbar";
13
+ import { toggleMark } from "./functions/toggleMark";
14
+ import { HOTKEYS } from "./template/HOTKEYS";
12
15
  import { INITIAL_VALUE } from "./template/INITIAL_VALUE";
13
16
  import "./styles.css";
14
17
  import { useFormController } from "../Form/FormController";
15
- function RichText({ name, defaultValue, enforceCharacterLimit = false, maxLimit = 2000, onValueChange, onChange, isError: baseIsError, }) {
18
+ function RichText({ name, defaultValue, enforceCharacterLimit = false, onChangeCharactersCount, maxLimit = 2000, onValueChange, onChange, isError: baseIsError, }) {
16
19
  const [charactersCount, setCharactersCount] = useState(0);
17
20
  const [editorValue, setEditorValue] = useState(defaultValue ? JSON.parse(defaultValue) : INITIAL_VALUE);
18
21
  const [onFocus, setOnFocus] = useState(false);
@@ -29,6 +32,7 @@ function RichText({ name, defaultValue, enforceCharacterLimit = false, maxLimit
29
32
  function handleChange(value) {
30
33
  const text = extractText(value);
31
34
  setCharactersCount(text.length);
35
+ onChangeCharactersCount && onChangeCharactersCount(text.length);
32
36
  if (enforceCharacterLimit && text.length >= maxLimit) {
33
37
  return;
34
38
  }
@@ -48,6 +52,14 @@ function RichText({ name, defaultValue, enforceCharacterLimit = false, maxLimit
48
52
  : "errorFalse";
49
53
  const className = `arkynRichText ${errorClass} ${focusClass}`;
50
54
  const restatesCharacters = maxLimit - charactersCount;
51
- return (_jsxs(Slate, { editor: editor, initialValue: defaultValue ? JSON.parse(defaultValue) : INITIAL_VALUE, onChange: handleChange, onValueChange: handleChange, children: [_jsxs("div", { className: className, children: [_jsxs(Toolbar, { children: [_jsx(BlockButton, { format: "headingOne", icon: Heading1 }), _jsx(BlockButton, { format: "headingTwo", icon: Heading2 }), _jsx(BlockButton, { format: "blockQuote", icon: Quote }), _jsx(MarkButton, { format: "bold", icon: Bold }), _jsx(MarkButton, { format: "italic", icon: Italic }), _jsx(MarkButton, { format: "underline", icon: Underline }), _jsx(MarkButton, { format: "code", icon: Code }), _jsx(BlockButton, { format: "left", icon: AlignLeft }), _jsx(BlockButton, { format: "right", icon: AlignRight }), _jsx(BlockButton, { format: "center", icon: AlignCenter }), _jsx(BlockButton, { format: "justify", icon: AlignJustify })] }), _jsx(Editable, { className: "editorContainer", renderElement: renderElement, renderLeaf: renderLeaf, spellCheck: true, id: id, onFocus: () => setOnFocus(true), onBlur: () => setOnFocus(false) }), restatesCharacters < 0 && (_jsx("div", { className: "restatesCharacters", children: restatesCharacters }))] }), _jsx("input", { ref: ref, type: "hidden", name: name, value: JSON.stringify(editorValue) })] }));
55
+ return (_jsxs(Slate, { editor: editor, initialValue: defaultValue ? JSON.parse(defaultValue) : INITIAL_VALUE, onChange: handleChange, onValueChange: handleChange, children: [_jsxs("div", { className: className, children: [_jsxs(Toolbar, { children: [_jsx(BlockButton, { format: "headingOne", icon: Heading1 }), _jsx(BlockButton, { format: "headingTwo", icon: Heading2 }), _jsx(BlockButton, { format: "blockQuote", icon: Quote }), _jsx(MarkButton, { format: "bold", icon: Bold }), _jsx(MarkButton, { format: "italic", icon: Italic }), _jsx(MarkButton, { format: "underline", icon: Underline }), _jsx(MarkButton, { format: "code", icon: Code }), _jsx(BlockButton, { format: "left", icon: AlignLeft }), _jsx(BlockButton, { format: "right", icon: AlignRight }), _jsx(BlockButton, { format: "center", icon: AlignCenter }), _jsx(BlockButton, { format: "justify", icon: AlignJustify })] }), _jsx(Editable, { className: "editorContainer", renderElement: renderElement, renderLeaf: renderLeaf, spellCheck: true, id: id, onFocus: () => setOnFocus(true), onBlur: () => setOnFocus(false), onKeyDown: (event) => {
56
+ for (const hotkey in HOTKEYS) {
57
+ if (isHotkey(hotkey, event)) {
58
+ event.preventDefault();
59
+ const mark = HOTKEYS[hotkey];
60
+ toggleMark(editor, mark);
61
+ }
62
+ }
63
+ } }), restatesCharacters < 0 && (_jsx("div", { className: "restatesCharacters", children: restatesCharacters }))] }), _jsx("input", { ref: ref, type: "hidden", name: name, value: JSON.stringify(editorValue) }), _jsx("input", { type: "hidden", name: `${name}Count`, value: charactersCount })] }));
52
64
  }
53
65
  export { RichText };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/components",
3
- "version": "1.3.116",
3
+ "version": "1.3.117",
4
4
  "main": "./dist/bundle.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Lucas Gonçalves",
@@ -35,6 +35,7 @@ type RichTextProps = {
35
35
  enforceCharacterLimit?: boolean;
36
36
  defaultValue?: string;
37
37
  isError?: boolean;
38
+ onChangeCharactersCount: (e: number) => void;
38
39
  onChange?: (value: Descendant[]) => void;
39
40
  onValueChange?: (value: string) => void;
40
41
  };
@@ -43,6 +44,7 @@ function RichText({
43
44
  name,
44
45
  defaultValue,
45
46
  enforceCharacterLimit = false,
47
+ onChangeCharactersCount,
46
48
  maxLimit = 2000,
47
49
  onValueChange,
48
50
  onChange,
@@ -74,6 +76,8 @@ function RichText({
74
76
  const text = extractText(value);
75
77
  setCharactersCount(text.length);
76
78
 
79
+ onChangeCharactersCount && onChangeCharactersCount(text.length);
80
+
77
81
  if (enforceCharacterLimit && text.length >= maxLimit) {
78
82
  return;
79
83
  } else {
@@ -133,15 +137,15 @@ function RichText({
133
137
  id={id}
134
138
  onFocus={() => setOnFocus(true)}
135
139
  onBlur={() => setOnFocus(false)}
136
- // onKeyDown={(event) => {
137
- // for (const hotkey in HOTKEYS) {
138
- // if (isHotkey(hotkey, event as any)) {
139
- // event.preventDefault();
140
- // const mark = HOTKEYS[hotkey as keyof typeof HOTKEYS];
141
- // toggleMark(editor, mark);
142
- // }
143
- // }
144
- // }}
140
+ onKeyDown={(event) => {
141
+ for (const hotkey in HOTKEYS) {
142
+ if (isHotkey(hotkey, event as any)) {
143
+ event.preventDefault();
144
+ const mark = HOTKEYS[hotkey as keyof typeof HOTKEYS];
145
+ toggleMark(editor, mark);
146
+ }
147
+ }
148
+ }}
145
149
  />
146
150
 
147
151
  {restatesCharacters < 0 && (
@@ -155,6 +159,8 @@ function RichText({
155
159
  name={name}
156
160
  value={JSON.stringify(editorValue)}
157
161
  />
162
+
163
+ <input type="hidden" name={`${name}Count`} value={charactersCount} />
158
164
  </Slate>
159
165
  );
160
166
  }