@codemirror/view 0.19.30 → 0.19.31

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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.19.31 (2021-12-13)
2
+
3
+ ### New features
4
+
5
+ The package now exports a `dropCursor` extension that draws a cursor at the current drop position when dragging content over the editor.
6
+
1
7
  ## 0.19.30 (2021-12-13)
2
8
 
3
9
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -5065,11 +5065,13 @@ const baseTheme = buildTheme("." + baseThemeID, {
5065
5065
  // recomputation.
5066
5066
  "@keyframes cm-blink": { "0%": {}, "50%": { visibility: "hidden" }, "100%": {} },
5067
5067
  "@keyframes cm-blink2": { "0%": {}, "50%": { visibility: "hidden" }, "100%": {} },
5068
- ".cm-cursor": {
5068
+ ".cm-cursor, .cm-dropCursor": {
5069
5069
  position: "absolute",
5070
5070
  borderLeft: "1.2px solid black",
5071
5071
  marginLeft: "-0.6px",
5072
5072
  pointerEvents: "none",
5073
+ },
5074
+ ".cm-cursor": {
5073
5075
  display: "none"
5074
5076
  },
5075
5077
  "&dark .cm-cursor": {
@@ -6990,6 +6992,94 @@ function measureCursor(view, cursor, primary) {
6990
6992
  return new Piece(pos.left - base.left, pos.top - base.top, -1, pos.bottom - pos.top, primary ? "cm-cursor cm-cursor-primary" : "cm-cursor cm-cursor-secondary");
6991
6993
  }
6992
6994
 
6995
+ const setDropCursorPos = state.StateEffect.define({
6996
+ map(pos, mapping) { return pos == null ? null : mapping.mapPos(pos); }
6997
+ });
6998
+ const dropCursorPos = state.StateField.define({
6999
+ create() { return null; },
7000
+ update(pos, tr) {
7001
+ if (pos != null)
7002
+ pos = tr.changes.mapPos(pos);
7003
+ return tr.effects.reduce((pos, e) => e.is(setDropCursorPos) ? e.value : pos, pos);
7004
+ }
7005
+ });
7006
+ const drawDropCursor = ViewPlugin.fromClass(class {
7007
+ constructor(view) {
7008
+ this.view = view;
7009
+ this.cursor = null;
7010
+ this.measureReq = { read: this.readPos.bind(this), write: this.drawCursor.bind(this) };
7011
+ }
7012
+ update(update) {
7013
+ var _a;
7014
+ let cursorPos = update.state.field(dropCursorPos);
7015
+ if (cursorPos == null) {
7016
+ if (this.cursor != null) {
7017
+ (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.remove();
7018
+ this.cursor = null;
7019
+ }
7020
+ }
7021
+ else {
7022
+ if (!this.cursor) {
7023
+ this.cursor = this.view.scrollDOM.appendChild(document.createElement("div"));
7024
+ this.cursor.className = "cm-dropCursor";
7025
+ }
7026
+ if (update.startState.field(dropCursorPos) != cursorPos || update.docChanged || update.geometryChanged)
7027
+ this.view.requestMeasure(this.measureReq);
7028
+ }
7029
+ }
7030
+ readPos() {
7031
+ let pos = this.view.state.field(dropCursorPos);
7032
+ let rect = pos != null && this.view.coordsAtPos(pos);
7033
+ if (!rect)
7034
+ return null;
7035
+ let outer = this.view.scrollDOM.getBoundingClientRect();
7036
+ return { left: rect.left - outer.left, top: rect.top - outer.top, height: rect.bottom - rect.top };
7037
+ }
7038
+ drawCursor(pos) {
7039
+ if (this.cursor) {
7040
+ if (pos) {
7041
+ this.cursor.style.left = pos.left + "px";
7042
+ this.cursor.style.top = pos.top + "px";
7043
+ this.cursor.style.height = pos.height + "px";
7044
+ }
7045
+ else {
7046
+ this.cursor.style.left = "-100000px";
7047
+ }
7048
+ }
7049
+ }
7050
+ destroy() {
7051
+ if (this.cursor)
7052
+ this.cursor.remove();
7053
+ }
7054
+ setDropPos(pos) {
7055
+ if (this.view.state.field(dropCursorPos) != pos)
7056
+ this.view.dispatch({ effects: setDropCursorPos.of(pos) });
7057
+ }
7058
+ }, {
7059
+ eventHandlers: {
7060
+ dragover(event) {
7061
+ this.setDropPos(this.view.posAtCoords({ x: event.clientX, y: event.clientY }));
7062
+ },
7063
+ dragleave(event) {
7064
+ if (event.target == this.view.contentDOM || !this.view.contentDOM.contains(event.relatedTarget))
7065
+ this.setDropPos(null);
7066
+ },
7067
+ dragend() {
7068
+ this.setDropPos(null);
7069
+ },
7070
+ drop() {
7071
+ this.setDropPos(null);
7072
+ }
7073
+ }
7074
+ });
7075
+ /**
7076
+ Draws a cursor at the current drop position when something is
7077
+ dragged over the editor.
7078
+ */
7079
+ function dropCursor() {
7080
+ return [dropCursorPos, drawDropCursor];
7081
+ }
7082
+
6993
7083
  function iterMatches(doc, re, from, to, f) {
6994
7084
  re.lastIndex = 0;
6995
7085
  for (let cursor = doc.iterRange(from, to), pos = from, m; !cursor.next().done; pos += cursor.value.length) {
@@ -7357,6 +7447,7 @@ exports.ViewUpdate = ViewUpdate;
7357
7447
  exports.WidgetType = WidgetType;
7358
7448
  exports.__test = __test;
7359
7449
  exports.drawSelection = drawSelection;
7450
+ exports.dropCursor = dropCursor;
7360
7451
  exports.highlightActiveLine = highlightActiveLine;
7361
7452
  exports.highlightSpecialChars = highlightSpecialChars;
7362
7453
  exports.keymap = keymap;
package/dist/index.d.ts CHANGED
@@ -1291,6 +1291,12 @@ content).
1291
1291
  */
1292
1292
  declare function drawSelection(config?: SelectionConfig): Extension;
1293
1293
 
1294
+ /**
1295
+ Draws a cursor at the current drop position when something is
1296
+ dragged over the editor.
1297
+ */
1298
+ declare function dropCursor(): Extension;
1299
+
1294
1300
  interface SpecialCharConfig {
1295
1301
  /**
1296
1302
  An optional function that renders the placeholder elements.
@@ -1409,4 +1415,4 @@ declare class MatchDecorator {
1409
1415
  private updateRange;
1410
1416
  }
1411
1417
 
1412
- export { BidiSpan, BlockInfo, BlockType, Command, DOMEventHandlers, DOMEventMap, Decoration, DecorationSet, Direction, EditorView, KeyBinding, MatchDecorator, MouseSelectionStyle, PluginField, PluginFieldProvider, PluginSpec, PluginValue, Rect, ViewPlugin, ViewUpdate, WidgetType, drawSelection, highlightActiveLine, highlightSpecialChars, keymap, logException, placeholder, runScopeHandlers, scrollPastEnd };
1418
+ export { BidiSpan, BlockInfo, BlockType, Command, DOMEventHandlers, DOMEventMap, Decoration, DecorationSet, Direction, EditorView, KeyBinding, MatchDecorator, MouseSelectionStyle, PluginField, PluginFieldProvider, PluginSpec, PluginValue, Rect, ViewPlugin, ViewUpdate, WidgetType, drawSelection, dropCursor, highlightActiveLine, highlightSpecialChars, keymap, logException, placeholder, runScopeHandlers, scrollPastEnd };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { MapMode, Text as Text$1, Facet, StateEffect, ChangeSet, EditorSelection, CharCategory, EditorState, Transaction, Prec, combineConfig } from '@codemirror/state';
1
+ import { MapMode, Text as Text$1, Facet, StateEffect, ChangeSet, EditorSelection, CharCategory, EditorState, Transaction, Prec, combineConfig, StateField } from '@codemirror/state';
2
2
  import { StyleModule } from 'style-mod';
3
3
  import { RangeSet, RangeValue, RangeSetBuilder } from '@codemirror/rangeset';
4
4
  export { Range } from '@codemirror/rangeset';
@@ -5059,11 +5059,13 @@ const baseTheme = /*@__PURE__*/buildTheme("." + baseThemeID, {
5059
5059
  // recomputation.
5060
5060
  "@keyframes cm-blink": { "0%": {}, "50%": { visibility: "hidden" }, "100%": {} },
5061
5061
  "@keyframes cm-blink2": { "0%": {}, "50%": { visibility: "hidden" }, "100%": {} },
5062
- ".cm-cursor": {
5062
+ ".cm-cursor, .cm-dropCursor": {
5063
5063
  position: "absolute",
5064
5064
  borderLeft: "1.2px solid black",
5065
5065
  marginLeft: "-0.6px",
5066
5066
  pointerEvents: "none",
5067
+ },
5068
+ ".cm-cursor": {
5067
5069
  display: "none"
5068
5070
  },
5069
5071
  "&dark .cm-cursor": {
@@ -6984,6 +6986,94 @@ function measureCursor(view, cursor, primary) {
6984
6986
  return new Piece(pos.left - base.left, pos.top - base.top, -1, pos.bottom - pos.top, primary ? "cm-cursor cm-cursor-primary" : "cm-cursor cm-cursor-secondary");
6985
6987
  }
6986
6988
 
6989
+ const setDropCursorPos = /*@__PURE__*/StateEffect.define({
6990
+ map(pos, mapping) { return pos == null ? null : mapping.mapPos(pos); }
6991
+ });
6992
+ const dropCursorPos = /*@__PURE__*/StateField.define({
6993
+ create() { return null; },
6994
+ update(pos, tr) {
6995
+ if (pos != null)
6996
+ pos = tr.changes.mapPos(pos);
6997
+ return tr.effects.reduce((pos, e) => e.is(setDropCursorPos) ? e.value : pos, pos);
6998
+ }
6999
+ });
7000
+ const drawDropCursor = /*@__PURE__*/ViewPlugin.fromClass(class {
7001
+ constructor(view) {
7002
+ this.view = view;
7003
+ this.cursor = null;
7004
+ this.measureReq = { read: this.readPos.bind(this), write: this.drawCursor.bind(this) };
7005
+ }
7006
+ update(update) {
7007
+ var _a;
7008
+ let cursorPos = update.state.field(dropCursorPos);
7009
+ if (cursorPos == null) {
7010
+ if (this.cursor != null) {
7011
+ (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.remove();
7012
+ this.cursor = null;
7013
+ }
7014
+ }
7015
+ else {
7016
+ if (!this.cursor) {
7017
+ this.cursor = this.view.scrollDOM.appendChild(document.createElement("div"));
7018
+ this.cursor.className = "cm-dropCursor";
7019
+ }
7020
+ if (update.startState.field(dropCursorPos) != cursorPos || update.docChanged || update.geometryChanged)
7021
+ this.view.requestMeasure(this.measureReq);
7022
+ }
7023
+ }
7024
+ readPos() {
7025
+ let pos = this.view.state.field(dropCursorPos);
7026
+ let rect = pos != null && this.view.coordsAtPos(pos);
7027
+ if (!rect)
7028
+ return null;
7029
+ let outer = this.view.scrollDOM.getBoundingClientRect();
7030
+ return { left: rect.left - outer.left, top: rect.top - outer.top, height: rect.bottom - rect.top };
7031
+ }
7032
+ drawCursor(pos) {
7033
+ if (this.cursor) {
7034
+ if (pos) {
7035
+ this.cursor.style.left = pos.left + "px";
7036
+ this.cursor.style.top = pos.top + "px";
7037
+ this.cursor.style.height = pos.height + "px";
7038
+ }
7039
+ else {
7040
+ this.cursor.style.left = "-100000px";
7041
+ }
7042
+ }
7043
+ }
7044
+ destroy() {
7045
+ if (this.cursor)
7046
+ this.cursor.remove();
7047
+ }
7048
+ setDropPos(pos) {
7049
+ if (this.view.state.field(dropCursorPos) != pos)
7050
+ this.view.dispatch({ effects: setDropCursorPos.of(pos) });
7051
+ }
7052
+ }, {
7053
+ eventHandlers: {
7054
+ dragover(event) {
7055
+ this.setDropPos(this.view.posAtCoords({ x: event.clientX, y: event.clientY }));
7056
+ },
7057
+ dragleave(event) {
7058
+ if (event.target == this.view.contentDOM || !this.view.contentDOM.contains(event.relatedTarget))
7059
+ this.setDropPos(null);
7060
+ },
7061
+ dragend() {
7062
+ this.setDropPos(null);
7063
+ },
7064
+ drop() {
7065
+ this.setDropPos(null);
7066
+ }
7067
+ }
7068
+ });
7069
+ /**
7070
+ Draws a cursor at the current drop position when something is
7071
+ dragged over the editor.
7072
+ */
7073
+ function dropCursor() {
7074
+ return [dropCursorPos, drawDropCursor];
7075
+ }
7076
+
6987
7077
  function iterMatches(doc, re, from, to, f) {
6988
7078
  re.lastIndex = 0;
6989
7079
  for (let cursor = doc.iterRange(from, to), pos = from, m; !cursor.next().done; pos += cursor.value.length) {
@@ -7335,4 +7425,4 @@ function placeholder(content) {
7335
7425
  */
7336
7426
  const __test = { HeightMap, HeightOracle, MeasuredHeights, QueryType, ChangedRange, computeOrder, moveVisually };
7337
7427
 
7338
- export { BidiSpan, BlockInfo, BlockType, Decoration, Direction, EditorView, MatchDecorator, PluginField, PluginFieldProvider, ViewPlugin, ViewUpdate, WidgetType, __test, drawSelection, highlightActiveLine, highlightSpecialChars, keymap, logException, placeholder, runScopeHandlers, scrollPastEnd };
7428
+ export { BidiSpan, BlockInfo, BlockType, Decoration, Direction, EditorView, MatchDecorator, PluginField, PluginFieldProvider, ViewPlugin, ViewUpdate, WidgetType, __test, drawSelection, dropCursor, highlightActiveLine, highlightSpecialChars, keymap, logException, placeholder, runScopeHandlers, scrollPastEnd };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/view",
3
- "version": "0.19.30",
3
+ "version": "0.19.31",
4
4
  "description": "DOM view component for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",