@difizen/libro-cofine-editor 0.1.13 → 0.1.15

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.
@@ -1,6 +1,8 @@
1
1
  import type {
2
2
  CodeEditorFactory,
3
3
  CompletionProvider,
4
+ EditorState,
5
+ EditorStateFactory,
4
6
  ICoordinate,
5
7
  IEditor,
6
8
  IEditorConfig,
@@ -18,6 +20,8 @@ import { NotebookCommands } from '@difizen/libro-core';
18
20
  import type { LSPProvider } from '@difizen/libro-lsp';
19
21
  import {
20
22
  CommandRegistry,
23
+ Deferred,
24
+ getOrigin,
21
25
  inject,
22
26
  ThemeService,
23
27
  transient,
@@ -35,6 +39,8 @@ import type { MonacoEditorOptions, MonacoEditorType, MonacoMatch } from './types
35
39
  import { MonacoRange, MonacoUri } from './types.js';
36
40
  import './index.less';
37
41
 
42
+ import * as monaco from '@difizen/monaco-editor-core';
43
+
38
44
  export interface LibroE2EditorConfig extends IEditorConfig {
39
45
  /**
40
46
  * The mode to use.
@@ -211,6 +217,9 @@ export interface LibroE2EditorOptions extends IEditorOptions {
211
217
  config?: Partial<LibroE2EditorConfig>;
212
218
  }
213
219
 
220
+ export const LibroE2EditorState = Symbol('LibroE2EditorState');
221
+ export type LibroE2EditorState = EditorState<E2EditorState>;
222
+
214
223
  export const libroE2DefaultConfig: Required<LibroE2EditorConfig> = {
215
224
  ...defaultConfig,
216
225
  theme: {
@@ -272,8 +281,39 @@ export const E2EditorClassname = 'libro-e2-editor';
272
281
 
273
282
  export const LibroE2URIScheme = 'libro-e2';
274
283
 
284
+ export type E2EditorState = monaco.editor.ITextModel | null;
285
+
286
+ export const e2StateFactory: (
287
+ languageSpecRegistry: LanguageSpecRegistry,
288
+ ) => EditorStateFactory<E2EditorState> = (languageSpecRegistry) => (options) => {
289
+ const spec = languageSpecRegistry.languageSpecs.find(
290
+ (item) => item.mime === options.model.mimeType,
291
+ );
292
+ const uri = MonacoUri.from({
293
+ scheme: LibroE2URIScheme,
294
+ path: `${options.uuid}${spec?.ext[0]}`,
295
+ });
296
+ const monacoModel = monaco.editor.createModel(
297
+ options.model.value,
298
+ spec?.language,
299
+ uri,
300
+ );
301
+ return {
302
+ state: monacoModel,
303
+ toJSON: () => {
304
+ return {};
305
+ },
306
+ dispose: () => {
307
+ monacoModel.dispose();
308
+ },
309
+ } as EditorState<E2EditorState>;
310
+ };
311
+
275
312
  @transient()
276
313
  export class LibroE2Editor implements IEditor {
314
+ protected editorReadyDeferred = new Deferred<void>();
315
+ editorReady = this.editorReadyDeferred.promise;
316
+
277
317
  protected readonly themeService: ThemeService;
278
318
 
279
319
  protected readonly languageSpecRegistry: LanguageSpecRegistry;
@@ -299,7 +339,7 @@ export class LibroE2Editor implements IEditor {
299
339
 
300
340
  protected _config: LibroE2EditorConfig;
301
341
 
302
- private resizeObserver: ResizeObserver;
342
+ private intersectionObserver: IntersectionObserver;
303
343
 
304
344
  private editorContentHeight: number;
305
345
 
@@ -322,6 +362,8 @@ export class LibroE2Editor implements IEditor {
322
362
  return this._model;
323
363
  }
324
364
 
365
+ editorState: EditorState<E2EditorState>;
366
+
325
367
  protected _editor?: E2Editor<MonacoEditorType>;
326
368
  get editor(): E2Editor<MonacoEditorType> | undefined {
327
369
  return this?._editor;
@@ -344,6 +386,7 @@ export class LibroE2Editor implements IEditor {
344
386
  protected isLayouting = false;
345
387
  constructor(
346
388
  @inject(LibroE2EditorOptions) options: LibroE2EditorOptions,
389
+ @inject(LibroE2EditorState) state: LibroE2EditorState,
347
390
  @inject(ThemeService) themeService: ThemeService,
348
391
  @inject(LanguageSpecRegistry)
349
392
  languageSpecRegistry: LanguageSpecRegistry,
@@ -370,6 +413,7 @@ export class LibroE2Editor implements IEditor {
370
413
  this.editorHost = document.createElement('div');
371
414
  this.host.append(this.editorHost);
372
415
 
416
+ this.editorState = state;
373
417
  this.createEditor(this.editorHost, fullConfig);
374
418
 
375
419
  this.onMimeTypeChanged();
@@ -407,7 +451,7 @@ export class LibroE2Editor implements IEditor {
407
451
  lineDecorationsWidth: 15,
408
452
  lineNumbersMinChars: 3,
409
453
  suggestSelection: 'first',
410
- wordBasedSuggestions: 'off',
454
+ wordBasedSuggestions: false,
411
455
  scrollBeyondLastLine: false,
412
456
  /**
413
457
  * 使用该选项可以让modal widget出现在正确的范围,而不是被遮挡,解决z-index问题,但是会导致hover组件之类的无法被选中
@@ -441,6 +485,19 @@ export class LibroE2Editor implements IEditor {
441
485
  'semanticHighlighting.enabled': true,
442
486
  maxTokenizationLineLength: 10000,
443
487
  // wrappingStrategy: 'advanced',
488
+ hover: {
489
+ enabled: true,
490
+ },
491
+ };
492
+ }
493
+
494
+ getState(): EditorState<E2EditorState> {
495
+ const cursorPosition = this.getCursorPosition();
496
+ const selections = this.getSelections();
497
+ return {
498
+ ...this.editorState,
499
+ cursorPosition,
500
+ selections,
444
501
  };
445
502
  }
446
503
 
@@ -459,10 +516,7 @@ export class LibroE2Editor implements IEditor {
459
516
  const editorPorvider =
460
517
  MonacoEnvironment.container.get<EditorProvider>(EditorProvider);
461
518
 
462
- const uri = MonacoUri.from({
463
- scheme: LibroE2URIScheme,
464
- path: `${this.uuid}${this.languageSpec.ext[0]}`,
465
- });
519
+ const model = this.editorState.state;
466
520
 
467
521
  const options: MonacoEditorOptions = {
468
522
  ...this.toMonacoOptions(editorConfig),
@@ -470,13 +524,11 @@ export class LibroE2Editor implements IEditor {
470
524
  * language ia an uri:
471
525
  */
472
526
  language: this.languageSpec.language,
473
- uri,
474
527
  theme: this.theme,
475
- value: this.model.value,
528
+ model: getOrigin(model),
476
529
  };
477
530
 
478
- const e2Editor = editorPorvider.create(host, options);
479
- this._editor = e2Editor;
531
+ this._editor = editorPorvider.create(host, options);
480
532
  this.toDispose.push(
481
533
  this.monacoEditor?.onDidChangeModelContent(() => {
482
534
  const value = this.monacoEditor?.getValue();
@@ -503,6 +555,7 @@ export class LibroE2Editor implements IEditor {
503
555
  config.placeholder,
504
556
  this.monacoEditor!,
505
557
  );
558
+ this.editorReadyDeferred.resolve();
506
559
 
507
560
  // console.log(
508
561
  // 'editor._themeService.getColorTheme()',
@@ -517,16 +570,12 @@ export class LibroE2Editor implements IEditor {
517
570
  }
518
571
 
519
572
  protected inspectResize() {
520
- // this.resizeObserver = new ResizeObserver((entries) => {
521
- // entries.forEach((entry) => {
522
- // const isVisible =
523
- // entry.contentRect.width !== 0 && entry.contentRect.height !== 0;
524
- // if (isVisible) {
525
- // this.updateEditorSize();
526
- // }
527
- // });
528
- // });
529
- // this.resizeObserver.observe(this.host);
573
+ this.intersectionObserver = new IntersectionObserver((entries) => {
574
+ if (entries[0].isIntersecting) {
575
+ this.updateEditorSize();
576
+ }
577
+ });
578
+ this.intersectionObserver.observe(this.host);
530
579
  }
531
580
 
532
581
  protected getEditorNode() {
@@ -735,7 +784,9 @@ export class LibroE2Editor implements IEditor {
735
784
  this.monacoEditor?.trigger('source', 'redo', {});
736
785
  };
737
786
  focus = () => {
738
- this.monacoEditor?.focus();
787
+ window.requestAnimationFrame(() => {
788
+ this.monacoEditor?.focus();
789
+ });
739
790
  };
740
791
  hasFocus = () => {
741
792
  return this.monacoEditor?.hasWidgetFocus() ?? false;
@@ -933,8 +984,8 @@ export class LibroE2Editor implements IEditor {
933
984
  }
934
985
 
935
986
  disposeResizeObserver = () => {
936
- if (this.resizeObserver) {
937
- this.resizeObserver.disconnect();
987
+ if (this.intersectionObserver) {
988
+ getOrigin(this.intersectionObserver).disconnect();
938
989
  }
939
990
  };
940
991
  }
package/src/module.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IEditorOptions } from '@difizen/libro-code-editor';
1
+ import type { EditorState, IEditorOptions } from '@difizen/libro-code-editor';
2
2
  import { CodeEditorModule } from '@difizen/libro-code-editor';
3
3
  import { ManaModule } from '@difizen/mana-app';
4
4
 
@@ -12,6 +12,7 @@ import {
12
12
  LibroE2Editor,
13
13
  LibroE2EditorFactory,
14
14
  LibroE2EditorOptions,
15
+ LibroE2EditorState,
15
16
  } from './libro-e2-editor.js';
16
17
  import { loadE2 } from './libro-e2-preload.js';
17
18
  import { LibroSQLRequestAPI } from './libro-sql-api.js';
@@ -26,9 +27,10 @@ export const LibroE2EditorModule = ManaModule.create()
26
27
  {
27
28
  token: LibroE2EditorFactory,
28
29
  useFactory: (ctx) => {
29
- return (options: IEditorOptions) => {
30
+ return (options: IEditorOptions, editorState: EditorState) => {
30
31
  const child = ctx.container.createChild();
31
32
  child.register({ token: LibroE2EditorOptions, useValue: options });
33
+ child.register({ token: LibroE2EditorState, useValue: editorState });
32
34
  return child.get(LibroE2Editor);
33
35
  };
34
36
  },