@modusoperandi/licit 1.3.1 → 1.3.2

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.
@@ -114,7 +114,7 @@ var LinkSetURLCommand = /*#__PURE__*/function (_UICommand) {
114
114
  return _context2.abrupt("return", Promise.resolve(undefined));
115
115
  case 5:
116
116
  from = selection.from, to = selection.to;
117
- result = findNodesWithSameMark(doc, from, to, markType);
117
+ result = findNodesWithSameMark(doc, from, to - 1, markType);
118
118
  href = result ? result.mark.attrs.href : null;
119
119
  _context2.next = 10;
120
120
  return _this.showTocList(view);
@@ -86,7 +86,7 @@ class LinkSetURLCommand extends UICommand {
86
86
  return Promise.resolve(undefined);
87
87
  }
88
88
  const { from, to } = selection;
89
- const result = findNodesWithSameMark(doc, from, to, markType);
89
+ const result = findNodesWithSameMark(doc, from, to - 1, markType);
90
90
  const href = result ? result.mark.attrs.href : null;
91
91
  const tocItemsNode = await this.showTocList(view);
92
92
  const viewPops = {
@@ -15,23 +15,39 @@ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.
15
15
  function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
16
16
  function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
17
17
  function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
18
- import { EditorState, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
18
+ import { Plugin, PluginKey, TextSelection } from 'prosemirror-state';
19
19
  import { EditorView } from 'prosemirror-view';
20
20
  import { MARK_LINK } from './MarkNames.js';
21
- import { hideSelectionPlaceholder } from './SelectionPlaceholderPlugin.js';
22
- import { applyMark, findNodesWithSameMark, atAnchorTopCenter, createPopUp } from '@modusoperandi/licit-ui-commands';
21
+ import { atAnchorTopCenter, createPopUp, findNodesWithSameMark } from '@modusoperandi/licit-ui-commands';
23
22
  import lookUpElement from './lookUpElement.js';
24
23
  import LinkTooltip from './ui/LinkTooltip.js';
25
- import LinkURLEditor from './ui/LinkURLEditor.js';
26
- import { INNER_LINK } from './Types.js';
27
-
28
- // https://prosemirror.net/examples/tooltip/
24
+ import scrollIntoView from 'smooth-scroll-into-view-if-needed';
25
+ import sanitizeURL from './sanitizeURL.js';
26
+ import '@modusoperandi/licit-ui-commands/ui/czi-pop-up.css';
29
27
  var SPEC = {
30
- // [FS] IRAD-1005 2020-07-07
31
- // Upgrade outdated packages.
32
28
  key: new PluginKey('LinkTooltipPlugin'),
29
+ props: {
30
+ handleDOMEvents: {
31
+ mouseover: function mouseover(view, event) {
32
+ var _pluginView$_handleMo;
33
+ var pluginView = view.dom._linkTooltipView;
34
+ return (_pluginView$_handleMo = pluginView === null || pluginView === void 0 ? void 0 : pluginView._handleMouseOver(event)) !== null && _pluginView$_handleMo !== void 0 ? _pluginView$_handleMo : false;
35
+ },
36
+ mouseout: function mouseout(view, event) {
37
+ var pluginView = view.dom._linkTooltipView;
38
+ return pluginView === null || pluginView === void 0 ? void 0 : pluginView._handleMouseOut.call(view, event);
39
+ },
40
+ click: function click(view, event) {
41
+ var pluginView = view.dom._linkTooltipView;
42
+ return pluginView === null || pluginView === void 0 ? void 0 : pluginView._handleClick.call(view, event);
43
+ }
44
+ }
45
+ },
33
46
  view: function view(editorView) {
34
- return new LinkTooltipView(editorView);
47
+ var pluginView = new LinkTooltipView(editorView);
48
+ // Store the instance for use in DOM handlers
49
+ editorView.dom._linkTooltipView = pluginView;
50
+ return pluginView;
35
51
  }
36
52
  };
37
53
  var LinkTooltipPlugin = /*#__PURE__*/function (_Plugin) {
@@ -43,153 +59,78 @@ var LinkTooltipPlugin = /*#__PURE__*/function (_Plugin) {
43
59
  return _createClass(LinkTooltipPlugin);
44
60
  }(Plugin);
45
61
  var LinkTooltipView = /*#__PURE__*/function () {
46
- function LinkTooltipView(editorView) {
62
+ function LinkTooltipView(_view) {
47
63
  var _this = this;
48
64
  _classCallCheck(this, LinkTooltipView);
49
- _defineProperty(this, "_anchorEl", null);
50
65
  _defineProperty(this, "_popup", null);
51
- _defineProperty(this, "_editor", null);
52
- _defineProperty(this, "_onCancel", function (view) {
53
- _this.destroy();
54
- view.focus();
66
+ _defineProperty(this, "_anchorEl", null);
67
+ _defineProperty(this, "_view", null);
68
+ _defineProperty(this, "jumpLink", function (view, tocItemPos, href, selectionId) {
69
+ if (selectionId || selectionId === 0 && tocItemPos) {
70
+ _this.jumpInnerLink(view, tocItemPos);
71
+ } else {
72
+ _this._openLink(href);
73
+ }
55
74
  });
56
- _defineProperty(this, "_onClose", function () {
57
- _this._anchorEl = null;
58
- _this._editor = null;
59
- _this._popup = null;
75
+ _defineProperty(this, "jumpInnerLink", function (view, tocItemPos) {
76
+ var transaction = view.state.tr;
77
+ var tr = transaction.setSelection(TextSelection.create(transaction.doc, tocItemPos.position + 1));
78
+ view.dispatch(tr.scrollIntoView(true));
79
+ var dom = view.domAtPos((tocItemPos === null || tocItemPos === void 0 ? void 0 : tocItemPos.position) + 1).node;
80
+ if (dom !== null && dom !== void 0 && dom.scrollIntoView) {
81
+ dom.scrollIntoView({
82
+ behavior: 'smooth',
83
+ block: 'start'
84
+ });
85
+ }
60
86
  });
61
- _defineProperty(this, "showTocList", /*#__PURE__*/function () {
62
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(view) {
63
- var storeTOCvalue, TOCselectedNode, stylePromise, styles;
64
- return _regeneratorRuntime().wrap(function _callee$(_context) {
65
- while (1) switch (_context.prev = _context.next) {
66
- case 0:
67
- storeTOCvalue = [];
68
- TOCselectedNode = [];
69
- stylePromise = view.runtime;
70
- if (!(stylePromise === null || undefined)) {
71
- _context.next = 7;
72
- break;
87
+ _defineProperty(this, "_openLink", function (href) {
88
+ if (_this.isBookMarkHref(href)) {
89
+ var id = href.substr(1);
90
+ var el = document.getElementById(id);
91
+ if (el) {
92
+ var _this$props = _this.props,
93
+ onCancel = _this$props.onCancel,
94
+ editorView = _this$props.editorView;
95
+ onCancel(editorView);
96
+ _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
97
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
98
+ while (1) switch (_context.prev = _context.next) {
99
+ case 0:
100
+ _context.next = 2;
101
+ return scrollIntoView(el, {
102
+ scrollMode: 'if-needed',
103
+ behavior: 'smooth'
104
+ });
105
+ case 2:
106
+ case "end":
107
+ return _context.stop();
73
108
  }
74
- return _context.abrupt("return", TOCselectedNode);
75
- case 7:
76
- _context.next = 9;
77
- return stylePromise.fetchStyles();
78
- case 9:
79
- styles = _context.sent;
80
- storeTOCvalue = styles.filter(function (style // Added TOT/TOF selected styles to be listed as well
81
- ) {
82
- var _style$styles, _style$styles2, _style$styles3;
83
- return (style === null || style === void 0 || (_style$styles = style.styles) === null || _style$styles === void 0 ? void 0 : _style$styles.toc) === true || (style === null || style === void 0 || (_style$styles2 = style.styles) === null || _style$styles2 === void 0 ? void 0 : _style$styles2.tot) === true || (style === null || style === void 0 || (_style$styles3 = style.styles) === null || _style$styles3 === void 0 ? void 0 : _style$styles3.tof) === true;
84
- }).map(function (style) {
85
- return style === null || style === void 0 ? void 0 : style.styleName;
86
- });
87
- view.state.tr.doc.descendants(function (node, pos) {
88
- if (node.attrs.styleName) {
89
- for (var i = 0; i <= storeTOCvalue.length; i++) {
90
- if (storeTOCvalue[i] === node.attrs.styleName) {
91
- TOCselectedNode.push({
92
- node_: node,
93
- pos_: pos
94
- });
95
- }
96
- }
97
- }
98
- });
99
- return _context.abrupt("return", TOCselectedNode);
100
- case 13:
101
- case "end":
102
- return _context.stop();
103
- }
104
- }, _callee);
105
- }));
106
- return function (_x) {
107
- return _ref.apply(this, arguments);
108
- };
109
- }());
110
- _defineProperty(this, "_onEdit", function (view) {
111
- _this._popup.close();
112
- if (_this._editor) {
113
- return;
114
- }
115
- var state = view.state;
116
- var schema = state.schema,
117
- doc = state.doc,
118
- selection = state.selection;
119
- var from = selection.from,
120
- to = selection.to;
121
- var markType = schema.marks[MARK_LINK];
122
- var result = findNodesWithSameMark(doc, from, to, markType);
123
- if (!result) {
109
+ }, _callee);
110
+ }))();
111
+ }
124
112
  return;
125
113
  }
126
- _this.showTocList(view).then(function (data) {
127
- var tocItemsNode = data;
128
- var href = result.mark.attrs.href;
129
- var viewPops = {
130
- selectionId_: result.mark.attrs.selectionId,
131
- href_: href,
132
- TOCselectedNode_: tocItemsNode,
133
- view_: view
134
- };
135
- _this._editor = createPopUp(LinkURLEditor, viewPops, {
136
- onClose: function onClose(value) {
137
- _this._editor = null;
138
- _this._onEditEnd(view, selection, value);
139
- }
140
- });
141
- });
142
- });
143
- _defineProperty(this, "_onRemove", function (view) {
144
- _this._popup.close();
145
- _this._onEditEnd(view, view.state.selection, null);
146
- });
147
- _defineProperty(this, "_onEditEnd", function (view, initialSelection, url) {
148
- var state = view.state,
149
- dispatch = view.dispatch;
150
- var tr = hideSelectionPlaceholder(state);
151
- if (url !== undefined) {
152
- var schema = state.schema;
153
- var markType = schema.marks[MARK_LINK];
154
- if (markType) {
155
- var result = findNodesWithSameMark(tr.doc, initialSelection.from, initialSelection.to, markType);
156
- if (result) {
157
- var linkSelection = TextSelection.create(tr.doc, result.from.pos, result.to.pos + 1);
158
- tr = tr.setSelection(linkSelection);
159
- var selectionId;
160
- var href;
161
- if (url === null) {
162
- selectionId = null;
163
- href = null;
164
- } else if (url.includes(INNER_LINK)) {
165
- selectionId = url.split(INNER_LINK)[0];
166
- href = url.split(INNER_LINK)[1];
167
- } else {
168
- selectionId = null;
169
- href = url;
170
- }
171
- var attrs = href ? {
172
- href: href,
173
- selectionId: selectionId
174
- } : null;
175
- tr = applyMark(tr, schema, markType, attrs);
176
-
177
- // [FS] IRAD-1005 2020-07-09
178
- // Upgrade outdated packages.
179
- // reset selection to original using the latest doc.
180
- var origSelection = TextSelection.create(tr.doc, initialSelection.from, initialSelection.to);
181
- tr = tr.setSelection(origSelection);
182
- }
114
+ if (href) {
115
+ var _this$_view;
116
+ var url = sanitizeURL(href);
117
+ var popupString;
118
+ if (_this._view.editable) {
119
+ popupString = 'Any unsaved changes will be lost';
120
+ } else {
121
+ popupString = '';
122
+ }
123
+ if ((_this$_view = _this._view) !== null && _this$_view !== void 0 && (_this$_view = _this$_view.runtime) !== null && _this$_view !== void 0 && _this$_view.openLinkDialog) {
124
+ _this._view.runtime.openLinkDialog(url, popupString);
125
+ } else {
126
+ window.open(url);
183
127
  }
184
128
  }
185
- dispatch(tr);
186
- view.focus();
187
129
  });
188
- this.update(editorView, null);
189
- }
190
- return _createClass(LinkTooltipView, [{
191
- key: "getInnerlinkSelected_position",
192
- value: function getInnerlinkSelected_position(view, selectionId) {
130
+ _defineProperty(this, "isBookMarkHref", function (href) {
131
+ return !!href && href.startsWith('#') && href.length >= 2;
132
+ });
133
+ _defineProperty(this, "getInnerlinkSelected_position", function (view, selectionId) {
193
134
  var tocItemPos = null;
194
135
  if (selectionId) {
195
136
  view.state.tr.doc.descendants(function (node, pos) {
@@ -202,68 +143,80 @@ var LinkTooltipView = /*#__PURE__*/function () {
202
143
  });
203
144
  }
204
145
  return tocItemPos;
146
+ });
147
+ this._view = _view;
148
+ }
149
+ return _createClass(LinkTooltipView, [{
150
+ key: "_handleMouseOver",
151
+ value: function _handleMouseOver(event) {
152
+ var _event$target,
153
+ _this$_popup,
154
+ _this2 = this;
155
+ var anchor = (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.closest('a');
156
+ if (!anchor || anchor === this._anchorEl) return;
157
+ this._anchorEl = anchor;
158
+ var href = anchor.getAttribute('href');
159
+ (_this$_popup = this._popup) === null || _this$_popup === void 0 || _this$_popup.close();
160
+ this._popup = createPopUp(LinkTooltip, {
161
+ href: href,
162
+ selectionId: null,
163
+ editorView: this._view
164
+ }, {
165
+ anchor: anchor,
166
+ autoDismiss: true,
167
+ onClose: function onClose() {
168
+ _this2._popup = null;
169
+ _this2._anchorEl = null;
170
+ },
171
+ position: atAnchorTopCenter
172
+ });
205
173
  }
206
174
  }, {
207
- key: "update",
208
- value: function update(view, lastState) {
209
- var state = view.state;
175
+ key: "_handleClick",
176
+ value: function _handleClick(event) {
177
+ var state = this.dom._linkTooltipView._view.state;
210
178
  var doc = state.doc,
211
179
  selection = state.selection,
212
180
  schema = state.schema;
213
- var markType = schema.marks[MARK_LINK];
214
- if (!markType) {
215
- return;
216
- }
217
181
  var from = selection.from,
218
182
  to = selection.to;
183
+ var markType = schema.marks[MARK_LINK];
219
184
  var result = findNodesWithSameMark(doc, from, to, markType);
220
185
  if (!result) {
221
- this.destroy();
222
- return;
186
+ return false;
223
187
  }
224
- var domFound = view.domAtPos(from);
188
+ var domFound = this.dom._linkTooltipView._view.domAtPos(from);
225
189
  if (!domFound) {
226
- this.destroy();
227
- return;
190
+ return false;
228
191
  }
229
- var anchorEl = lookUpElement(domFound.node, function (el) {
192
+ var anchor = lookUpElement(domFound.node, function (el) {
230
193
  return el.nodeName === 'A';
231
194
  });
232
- if (!anchorEl) {
233
- this.destroy();
234
- return;
195
+ if (!anchor) {
196
+ return false;
235
197
  }
236
- var tocItemPos = this.getInnerlinkSelected_position(view, result.mark.attrs.selectionId);
237
- var popup = this._popup;
238
- var viewPops = {
239
- editorState: state,
240
- editorView: view,
241
- href: result.mark.attrs.href,
242
- selectionId_: result.mark.attrs.selectionId,
243
- onCancel: this._onCancel,
244
- onEdit: this._onEdit,
245
- onRemove: this._onRemove,
246
- tocItemPos_: tocItemPos
247
- };
248
- if (popup && anchorEl === this._anchorEl) {
249
- popup.update(viewPops);
250
- } else {
251
- popup === null || popup === void 0 || popup.close();
252
- this._anchorEl = anchorEl;
253
- this._popup = createPopUp(LinkTooltip, viewPops, {
254
- anchor: anchorEl,
255
- autoDismiss: false,
256
- onClose: this._onClose,
257
- position: atAnchorTopCenter
258
- });
198
+ var href = anchor.getAttribute('href');
199
+ var selectionId = anchor.getAttribute('selectionid');
200
+ var tocItemPos = this.dom._linkTooltipView.getInnerlinkSelected_position(this.dom._linkTooltipView._view, result.mark.attrs.selectionId);
201
+ this.dom._linkTooltipView.jumpLink(this.dom._linkTooltipView._view, tocItemPos, href, selectionId);
202
+ event.preventDefault(); // prevent default browser navigation
203
+ return true;
204
+ }
205
+ }, {
206
+ key: "_handleMouseOut",
207
+ value: function _handleMouseOut(event) {
208
+ if (this._anchorEl && !this._anchorEl.contains(event.relatedTarget)) {
209
+ var _this$_popup2;
210
+ (_this$_popup2 = this._popup) === null || _this$_popup2 === void 0 || _this$_popup2.close();
211
+ this._popup = null;
212
+ this._anchorEl = null;
259
213
  }
260
214
  }
261
215
  }, {
262
216
  key: "destroy",
263
217
  value: function destroy() {
264
- var _this$_popup, _this$_editor;
265
- (_this$_popup = this._popup) === null || _this$_popup === void 0 || _this$_popup.close();
266
- (_this$_editor = this._editor) === null || _this$_editor === void 0 || _this$_editor.close();
218
+ var _this$_popup3;
219
+ (_this$_popup3 = this._popup) === null || _this$_popup3 === void 0 || _this$_popup3.close();
267
220
  }
268
221
  }]);
269
222
  }();