Welcome to mirror list, hosted at ThFree Co, Russian Federation.

source_editor_instance.js « editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e0ca4ea518b50da6f3bb06f8cdbaf5635f121866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
 * @module source_editor_instance
 */

/**
 * A Source Editor Extension definition
 * @typedef {Object} SourceEditorExtensionDefinition
 * @property {Object} definition
 * @property {Object} setupOptions
 */

/**
 * A Source Editor Extension
 * @typedef {Object} SourceEditorExtension
 * @property {Object} obj
 * @property {string} name
 * @property {Object} api
 */

import { isEqual } from 'lodash';
import { editor as monacoEditor } from 'monaco-editor';
import { getBlobLanguage } from '~/editor/utils';
import { logError } from '~/lib/logger';
import { sprintf } from '~/locale';
import EditorExtension from './source_editor_extension';
import {
  EDITOR_EXTENSION_DEFINITION_TYPE_ERROR,
  EDITOR_EXTENSION_NAMING_CONFLICT_ERROR,
  EDITOR_EXTENSION_NO_DEFINITION_ERROR,
  EDITOR_EXTENSION_NOT_REGISTERED_ERROR,
  EDITOR_EXTENSION_NOT_SPECIFIED_FOR_UNUSE_ERROR,
  EDITOR_EXTENSION_STORE_IS_MISSING_ERROR,
} from './constants';

const utils = {
  removeExtFromMethod: (method, extensionName, container) => {
    if (!container) {
      return;
    }
    if (Object.prototype.hasOwnProperty.call(container, method)) {
      // eslint-disable-next-line no-param-reassign
      delete container[method];
    }
  },

  getStoredExtension: (extensionsStore, name) => {
    if (!extensionsStore) {
      logError(EDITOR_EXTENSION_STORE_IS_MISSING_ERROR);
      return undefined;
    }
    return extensionsStore.get(name);
  },
};

/** Class representing a Source Editor Instance */
export default class EditorInstance {
  /**
   * Create a Source Editor Instance
   * @param {Object} rootInstance - Monaco instance to build on top of
   * @param {Map} extensionsStore - The global registry for the extension instances
   * @returns {Object} - A Proxy returning props/methods from either registered extensions, or Source Editor instance, or underlying Monaco instance
   */
  constructor(rootInstance = {}, extensionsStore = new Map()) {
    /** The methods provided by extensions. */
    this.methods = {};

    const seInstance = this;
    const getHandler = {
      get(target, prop, receiver) {
        const methodExtension =
          Object.prototype.hasOwnProperty.call(seInstance.methods, prop) &&
          seInstance.methods[prop];
        if (methodExtension) {
          const extension = extensionsStore.get(methodExtension);

          return (...args) => {
            return extension.api[prop].call(seInstance, ...args, receiver);
          };
        }
        return Reflect.get(seInstance[prop] ? seInstance : target, prop, receiver);
      },
      set(target, prop, value) {
        Object.assign(seInstance, {
          [prop]: value,
        });
        return true;
      },
    };
    const instProxy = new Proxy(rootInstance, getHandler);

    /**
     * Main entry point to apply an extension to the instance
     * @param {SourceEditorExtensionDefinition}
     */
    this.use = EditorInstance.useUnuse.bind(instProxy, extensionsStore, this.useExtension);

    /**
     * Main entry point to un-use an extension and remove it from the instance
     * @param {SourceEditorExtension}
     */
    this.unuse = EditorInstance.useUnuse.bind(instProxy, extensionsStore, this.unuseExtension);

    return instProxy;
  }

  /**
   * A private dispatcher function for both `use` and `unuse`
   * @param {Map} extensionsStore - The global registry for the extension instances
   * @param {Function} fn - A function to route to. Either `this.useExtension` or `this.unuseExtension`
   * @param {SourceEditorExtensionDefinition[]} extensions - The extensions to use/unuse.
   * @returns {Function}
   */
  static useUnuse(extensionsStore, fn, extensions) {
    if (Array.isArray(extensions)) {
      /**
       * We cut short if the Array is empty and let the destination function to throw
       * Otherwise, we run the destination function on every entry of the Array
       */
      return extensions.length
        ? extensions.map(fn.bind(this, extensionsStore))
        : fn.call(this, extensionsStore);
    }
    return fn.call(this, extensionsStore, extensions);
  }

  //
  // REGISTERING NEW EXTENSION
  //

