@jupyterlab/codemirror 4.4.0-alpha.2 → 4.4.0-beta.0

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.
@@ -0,0 +1,115 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ *
5
+ * Vendored from https://github.com/yjs/y-codemirror.next
6
+ * licensed under MIT License by Kevin Jahns.
7
+ *
8
+ * Ideally we would depend on the y-codemirror.next, but it is impractical
9
+ * until https://github.com/yjs/y-codemirror.next/issues/27 is resolved.
10
+ *
11
+ * Modifications compared to upstream:
12
+ * - removed spurious mutex (https://github.com/yjs/y-codemirror.next/issues/15)
13
+ * - added TypeScript types
14
+ * - simplified `YUndoManagerConfig` by removing public methods
15
+ * - moved `_onStackItemAdded`, `_onStackItemPopped` and `_storeSelection` definitions out of constructor
16
+ */
17
+ import {
18
+ EditorView,
19
+ PluginValue,
20
+ ViewPlugin,
21
+ ViewUpdate
22
+ } from '@codemirror/view';
23
+ import { Facet } from '@codemirror/state';
24
+ import { YRange, ySyncAnnotation, YSyncConfig, ySyncFacet } from './ybinding';
25
+ import type { UndoManager } from 'yjs';
26
+
27
+ interface IStackItem {
28
+ meta: Map<any, any>;
29
+ }
30
+
31
+ export class YUndoManagerConfig {
32
+ constructor(undoManager: UndoManager) {
33
+ this.undoManager = undoManager;
34
+ }
35
+ public undoManager: UndoManager;
36
+ }
37
+
38
+ export const yUndoManagerFacet = Facet.define<
39
+ YUndoManagerConfig,
40
+ YUndoManagerConfig
41
+ >({
42
+ combine(inputs) {
43
+ return inputs[inputs.length - 1];
44
+ }
45
+ });
46
+
47
+ class YUndoManagerPluginValue implements PluginValue {
48
+ constructor(view: EditorView) {
49
+ this._view = view;
50
+ this._conf = view.state.facet(yUndoManagerFacet);
51
+ this._undoManager = this._conf.undoManager;
52
+ this._syncConf = view.state.facet(ySyncFacet);
53
+ this._beforeChangeSelection = null;
54
+ this._undoManager.on('stack-item-added', this._onStackItemAdded);
55
+ this._undoManager.on('stack-item-popped', this._onStackItemPopped);
56
+ this._undoManager.addTrackedOrigin(this._syncConf);
57
+ }
58
+ _onStackItemAdded = ({
59
+ stackItem,
60
+ changedParentTypes
61
+ }: {
62
+ stackItem: IStackItem;
63
+ changedParentTypes: Map<any, any>;
64
+ }) => {
65
+ // only store metadata if this type was affected
66
+ if (
67
+ changedParentTypes.has(this._syncConf.ytext) &&
68
+ this._beforeChangeSelection &&
69
+ !stackItem.meta.has(this)
70
+ ) {
71
+ // do not overwrite previous stored selection
72
+ stackItem.meta.set(this, this._beforeChangeSelection);
73
+ }
74
+ };
75
+ _onStackItemPopped = ({ stackItem }: { stackItem: IStackItem }) => {
76
+ const sel = stackItem.meta.get(this);
77
+ if (sel) {
78
+ const selection = this._syncConf.fromYRange(sel);
79
+ this._view.dispatch(
80
+ this._view.state.update({
81
+ selection,
82
+ effects: [EditorView.scrollIntoView(selection)]
83
+ })
84
+ );
85
+ this._storeSelection();
86
+ }
87
+ };
88
+ _storeSelection = () => {
89
+ // store the selection before the change is applied so we can restore it with the undo manager.
90
+ this._beforeChangeSelection = this._syncConf.toYRange(
91
+ this._view.state.selection.main
92
+ );
93
+ };
94
+ update(update: ViewUpdate) {
95
+ if (
96
+ update.selectionSet &&
97
+ (update.transactions.length === 0 ||
98
+ update.transactions[0].annotation(ySyncAnnotation) !== this._syncConf)
99
+ ) {
100
+ // This only works when YUndoManagerPlugin is included before the sync plugin
101
+ this._storeSelection();
102
+ }
103
+ }
104
+ destroy() {
105
+ this._undoManager.off('stack-item-added', this._onStackItemAdded);
106
+ this._undoManager.off('stack-item-popped', this._onStackItemPopped);
107
+ this._undoManager.removeTrackedOrigin(this._syncConf);
108
+ }
109
+ private _undoManager: UndoManager;
110
+ private _view: EditorView;
111
+ private _beforeChangeSelection: null | YRange;
112
+ private _conf: YUndoManagerConfig;
113
+ private _syncConf: YSyncConfig;
114
+ }
115
+ export const yUndoManager = ViewPlugin.fromClass(YUndoManagerPluginValue);
package/src/theme.ts CHANGED
@@ -81,10 +81,6 @@ export const jupyterEditorTheme = EditorView.theme({
81
81
 
82
82
  '.cm-tooltip': {
83
83
  backgroundColor: 'var(--jp-layout-color1)'
84
- },
85
-
86
- '.cm-builtin': {
87
- color: 'var(--jp-mirror-editor-builtin-color)'
88
84
  }
89
85
  });
90
86
 
package/src/token.ts CHANGED
@@ -91,6 +91,14 @@ export interface IExtensionsHandler extends IDisposable {
91
91
  */
92
92
  setOptions(options: Record<string, any>): void;
93
93
 
94
+ /**
95
+ * Set a base config options for the editor.
96
+ *
97
+ * You will need to reconfigure the editor extensions by listening
98
+ * to `IExtensionsHandler.configChanged`.
99
+ */
100
+ setBaseOptions(options: Record<string, any>): void;
101
+
94
102
  /**
95
103
  * Returns the list of initial extensions of an editor.
96
104
  *
package/style/base.css CHANGED
@@ -67,6 +67,10 @@
67
67
  margin-right: -5px;
68
68
  }
69
69
 
70
+ .cm-builtin {
71
+ color: var(--jp-mirror-editor-builtin-color);
72
+ }
73
+
70
74
  .cm-searching,
71
75
  .cm-searching span {
72
76
  /* `.cm-searching span`: we need to override syntax highlighting */