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

example_source_editor_extension.js « extensions « editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 119a2aea9eb2220bd6ffed9e2175f790b6009b52 (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
// THIS IS AN EXAMPLE
//
// This file contains a basic documented example of the Source Editor extensions'
// API for your convenience. You can copy/paste it into your own file
// and adjust as you see fit
//

export class MyFancyExtension {
  /**
   * THE LIFE-CYCLE CALLBACKS
   */

  /**
   * Is called before the extension gets used by an instance,
   * Use `onSetup` to setup Monaco directly:
   * actions, keystrokes, update options, etc.
   * Is called only once before the extension gets registered
   *
   * @param { Object } [setupOptions]  The setupOptions object
   * @param { Object } [instance] The Source Editor instance
   */
  // eslint-disable-next-line class-methods-use-this,no-unused-vars
  onSetup(setupOptions, instance) {}

  /**
   * The first thing called after the extension is
   * registered and used by an instance.
   * Is called every time the extension is applied
   *
   * @param { Object } [instance] The Source Editor instance
   */
  // eslint-disable-next-line class-methods-use-this,no-unused-vars
  onUse(instance) {}

  /**
   * Is called before un-using an extension. Can be used for time-critical
   * actions like cleanup, reverting visual changes, and other user-facing
   * updates.
   *
   * @param { Object } [instance] The Source Editor instance
   */
  // eslint-disable-next-line class-methods-use-this,no-unused-vars
  onBeforeUnuse(instance) {}

  /**
   * Is called right after an extension is removed from an instance (un-used)
   * Can be used for non time-critical tasks like cleanup on the Monaco level
   * (removing actions, keystrokes, etc.).
   * onUnuse() will be executed during the browser's idle period
   * (https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback)
   *
   * @param { Object } [instance] The Source Editor instance
   */
  // eslint-disable-next-line class-methods-use-this,no-unused-vars
  onUnuse(instance) {}

  /**
   * The public API of the extension: these are the methods that will be exposed
   * to the end user
   * @returns {Object}
   */
  provides() {
    return {
      basic: () => {
        // The most basic method not depending on anything
        // Use: instance.basic();
        // eslint-disable-next-line @gitlab/require-i18n-strings
        return 'Foo Bar';
      },
      basicWithProp: () => {
        // The methods with access to the props of the extension.
        // The props can be either hardcoded (for example in `onSetup`), or
        // can be dynamically passed as part of `setupOptions` object when
        // using the extension.
        // Use: instance.use({ definition: MyFancyExtension, setupOptions: { foo: 'bar' }});
        return this.foo;
      },
      basicWithPropsAsList: (prop1, prop2) => {
        // Just a simple method with local props
        // The props are passed as usually.
        // Use: instance.basicWithPropsAsList(prop1, prop2);
        // eslint-disable-next-line @gitlab/require-i18n-strings
        return `The prop1 is ${prop1}; the prop2 is ${prop2}`;
      },
      basicWithInstance: (instance) => {
        // The method accessing the instance methods: either own or provided
        // by previously-registered extensions
        // `instance` is always supplied to all methods in provides() as THE LAST
        // argument.
        // You don't need to explicitly pass instance to this method:
        // Use: instance.basicWithInstance();
        // eslint-disable-next-line @gitlab/require-i18n-strings
        return `We have access to the whole Instance! ${instance.alpha()}`;
      },
      advancedWithInstanceAndProps: ({ author, book } = {}, firstname, lastname, instance) => {
        // Advanced method where
        // { author, book } — are the props passed as an object
        // prop1, prop2    — are the props passed as simple list
        // instance        — is automatically supplied, no need to pass it to
        //                   the method explicitly
        // Use: instance.advancedWithInstanceAndProps(
        //     {
        //       author: 'Franz Kafka',
        //       book: 'The Transformation'
        //     },
        //     'Franz',
        //     'Kafka'
        //   );
        return `
The author is ${author}; the book is ${book}
The author's name is ${firstname}; the last name is ${lastname}
We have access to the whole Instance! For example, 'instance.alpha()': ${instance.alpha()}`;
      },
    };
  }
}