@vonaffenfels/slate-editor 1.1.37 → 1.1.40

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vonaffenfels/slate-editor",
3
- "version": "1.1.37",
3
+ "version": "1.1.40",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -72,7 +72,7 @@
72
72
  "cssnano": "^5.0.1",
73
73
  "escape-html": "^1.0.3"
74
74
  },
75
- "gitHead": "23bd1926bceee0ce73a5d25f021c544ead009f77",
75
+ "gitHead": "6b708b3a2b8b1e61077b6d504dd94ec9665b47f4",
76
76
  "publishConfig": {
77
77
  "access": "public"
78
78
  }
@@ -37,6 +37,7 @@ export const Element = ({
37
37
  switch (props.element.type) {
38
38
  case 'storybook':
39
39
  return <div
40
+ id={props?.element?.attributes?.id}
40
41
  onClick={e => e.stopPropagation()}
41
42
  className={"storybook-element-wrapper"}>
42
43
  <StorybookNode
@@ -0,0 +1,95 @@
1
+ import {
2
+ ReactEditor, useSlate,
3
+ } from "slate-react";
4
+ import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
5
+ import React, {
6
+ useEffect, useState,
7
+ } from "react";
8
+ import classNames from "classnames";
9
+ import {
10
+ Editor, Transforms, Element as SlateElement, Range, Node,
11
+ } from "slate";
12
+ import {
13
+ faAnchor, faLink,
14
+ } from "@fortawesome/free-solid-svg-icons";
15
+
16
+ export const AnchorButtonElement = ({
17
+ icon,
18
+ children,
19
+ }) => {
20
+ const editor = useSlate();
21
+
22
+ const [anchor, setAnchor] = useState("");
23
+ const [node, setNode] = useState(undefined);
24
+
25
+ useEffect(() => {
26
+ if (editor?.selection?.anchor?.path?.[0] !== undefined) {
27
+ let node = Node.get(editor, [editor?.selection?.anchor?.path?.[0]]);
28
+
29
+ setAnchor(node?.attributes?.id || "");
30
+ setNode(node);
31
+ }
32
+ }, [editor?.selection?.anchor?.path?.[0], editor]);
33
+
34
+ const apply = (value) => {
35
+ // Node.get could return editor itself
36
+ if (!node?.type) {
37
+ return;
38
+ }
39
+
40
+ let path = ReactEditor.findPath(editor, node);
41
+
42
+ let newNode = {
43
+ ...node,
44
+ attributes: {
45
+ ...node.attributes,
46
+ id: anchor,
47
+ },
48
+ };
49
+
50
+ setNode(newNode);
51
+
52
+ Transforms.setNodes(editor, newNode, {at: path});
53
+ };
54
+
55
+ const reset = () => {
56
+ apply("");
57
+
58
+ return;
59
+ };
60
+
61
+ return (
62
+ <span
63
+ className={
64
+ classNames({
65
+ "toolbar-btn": true,
66
+ "active": node?.attributes?.id,
67
+ })
68
+ }
69
+ >
70
+ {!!icon && <FontAwesomeIcon icon={icon} />}
71
+ {children}
72
+ <span className="toolbar-btn-config">
73
+ <span className="toolbar-btn-config-inner">
74
+ <span className="toolbar-btn-config-row">
75
+ <label className="toolbar-form-label">
76
+ Ankername
77
+ </label>
78
+ <span className="toolbar-form-input">
79
+ <input type="text" value={anchor} onChange={(e) => setAnchor(e.target.value)}/>
80
+ </span>
81
+ </span>
82
+ <span className="toolbar-btn-config-row">
83
+ <span className="toolbar-btn" onMouseDown={() => apply(anchor)}>Apply</span>
84
+ {node?.attributes?.id && <span className="toolbar-btn" onMouseDown={reset}>Remove</span>}
85
+ </span>
86
+ </span>
87
+ </span>
88
+ </span>
89
+ );
90
+ };
91
+
92
+
93
+ export const AnchorButton = () => {
94
+ return <AnchorButtonElement icon={faAnchor}/>;
95
+ };
@@ -25,6 +25,7 @@ import {
25
25
  } from "@contentful/forma-36-react-components";
26
26
  import {Transforms} from "slate";
27
27
  import {ElementAutocomplete} from "../ElementAutocomplete";
28
+ import {AnchorButton} from "./Anchor";
28
29
 
29
30
  export const Portal = ({children}) => {
30
31
  return ReactDOM.createPortal(children, window.document.body);
@@ -166,6 +167,8 @@ export const Toolbar = ({
166
167
  <AlignButtonRight/>
167
168
  </ToobarHoverExpandButton>
168
169
 
170
+ <AnchorButton />
171
+
169
172
  <ToobarHoverExpandButton>
170
173
  <InsertDividerButton/>
171
174
  <InsertGridButton/>
package/webpack.config.js CHANGED
@@ -104,12 +104,13 @@ module.exports = {
104
104
  resolve: {
105
105
  extensions: ['.js', '.json', '.jsx'],
106
106
  fallback: {
107
+ fs: false,
108
+ zlib: false,
107
109
  stream: false,
108
- buffer: false,
109
- util: false,
110
110
  path: false,
111
- fs: false,
112
111
  timers: false,
112
+ buffer: false,
113
+ util: false,
113
114
  },
114
115
  alias: {
115
116
  scss: path.resolve(__dirname, 'scss'),