Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): optimize editor focus #1616

Merged
merged 4 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function monacoBuildTask() {
entryPoints: monacoEditorEntryPoints.map((entry) => `./node_modules/monaco-editor/esm/${entry}`),
bundle: true,
color: true,
target: 'chrome70',
format: 'iife',
outbase: './node_modules/monaco-editor/esm/',
outdir: './local',
Expand All @@ -42,6 +43,7 @@ const ctx = await esbuild[args.watch ? 'context' : 'build']({
loader: { '.svg': 'file', '.ttf': 'file' },
sourcemap: args.watch,
minify: !args.watch,
target: 'chrome70',
plugins: [
copyPlugin({
assets: {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/shared/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export function handleStyleToString(style: IStyleData, isCell: boolean = false)
if (style.ul?.s) {
// If there are existing lines, add new lines
if (str.indexOf('text-decoration-line') > -1) {
str = str.replace(/(?<=text-decoration-line:.*)\b(?=;)/g, ' underline');
str = str.replace(/(text-decoration-line:\s*[^;]+)(?=;)/g, (_, p1) => `${p1} underline`);
} else {
str += 'text-decoration-line: underline; ';
}
Expand All @@ -271,7 +271,7 @@ export function handleStyleToString(style: IStyleData, isCell: boolean = false)
() => {
if (style.st?.s) {
if (str.indexOf('text-decoration-line') > -1) {
str = str.replace(/(?<=text-decoration-line:.*)\b(?=;)/g, ' line-through');
str = str.replace(/(text-decoration-line:\s*[^;]+)(?=;)/g, (_, p1) => `${p1} line-through`);
} else {
str += 'text-decoration-line: line-through; ';
}
Expand All @@ -289,7 +289,7 @@ export function handleStyleToString(style: IStyleData, isCell: boolean = false)
() => {
if (style.ol?.s) {
if (str.indexOf('text-decoration-line') > -1) {
str = str.replace(/(?<=text-decoration-line:.*)\b(?=;)/g, ' overline');
str = str.replace(/(text-decoration-line:\s*[^;]+)(?=;)/g, (_, p1) => `${p1} overline`);
} else {
str += 'text-decoration-line: overline; ';
}
Expand Down
25 changes: 13 additions & 12 deletions packages/docs-ui/src/controllers/text-selection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,23 @@ export class TextSelectionController extends Disposable {
this._currentUniverService.setCurrentUniverDocInstance(unitId);
}

this._textSelectionRenderManager.eventTrigger(evt);

const { offsetX, offsetY } = evt;

if (this._editorService.getEditor(unitId)) {
/**
* To accommodate focus switching between different editors.
* Since the editor for Univer is canvas-based,
* it primarily relies on focus and cannot use the focus event.
* Our editor's focus monitoring is based on PointerDown.
* The order of occurrence is such that PointerDown comes first.
* Translate the above text into English.
*/
/**
* To accommodate focus switching between different editors.
* Since the editor for Univer is canvas-based,
* it primarily relies on focus and cannot use the focus event.
* Our editor's focus monitoring is based on PointerDown.
* The order of occurrence is such that PointerDown comes first.
* Translate the above text into English.
*/
setTimeout(() => {
this._textSelectionRenderManager.eventTrigger(evt);

this._setEditorFocus(unitId);
this._textSelectionRenderManager.setCursorManually(offsetX, offsetY);
}, 0);
} else {
this._textSelectionRenderManager.eventTrigger(evt);
}

if (evt.button !== 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ export interface ITextSelectionRenderManager {
handleTripleClick(evt: IPointerEvent | IMouseEvent): void;

eventTrigger(evt: IPointerEvent | IMouseEvent): void;

setCursorManually(evtOffsetX: number, evtOffsetY: number): void;
}

export interface IEditorInputConfig {
Expand Down Expand Up @@ -442,6 +444,26 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this.addTextRanges(textRanges, false);
}

setCursorManually(evtOffsetX: number, evtOffsetY: number) {
const startNode = this._findNodeByCoord(evtOffsetX, evtOffsetY);

const position = this._getNodePosition(startNode);

if (position == null) {
this._removeAllTextRanges();

return;
}

if (startNode?.node.streamType === DataStreamTreeTokenType.PARAGRAPH) {
position.isBack = true;
}

this._updateTextRangeAnchorPosition(position);

this._activeSelectionRefresh();
}

// Handle pointer down.
eventTrigger(evt: IPointerEvent | IMouseEvent) {
if (!this._scene || !this._isSelectionEnabled) {
Expand Down
Loading