-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathhintTextPane.tsx
More file actions
61 lines (50 loc) · 2.09 KB
/
hintTextPane.tsx
File metadata and controls
61 lines (50 loc) · 2.09 KB
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
import * as React from 'react';
import { addHintText } from 'roosterjs-content-model-plugins';
import { ApiPaneProps, ApiPlaygroundComponent } from '../ApiPaneProps';
import { getSelectedSegmentsAndParagraphs } from 'roosterjs-content-model-dom';
import type { PluginEvent } from 'roosterjs-content-model-types';
export default class HintTextPane extends React.Component<ApiPaneProps, {}>
implements ApiPlaygroundComponent {
private word: React.RefObject<HTMLInputElement> = React.createRef();
private hintText: React.RefObject<HTMLInputElement> = React.createRef();
constructor(props: ApiPaneProps) {
super(props);
this.state = {};
}
render() {
return (
<>
<div>
When type word: <input type="text" ref={this.word} />
</div>
<div>
Insert hint text: <input type="text" ref={this.hintText} />
</div>
</>
);
}
onPluginEvent(e: PluginEvent) {
const word = this.word.current.value;
const hintText = this.hintText.current.value;
const editor = this.props.getEditor();
if (e.eventType == 'input' && word && hintText) {
let shouldAdd = false;
// Demo code only, do not do this in real production
editor.formatContentModel(model => {
const selections = getSelectedSegmentsAndParagraphs(model, false, false);
if (selections.length == 1 && selections[0][0].segmentType == 'SelectionMarker') {
const [segment, paragraph] = selections[0];
const index = paragraph.segments.indexOf(segment);
const text = index > 0 ? paragraph.segments[index - 1] : null;
if (text.segmentType == 'Text' && text.text.endsWith(this.word.current.value)) {
shouldAdd = true;
}
}
return false;
});
if (shouldAdd) {
addHintText(editor, hintText);
}
}
}
}