  /**
   * Run all registrations when using an extension
   * @param {Map} extensionsStore - The global registry for the extension instances
   * @param {SourceEditorExtensionDefinition} extension - The extension definition to use.
   * @returns {EditorExtension|*}
   */
  useExtension(extensionsStore, extension = {}) {
    const { definition } = extension;
    if (!definition) {
      throw new Error(EDITOR_EXTENSION_NO_DEFINITION_ERROR);
    }
    if (typeof definition !== 'function') {
      throw new Error(EDITOR_EXTENSION_DEFINITION_TYPE_ERROR);
    }

    // Existing Extension Path
    const existingExt = utils.getStoredExtension(extensionsStore, definition.name);
    if (existingExt) {
      if (isEqual(extension.setupOptions, existingExt.setupOptions)) {
        return existingExt;
      }
      this.unuseExtension(extensionsStore, existingExt);
    }

    // New Extension Path
    const extensionInstance = new EditorExtension(extension);
    const { setupOptions, obj: extensionObj } = extensionInstance;
    if (extensionObj.onSetup) {
      extensionObj.onSetup(setupOptions, this);
    }
    if (extensionsStore) {
      this.registerExtension(extensionInstance, extensionsStore);
    }
    this.registerExtensionMethods(extensionInstance);
    return extensionInstance;
  }

  /**
   * Register extension in the global extensions store
   * @param {SourceEditorExtension} extension - Instance of Source Editor extension
   * @param {Map} extensionsStore - The global registry for the extension instances
   */
  registerExtension(extension, extensionsStore) {
    const { name } = extension;
    const hasExtensionRegistered =
      extensionsStore.has(name) &&
      isEqual(extension.setupOptions, extensionsStore.get(name).setupOptions);
    if (hasExtensionRegistered) {
      return;
    }
    extensionsStore.set(name, extension);
    const { obj: extensionObj } = extension;
    if (extensionObj.onUse) {
      extensionObj.onUse(this);
    }
  }

  /**
   * Register extension methods in the registry on the instance
   * @param {SourceEditorExtension} extension - Instance of Source Editor extension
   */
  registerExtensionMethods(extension) {
    const { api, name } = extension;

    if (!api) {
      return;
    }

    Object.keys(api).forEach((prop) => {
      if (this[prop]) {
        logError(sprintf(EDITOR_EXTENSION_NAMING_CONFLICT_ERROR, { prop }));
      } else {
        this.methods[prop] = name;
      }
    }, this);
  }

  //
  // UNREGISTERING AN EXTENSION
  //

  /**
   * Unregister extension with the cleanup
   * @param {Map} extensionsStore - The global registry for the extension instances
   * @param {SourceEditorExtension} extension - Instance of Source Editor extension to un-use
   */
  unuseExtension(extensionsStore, extension) {
    if (!extension) {
      throw new Error(EDITOR_EXTENSION_NOT_SPECIFIED_FOR_UNUSE_ERROR);
    }
    const { name } = extension;
    const existingExt = utils.getStoredExtension(extensionsStore, name);
    if (!existingExt) {
      throw new Error(sprintf(EDITOR_EXTENSION_NOT_REGISTERED_ERROR, { name }));
    }
    const { obj: extensionObj } = existingExt;
    if (extensionObj.onBeforeUnuse) {
      extensionObj.onBeforeUnuse(this);
    }
    this.unregisterExtensionMethods(existingExt);
    if (extensionObj.onUnuse) {
      extensionObj.onUnuse(this);
    }
  }

  /**
   * Remove all methods associated with this extension from the registry on the instance
   * @param {SourceEditorExtension} extension - Instance of Source Editor extension to un-use
   */
  unregisterExtensionMethods(extension) {
    const { api, name } = extension;
    if (!api) {
      return;
    }
    Object.keys(api).forEach((method) => {
      utils.removeExtFromMethod(method, name, this.methods);
    });
  }

  /**
   * PUBLIC API OF AN INSTANCE
   */

  /**
   * Updates model language based on the path
   * @param {String} path - blob path
   */
  updateModelLanguage(path) {
    const lang = getBlobLanguage(path);
    const model = this.getModel();
    // return monacoEditor.setModelLanguage(model, lang);
    monacoEditor.setModelLanguage(model, lang);
  }

  /**
   * Get the methods returned by extensions.
   * @returns {Array}
   */
  get extensionsAPI() {
    return Object.keys(this.methods);
  }
}