|
| 1 | +import * as MonacoTypes from 'monaco-editor/esm/vs/editor/editor.api' |
| 2 | +import isEqual from 'lodash/isEqual' |
| 3 | + |
| 4 | +import { |
| 5 | + DEFAULT_SELECTION, |
| 6 | + CompositionSelection, |
| 7 | +} from 'src/dataExplorer/context/persistance' |
| 8 | + |
| 9 | +// Types |
| 10 | +import {EditorType, RecursivePartial} from 'src/types' |
| 11 | +import {LspRange} from 'src/languageSupport/languages/agnostic/types' |
| 12 | + |
| 13 | +// Utils |
| 14 | +import {notify} from 'src/shared/actions/notifications' |
| 15 | +import {oldSession} from 'src/shared/copy/notifications' |
| 16 | + |
| 17 | +export class ConnectionManager { |
| 18 | + protected _editor: EditorType |
| 19 | + protected _model: MonacoTypes.editor.IModel |
| 20 | + protected _compositionStyle: string[] = [] |
| 21 | + protected _session: CompositionSelection = JSON.parse( |
| 22 | + JSON.stringify(DEFAULT_SELECTION) |
| 23 | + ) |
| 24 | + protected _callbackSetSession: ( |
| 25 | + schema: RecursivePartial<CompositionSelection> |
| 26 | + ) => void = () => null |
| 27 | + protected _dispatcher = _ => {} |
| 28 | + protected _first_load = true |
| 29 | + protected _compositionRange: LspRange = null |
| 30 | + |
| 31 | + subscribeToModel(editor: EditorType) { |
| 32 | + this._editor = editor |
| 33 | + this._model = editor.getModel() |
| 34 | + } |
| 35 | + |
| 36 | + _setSessionSync(synced: boolean) { |
| 37 | + this._callbackSetSession({ |
| 38 | + composition: {synced}, |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + _compositionSyncStyle(startLine: number, endLine: number, synced: boolean) { |
| 43 | + const classNamePrefix = synced |
| 44 | + ? 'composition-sync--on' |
| 45 | + : 'composition-sync--off' |
| 46 | + |
| 47 | + // Customize the full width of Monaco editor margin using API `marginClassName` |
| 48 | + // https://github.com/microsoft/monaco-editor/blob/35eb0ef/website/typedoc/monaco.d.ts#L1533 |
| 49 | + const startLineStyle = { |
| 50 | + range: new MonacoTypes.Range(startLine, 1, startLine, 1), |
| 51 | + options: { |
| 52 | + marginClassName: `${classNamePrefix}--first`, |
| 53 | + }, |
| 54 | + } |
| 55 | + const middleLinesStyle = { |
| 56 | + range: new MonacoTypes.Range(startLine, 1, endLine, 1), |
| 57 | + options: { |
| 58 | + marginClassName: classNamePrefix, |
| 59 | + }, |
| 60 | + } |
| 61 | + const endLineStyle = { |
| 62 | + range: new MonacoTypes.Range(endLine, 1, endLine, 1), |
| 63 | + options: { |
| 64 | + marginClassName: `${classNamePrefix}--last`, |
| 65 | + }, |
| 66 | + } |
| 67 | + return [startLineStyle, middleLinesStyle, endLineStyle] |
| 68 | + } |
| 69 | + |
| 70 | + _setEditorBlockStyle(range: LspRange | null, synced: boolean = false) { |
| 71 | + this._compositionRange = range |
| 72 | + const shouldRemoveAllStyles = range == null |
| 73 | + |
| 74 | + this._compositionStyle = this._editor.deltaDecorations( |
| 75 | + this._compositionStyle, |
| 76 | + shouldRemoveAllStyles |
| 77 | + ? [] |
| 78 | + : this._compositionSyncStyle(range.start.line, range.end.line, synced) |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + _isNewScript( |
| 83 | + schema: CompositionSelection, |
| 84 | + previousState: CompositionSelection |
| 85 | + ): boolean { |
| 86 | + return previousState.bucket != null && schema.bucket == null |
| 87 | + } |
| 88 | + |
| 89 | + _diffSchemaChange( |
| 90 | + schema: CompositionSelection, |
| 91 | + previousState: CompositionSelection, |
| 92 | + defaultText: string |
| 93 | + ) { |
| 94 | + const toAdd: Partial<CompositionSelection> = {} |
| 95 | + const toRemove: Partial<CompositionSelection> = {} |
| 96 | + let shouldRemoveDefaultMsg = false |
| 97 | + |
| 98 | + if (this._isNewScript(schema, previousState)) { |
| 99 | + // no action to take. |
| 100 | + // `textDocument/didChange` --> will inform LSP to drop composition |
| 101 | + return {toAdd, toRemove, shouldRemoveDefaultMsg} |
| 102 | + } |
| 103 | + |
| 104 | + if (schema.bucket && previousState.bucket != schema.bucket) { |
| 105 | + toAdd.bucket = schema.bucket |
| 106 | + if (this._model.getValue() == defaultText) { |
| 107 | + shouldRemoveDefaultMsg = true |
| 108 | + } |
| 109 | + } |
| 110 | + if (schema.measurement && previousState.measurement != schema.measurement) { |
| 111 | + toAdd.measurement = schema.measurement |
| 112 | + } |
| 113 | + if (!isEqual(schema.fields, previousState.fields)) { |
| 114 | + const fieldsToRemove = previousState.fields.filter( |
| 115 | + f => !schema.fields.includes(f) |
| 116 | + ) |
| 117 | + if (fieldsToRemove.length) { |
| 118 | + toRemove.fields = fieldsToRemove |
| 119 | + } |
| 120 | + const fieldsToAdd = schema.fields.filter( |
| 121 | + f => !previousState.fields.includes(f) |
| 122 | + ) |
| 123 | + if (fieldsToAdd.length) { |
| 124 | + toAdd.fields = fieldsToAdd |
| 125 | + } |
| 126 | + } |
| 127 | + if (!isEqual(schema.tagValues, previousState.tagValues)) { |
| 128 | + const tagValuesToRemove = previousState.tagValues.filter( |
| 129 | + ({key, value}) => |
| 130 | + !schema.tagValues.some(pair => pair.value == value && pair.key == key) |
| 131 | + ) |
| 132 | + if (tagValuesToRemove.length) { |
| 133 | + toRemove.tagValues = tagValuesToRemove |
| 134 | + } |
| 135 | + const tagValuesToAdd = schema.tagValues.filter( |
| 136 | + ({key, value}) => |
| 137 | + !previousState.tagValues.some( |
| 138 | + pair => pair.value == value && pair.key == key |
| 139 | + ) |
| 140 | + ) |
| 141 | + if (tagValuesToAdd.length) { |
| 142 | + toAdd.tagValues = tagValuesToAdd |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return {toAdd, toRemove, shouldRemoveDefaultMsg} |
| 147 | + } |
| 148 | + |
| 149 | + _updateLocalState(schema: CompositionSelection, sessionCb, dispatch) { |
| 150 | + if (!schema.composition) { |
| 151 | + dispatch(notify(oldSession())) |
| 152 | + return {shouldContinue: false} |
| 153 | + } |
| 154 | + |
| 155 | + this._dispatcher = dispatch |
| 156 | + this._callbackSetSession = sessionCb |
| 157 | + const previousState = { |
| 158 | + ...this._session, |
| 159 | + composition: {...this._session?.composition}, |
| 160 | + } |
| 161 | + |
| 162 | + if (!schema.composition.synced) { |
| 163 | + this._session.composition.synced = false |
| 164 | + this._setEditorBlockStyle(this._compositionRange) |
| 165 | + return {shouldContinue: false} |
| 166 | + } |
| 167 | + const syncTurnedBackOn = |
| 168 | + schema.composition.synced && !previousState.composition.synced |
| 169 | + if (syncTurnedBackOn) { |
| 170 | + this._setEditorBlockStyle(this._compositionRange, true) |
| 171 | + } |
| 172 | + |
| 173 | + this._session = {...schema, composition: {...schema.composition}} |
| 174 | + return {previousState, shouldContinue: true} |
| 175 | + } |
| 176 | + |
| 177 | + dispose() { |
| 178 | + this._model.onDidChangeContent(null) |
| 179 | + } |
| 180 | +} |
0 commit comments