-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
index.ts
186 lines (169 loc) · 5.72 KB
/
index.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
import type { UIEventStateContext } from '@blocksuite/block-std';
import {
assertExists,
DisposableGroup,
throttle,
} from '@blocksuite/global/utils';
import type { EditorHost } from '@blocksuite/lit';
import { WidgetElement } from '@blocksuite/lit';
import type { BaseBlockModel } from '@blocksuite/store';
import { customElement } from 'lit/decorators.js';
import { isControlledKeyboardEvent } from '../../../_common/utils/event.js';
import { matchFlavours } from '../../../_common/utils/index.js';
import {
getInlineEditorByModel,
getViewportElement,
} from '../../../_common/utils/query.js';
import { getCurrentNativeRange } from '../../../_common/utils/selection.js';
import { getPopperPosition } from '../../../page-block/utils/position.js';
import { getMenus, type LinkedPageOptions } from './config.js';
import { LinkedPagePopover } from './linked-page-popover.js';
export function showLinkedPagePopover({
editorHost,
model,
range,
container = document.body,
abortController = new AbortController(),
options,
triggerKey,
}: {
editorHost: EditorHost;
model: BaseBlockModel;
range: Range;
container?: HTMLElement;
abortController?: AbortController;
options: LinkedPageOptions;
triggerKey: string;
}) {
const disposables = new DisposableGroup();
abortController.signal.addEventListener('abort', () => disposables.dispose());
const linkedPage = new LinkedPagePopover(model, abortController);
linkedPage.options = options;
linkedPage.triggerKey = triggerKey;
// Mount
container.appendChild(linkedPage);
disposables.add(() => linkedPage.remove());
// Handle position
const updatePosition = throttle(() => {
const linkedPageElement = linkedPage.linkedPageElement;
assertExists(
linkedPageElement,
'You should render the linked page node even if no position'
);
const position = getPopperPosition(linkedPageElement, range);
linkedPage.updatePosition(position);
}, 10);
disposables.addFromEvent(window, 'resize', updatePosition);
const scrollContainer = getViewportElement(editorHost);
if (scrollContainer) {
// Note: in edgeless mode, the scroll container is not exist!
disposables.addFromEvent(scrollContainer, 'scroll', updatePosition, {
passive: true,
});
}
// Wait for node to be mounted
setTimeout(updatePosition);
disposables.addFromEvent(window, 'mousedown', (e: Event) => {
if (e.target === linkedPage) return;
abortController.abort();
});
return linkedPage;
}
export const AFFINE_LINKED_PAGE_WIDGET = 'affine-linked-page-widget';
@customElement(AFFINE_LINKED_PAGE_WIDGET)
export class AffineLinkedPageWidget extends WidgetElement {
static DEFAULT_OPTIONS: LinkedPageOptions = {
/**
* The first item of the trigger keys will be the primary key
*/
triggerKeys: ['@', '[[', '【【'],
ignoreBlockTypes: ['affine:code'],
/**
* Convert trigger key to primary key (the first item of the trigger keys)
*/
convertTriggerKey: true,
getMenus,
};
options = AffineLinkedPageWidget.DEFAULT_OPTIONS;
override connectedCallback() {
super.connectedCallback();
this.handleEvent('keyDown', this._onKeyDown);
}
public showLinkedPage = (model: BaseBlockModel, triggerKey: string) => {
const curRange = getCurrentNativeRange();
showLinkedPagePopover({
editorHost: this.host,
model,
range: curRange,
options: this.options,
triggerKey,
});
};
private _onKeyDown = (ctx: UIEventStateContext) => {
const eventState = ctx.get('keyboardState');
const event = eventState.raw;
if (isControlledKeyboardEvent(event) || event.key.length !== 1) return;
const text = this.host.selection.value.find(selection =>
selection.is('text')
);
if (!text) {
return;
}
const model = this.host.page.getBlockById(text.blockId);
if (!model) {
return;
}
if (matchFlavours(model, this.options.ignoreBlockTypes)) return;
const inlineEditor = getInlineEditorByModel(model);
if (!inlineEditor) return;
const inlineRange = inlineEditor.getInlineRange();
if (!inlineRange) return;
if (inlineRange.length > 0) {
// When select text and press `[[` should not trigger transform,
// since it will break the bracket complete.
// Expected `[[selected text]]` instead of `@selected text]]`
return;
}
const [leafStart, offsetStart] = inlineEditor.getTextPoint(
inlineRange.index
);
const prefixText = leafStart.textContent
? leafStart.textContent.slice(0, offsetStart)
: '';
const matchedKey = this.options.triggerKeys.find(triggerKey =>
(prefixText + event.key).endsWith(triggerKey)
);
if (!matchedKey) return;
const primaryTriggerKey = this.options.triggerKeys[0];
inlineEditor.slots.inlineRangeApply.once(() => {
if (this.options.convertTriggerKey && primaryTriggerKey !== matchedKey) {
// Convert to the primary trigger key
// e.g. [[ -> @
const startIdxBeforeMatchKey =
inlineRange.index - (matchedKey.length - 1);
inlineEditor.deleteText({
index: startIdxBeforeMatchKey,
length: matchedKey.length,
});
inlineEditor.insertText(
{ index: startIdxBeforeMatchKey, length: 0 },
primaryTriggerKey
);
inlineEditor.setInlineRange({
index: startIdxBeforeMatchKey + primaryTriggerKey.length,
length: 0,
});
inlineEditor.slots.inlineRangeApply.once(() => {
this.showLinkedPage(model, primaryTriggerKey);
});
return;
}
this.showLinkedPage(model, matchedKey);
});
};
}
declare global {
interface HTMLElementTagNameMap {
[AFFINE_LINKED_PAGE_WIDGET]: AffineLinkedPageWidget;
}
}