@mirrormedia/lilith-draft-editor 1.1.0-alpha.25 → 1.1.0-alpha.26

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.
@@ -7,32 +7,40 @@ exports.LinkButton = LinkButton;
7
7
 
8
8
  var _react = _interopRequireWildcard(require("react"));
9
9
 
10
+ var _styledComponents = _interopRequireDefault(require("styled-components"));
11
+
10
12
  var _modals = require("@keystone-ui/modals");
11
13
 
12
14
  var _draftJs = require("draft-js");
13
15
 
14
16
  var _fields = require("@keystone-ui/fields");
15
17
 
18
+ var _util = require("../util");
19
+
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+
16
22
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17
23
 
18
24
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
25
 
20
- const styles = {
21
- urlInput: {
22
- fontFamily: "'Georgia', serif",
23
- marginRight: 10,
24
- padding: 10
25
- }
26
- };
26
+ const StyledTextInput = (0, _styledComponents.default)(_fields.TextInput)`
27
+ fontfamily: Georgia, serif;
28
+ marginright: 10;
29
+ padding: 10;
30
+ `;
27
31
 
28
32
  function LinkButton(props) {
29
33
  const {
30
- isActive,
31
34
  editorState,
32
35
  onChange
33
36
  } = props;
37
+ const entityData = (0, _util.getSelectionEntityData)(editorState);
38
+ const url = (entityData === null || entityData === void 0 ? void 0 : entityData.url) || '';
39
+ (0, _react.useEffect)(() => {
40
+ setLinkUrl(url);
41
+ }, [url]);
34
42
  const [toShowUrlInput, setToShowUrlInput] = (0, _react.useState)(false);
35
- const [urlValue, setUrlValue] = (0, _react.useState)('');
43
+ const [linkUrl, setLinkUrl] = (0, _react.useState)(url || '');
36
44
 
37
45
  const promptForLink = e => {
38
46
  e.preventDefault();
@@ -46,7 +54,7 @@ function LinkButton(props) {
46
54
  const confirmLink = () => {
47
55
  const contentState = editorState.getCurrentContent();
48
56
  const contentStateWithEntity = contentState.createEntity('LINK', 'MUTABLE', {
49
- url: urlValue
57
+ url: linkUrl
50
58
  });
51
59
  const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
52
60
 
@@ -54,9 +62,11 @@ function LinkButton(props) {
54
62
  currentContent: contentStateWithEntity
55
63
  });
56
64
 
57
- onChange(_draftJs.RichUtils.toggleLink(newEditorState, newEditorState.getSelection(), entityKey));
65
+ const selection = newEditorState.getSelection(); // RichUtils.toggleLink will reset selection back to first block and cause
66
+ // the editor scroll to top. Use `forceSelection` to hold the selection.
67
+
68
+ onChange(_draftJs.EditorState.forceSelection(_draftJs.RichUtils.toggleLink(newEditorState, selection, entityKey), selection));
58
69
  setToShowUrlInput(false);
59
- setUrlValue('');
60
70
  };
61
71
 
62
72
  const onLinkInputKeyDown = e => {
@@ -70,16 +80,28 @@ function LinkButton(props) {
70
80
  const selection = editorState.getSelection();
71
81
 
72
82
  if (!selection.isCollapsed()) {
73
- onChange(_draftJs.RichUtils.toggleLink(editorState, selection, null));
83
+ // RichUtils.toggleLink will reset selection back to first block and cause
84
+ // the editor scroll to top. Use `forceSelection` to hold the selection.
85
+ onChange(_draftJs.EditorState.forceSelection(_draftJs.RichUtils.toggleLink(editorState, selection, null), selection));
74
86
  }
75
87
 
76
88
  setToShowUrlInput(false);
77
- setUrlValue('');
78
89
  };
79
90
 
80
- const urlInput = /*#__PURE__*/_react.default.createElement(_modals.AlertDialog, {
81
- title: "Insert LINK",
82
- isOpen: toShowUrlInput,
91
+ const alertProps = linkUrl ? {
92
+ title: 'Update Link',
93
+ actions: {
94
+ cancel: {
95
+ label: 'Remove',
96
+ action: removeLink
97
+ },
98
+ confirm: {
99
+ label: 'Update',
100
+ action: confirmLink
101
+ }
102
+ }
103
+ } : {
104
+ title: 'Insert Link',
83
105
  actions: {
84
106
  cancel: {
85
107
  label: 'Cancel',
@@ -90,17 +112,22 @@ function LinkButton(props) {
90
112
  action: confirmLink
91
113
  }
92
114
  }
93
- }, /*#__PURE__*/_react.default.createElement(_fields.TextInput, {
94
- onChange: e => setUrlValue(e.target.value),
95
- style: styles.urlInput,
115
+ };
116
+
117
+ const urlInput = /*#__PURE__*/_react.default.createElement(_modals.AlertDialog, {
118
+ title: alertProps.title,
119
+ isOpen: toShowUrlInput,
120
+ actions: alertProps.actions
121
+ }, /*#__PURE__*/_react.default.createElement(StyledTextInput, {
122
+ onChange: e => setLinkUrl(e.target.value),
123
+ value: linkUrl,
96
124
  type: "text",
97
- value: urlValue,
98
125
  onKeyDown: onLinkInputKeyDown
99
126
  }));
100
127
 
101
128
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, urlInput, /*#__PURE__*/_react.default.createElement("div", {
102
129
  className: props.className,
103
- onMouseDown: isActive ? removeLink : promptForLink
130
+ onMouseDown: promptForLink
104
131
  }, /*#__PURE__*/_react.default.createElement("i", {
105
132
  className: "fas fa-link"
106
133
  })));
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getSelectionEntityData = void 0;
7
+
8
+ const getSelectionEntityData = editorState => {
9
+ const selection = editorState.getSelection();
10
+ const startOffset = selection.getStartOffset();
11
+ const startBlock = editorState.getCurrentContent().getBlockForKey(selection.getStartKey());
12
+ let data;
13
+ let entityInstance;
14
+ let entityKey;
15
+
16
+ if (!selection.isCollapsed()) {
17
+ entityKey = startBlock.getEntityAt(startOffset);
18
+ } else {
19
+ entityKey = startBlock.getEntityAt(0);
20
+ }
21
+
22
+ const contentState = editorState.getCurrentContent();
23
+
24
+ if (entityKey !== null) {
25
+ entityInstance = contentState.getEntity(entityKey);
26
+ data = entityInstance.getData();
27
+ }
28
+
29
+ return data;
30
+ };
31
+
32
+ exports.getSelectionEntityData = getSelectionEntityData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mirrormedia/lilith-draft-editor",
3
- "version": "1.1.0-alpha.25",
3
+ "version": "1.1.0-alpha.26",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {