Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/placeholderText/browser/placeholderTextContribution.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { h } from '../../../../base/browser/dom.js';
7
import { structuralEquals } from '../../../../base/common/equals.js';
8
import { Disposable } from '../../../../base/common/lifecycle.js';
9
import { autorun, constObservable, DebugOwner, derivedObservableWithCache, derivedOpts, derived, IObservable, IReader } from '../../../../base/common/observable.js';
10
import { ICodeEditor } from '../../../browser/editorBrowser.js';
11
import { observableCodeEditor } from '../../../browser/observableCodeEditor.js';
12
import { EditorOption } from '../../../common/config/editorOptions.js';
13
import { IEditorContribution } from '../../../common/editorCommon.js';
14
15
/**
16
* Use the editor option to set the placeholder text.
17
*/
18
export class PlaceholderTextContribution extends Disposable implements IEditorContribution {
19
public static get(editor: ICodeEditor): PlaceholderTextContribution {
20
return editor.getContribution<PlaceholderTextContribution>(PlaceholderTextContribution.ID)!;
21
}
22
23
public static readonly ID = 'editor.contrib.placeholderText';
24
private readonly _editorObs;
25
26
private readonly _placeholderText;
27
28
private readonly _state;
29
30
private readonly _shouldViewBeAlive;
31
32
private readonly _view;
33
34
constructor(
35
private readonly _editor: ICodeEditor,
36
) {
37
super();
38
this._editorObs = observableCodeEditor(this._editor);
39
this._placeholderText = this._editorObs.getOption(EditorOption.placeholder);
40
this._state = derivedOpts<{ placeholder: string } | undefined>({ owner: this, equalsFn: structuralEquals }, reader => {
41
const p = this._placeholderText.read(reader);
42
if (!p) { return undefined; }
43
if (!this._editorObs.valueIsEmpty.read(reader)) { return undefined; }
44
return { placeholder: p };
45
});
46
this._shouldViewBeAlive = isOrWasTrue(this, reader => this._state.read(reader)?.placeholder !== undefined);
47
this._view = derived((reader) => {
48
if (!this._shouldViewBeAlive.read(reader)) { return; }
49
50
const element = h('div.editorPlaceholder');
51
52
reader.store.add(autorun(reader => {
53
const data = this._state.read(reader);
54
const shouldBeVisibile = data?.placeholder !== undefined;
55
element.root.style.display = shouldBeVisibile ? 'block' : 'none';
56
element.root.innerText = data?.placeholder ?? '';
57
}));
58
reader.store.add(autorun(reader => {
59
const info = this._editorObs.layoutInfo.read(reader);
60
element.root.style.left = `${info.contentLeft}px`;
61
element.root.style.width = (info.contentWidth - info.verticalScrollbarWidth) + 'px';
62
element.root.style.top = `${this._editor.getTopForLineNumber(0)}px`;
63
}));
64
reader.store.add(autorun(reader => {
65
element.root.style.fontFamily = this._editorObs.getOption(EditorOption.fontFamily).read(reader);
66
element.root.style.fontSize = this._editorObs.getOption(EditorOption.fontSize).read(reader) + 'px';
67
element.root.style.lineHeight = this._editorObs.getOption(EditorOption.lineHeight).read(reader) + 'px';
68
}));
69
reader.store.add(this._editorObs.createOverlayWidget({
70
allowEditorOverflow: false,
71
minContentWidthInPx: constObservable(0),
72
position: constObservable(null),
73
domNode: element.root,
74
}));
75
});
76
this._view.recomputeInitiallyAndOnChange(this._store);
77
}
78
}
79
80
function isOrWasTrue(owner: DebugOwner, fn: (reader: IReader) => boolean): IObservable<boolean> {
81
return derivedObservableWithCache<boolean>(owner, (reader, lastValue) => {
82
if (lastValue === true) { return true; }
83
return fn(reader);
84
});
85
}
86
87