-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathtext-editor.ts
214 lines (191 loc) · 5.23 KB
/
text-editor.ts
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
import { attr, DOM, observable, ViewTemplate } from "@microsoft/fast-element";
import { eventFocusIn, eventFocusOut } from "@microsoft/fast-web-utilities";
import { createEditor, IEditor } from "roosterjs";
import { FoundationElement } from "../foundation-element";
import {
AnchoredRegion,
AnchoredRegionConfig,
FlyoutPosBottom,
FlyoutPosTallest,
FlyoutPosTop,
} from "../anchored-region";
import type { TextEditorToolbar } from "./text-editor-toolbar";
/**
* Defines the vertical positioning options for an anchored region
*
* @beta
*/
export type toolbarPlacement = "bottom" | "tallest" | "top";
/**
* A text editor Custom HTML Element.
*
* @public
*/
export class TextEditor extends FoundationElement {
/**
* Controls menu placement
*
* @public
* @remarks
* HTML Attribute: menu-placement
*/
@attr({ attribute: "toolbar-placement" })
public toolbarPlacement: toolbarPlacement = "tallest";
private toolbarPlacementChanged(): void {
if (this.$fastController.isConnected) {
this.updateToolbarConfig();
}
}
/**
* Sets the template to use to generate the toolbar.
* Set the the text editor.
*
* @public
*/
@observable
public toolbarTemplate: ViewTemplate<TextEditorToolbar>;
private toolbarTemplateChanged(): void {
if (this.$fastController.isConnected && this.toolbarElement) {
this.toolbarElement.toolbarTemplate = this.toolbarTemplate;
}
}
/**
*
*
* @public
*/
@observable
public toolbarResources: object;
private toolbarResourcesChanged(): void {
if (this.$fastController.isConnected && this.toolbarElement) {
this.toolbarElement.resources = this.toolbarResources;
}
}
/**
* Controls toolbar visibility
*
* @internal
*/
@observable
public showToolbar: boolean = false;
private showToolbarChanged(): void {
if (this.$fastController.isConnected) {
if (this.showToolbar) {
// this.addToolbarElement();
DOM.queueUpdate(this.setRegionProps);
this.$emit("toolbaropening");
} else {
// this.removeToolbarElement();
this.$emit("toolbarclosing");
}
}
}
/**
* The anchored region config to apply.
*
* @internal
*/
@observable
public toolbarConfig: AnchoredRegionConfig;
/**
* reference to the anchored region element
*
* @internal
*/
public region: AnchoredRegion;
/**
* The tag for the text editor toolbar element (ie. "fast-text-editor-toolbar" vs. "fluent-text-editor-toolbar")
*
* @internal
*/
@observable
public toolbarTag: string;
/**
* reference to the editor host div
*
* @internal
*/
public editorHost: HTMLDivElement;
public editor: IEditor | undefined;
private toolbarElement: TextEditorToolbar | undefined;
/**
* @internal
*/
public connectedCallback(): void {
super.connectedCallback();
this.editor = createEditor(this.editorHost);
this.addEventListener(eventFocusIn, this.handleFocusIn);
this.addEventListener(eventFocusOut, this.handleFocusOut);
this.updateToolbarConfig();
}
/**
* @internal
*/
public disconnectedCallback() {
this.editor = undefined;
this.removeEventListener(eventFocusIn, this.handleFocusIn);
this.removeEventListener(eventFocusOut, this.handleFocusOut);
super.disconnectedCallback();
}
/**
* @internal
*/
public handleFocusIn(e: FocusEvent): void {
this.showToolbar = true;
}
/**
* @internal
*/
public handleFocusOut(e: FocusEvent): void {
if (!e.relatedTarget || !this.contains(e.relatedTarget as Node)) {
this.showToolbar = false;
}
}
/**
* Anchored region is loaded
*
* @internal
*/
public handleRegionLoaded(e: Event): void {
DOM.queueUpdate(() => {
this.$emit("toolbarloaded", { bubbles: false });
});
}
/**
* Sets properties on the anchored region once it is instanciated.
*/
private setRegionProps = (): void => {
if (!this.showToolbar) {
return;
}
if (this.region === null || this.region === undefined) {
DOM.queueUpdate(this.setRegionProps);
return;
}
this.region.anchorElement = this.editorHost;
};
/**
* Updates the toolbar config
*/
private updateToolbarConfig(): void {
let newConfig = this.configLookup[this.toolbarPlacement];
if (newConfig === null) {
newConfig = FlyoutPosBottom;
}
this.toolbarConfig = {
...newConfig,
autoUpdateMode: "auto",
fixedPlacement: true,
horizontalViewportLock: false,
verticalViewportLock: false,
};
}
/**
* matches menu placement values with the associated menu config
*/
private configLookup: object = {
top: FlyoutPosTop,
bottom: FlyoutPosBottom,
tallest: FlyoutPosTallest,
};
}