Skip to content

Commit

Permalink
feat: add canvas offset for slide editor pos
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Aug 5, 2024
1 parent 6589dff commit b2ad5ce
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/src/data/slides/default-pages/page1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const DEFAULT_FIRST_PAGE = {
zIndex: 2,
left: 430,
top: 42,
width: 100,
width: 120,
height: 40,
title: 'mask',
description: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export interface IRenderManagerService extends IDisposable {
createRender(unitId: string): IRender;
removeRender(unitId: string): void;
setCurrent(unitId: string): void;
/**
* get RenderUnit By Id, RenderUnit implements IRender
* @param unitId
*/
getRenderById(unitId: string): Nullable<IRender>;
getAllRenderersOfType(type: UniverInstanceType): RenderUnit[];
getCurrentTypeOfRenderer(type: UniverInstanceType): Nullable<RenderUnit>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export class SlideEditingRenderController extends Disposable implements IRenderM
}

private _initEditorVisibilityListener(): void {
// startEditing --> slide-editor-bridge.render-controller.ts@changeVisible --> _editorBridgeService.changeVisible
this.disposeWithMe(this._editorBridgeService.visible$
// .pipe(distinctUntilChanged((prev, curr) => prev.visible === curr.visible))
.subscribe((param) => {
Expand Down Expand Up @@ -242,7 +243,9 @@ export class SlideEditingRenderController extends Disposable implements IRenderM
* @param d DisposableCollection
*/
private _subscribeToCurrentCell(d: DisposableCollection) {
// invoked by slide-editor-bridge.service.ts@setEditorRectn---> currentEditRectState$.next
// first part of editing.
// startEditing --> _updateEditor --> slide-editor-bridge.service.ts@setEditorRect---> currentEditRectState$.next(editCellState)
// startEditing --> changeVisible
d.add(this._editorBridgeService.currentEditRectState$.subscribe((editCellState) => {
if (editCellState == null) {
return;
Expand Down Expand Up @@ -272,11 +275,13 @@ export class SlideEditingRenderController extends Disposable implements IRenderM
endOffset: 0,
}]);

// hide the editor, but the editor is still exist.
// the last active, calling stack trace:
// EditorContainer.tsx cellEditorManagerService.state$.subscribe -->
// EditorContainer.tsx cellEditorManagerService.setFocus(true) -->
// _textSelectionRenderManager.sync() --> _updateInputPosition --> activate

// hide the editor, but the editor is still exist.
// the last and valid call activate is in _textSelectionRenderManager.sync() --> _updateInputPosition
this._textSelectionRenderManager.activate(HIDDEN_EDITOR_POSITION, HIDDEN_EDITOR_POSITION);
}));
}
Expand Down Expand Up @@ -427,6 +432,9 @@ export class SlideEditingRenderController extends Disposable implements IRenderM

let { startX, startY } = actualRangeWithCoord;

startX += canvasOffset.left;
startY += canvasOffset.top;

const { document: documentComponent, scene, engine: docEngine } = editorObject;

const viewportMain = scene.getViewport(DOC_VIEWPORT_KEY.VIEW_MAIN);
Expand Down Expand Up @@ -546,10 +554,14 @@ export class SlideEditingRenderController extends Disposable implements IRenderM
}

/**
* show input area, resize input area and then place input to right place.
* invoked when this._editorBridgeService.visible$ param.visible = true
*
* handleVisible is the 2nd part of editing.
* startEditing --> _updateEditor
* startEditing --> changeVisible --> slide-editor-bridge.render-controller.ts@changeVisible --> _editorBridgeService.changeVisible
* @param param
*/

private _handleEditorVisible(param: IEditorBridgeServiceVisibleParam) {
const { eventType, keycode } = param;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,31 +104,37 @@ export class SlideEditorBridgeRenderController extends RxDisposable implements I

if (!transformer) return;

// calling twice when add an object.
// This is the start of editing.
//TODO: @lumixraku calling twice when add an object.
d.add(transformer.changeStart$.subscribe((param: IChangeObserverConfig) => {
const target = param.target;
if (!target) return;
if (target.objectType !== ObjectType.RICH_TEXT) {
this.saveCurrEditingState();
this.setEditorVisible(false);
this.endEditing();

// rm other text editor
this.changeVisible(false);
} else {
// const elementData = (target as RichText).toJson();
this._curRichText = target as RichText;
this.startEditing(target as RichText);
}
}));
}
}

saveCurrEditingState() {
/**
* invoked when picking other object.
*
* save editing state to curr richText.
*/
endEditing() {
if (!this._curRichText) return;
const curRichText = this._curRichText;

const slideData = this._instanceSrv.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
if (!slideData) return false;
curRichText.updateDocumentByDocData();
this._curRichText = null;
}

/**
Expand All @@ -140,11 +146,17 @@ export class SlideEditorBridgeRenderController extends RxDisposable implements I
*/
startEditing(target: RichText) {
// this.setSlideTextEditor$.next({ content, rect });
this._curRichText = target as RichText;
this._updateEditor(target);
this.changeVisible(true);
this.setEditorVisible(true);
}

changeVisible(visible: boolean) {
setEditorVisible(visible: boolean) {
if (visible) {
this._curRichText?.hide();
} else {
this._curRichText?.show();
}
const { unitId } = this._renderContext;
this._editorBridgeService.changeVisible({ visible, eventType: 3, unitId });
}
Expand Down
36 changes: 33 additions & 3 deletions packages/slides-ui/src/services/slide-editor-bridge.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ import {
} from '@univerjs/core';
import type { Engine, IDocumentLayoutObject, IRenderContext, RichText, Scene } from '@univerjs/engine-render';
import { DeviceInputEventType, IRenderManagerService } from '@univerjs/engine-render';
import { SLIDE_KEY } from '@univerjs/slides';
import type { KeyCode } from '@univerjs/ui';
import { IEditorService } from '@univerjs/ui';
import type { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs';

// TODO same as @univerjs/slides/views/render/adaptors/index.js
export enum SLIDE_VIEW_KEY {
MAIN = '__SLIDERender__',
SCENE_VIEWER = '__SLIDEViewer__',
SCENE = '__SLIDEScene__',
VIEWPORT = '__SLIDEViewPort_',
}

export const ISlideEditorBridgeService = createIdentifier<SlideEditorBridgeService>('univer.slide-editor-bridge.service');

export interface IEditorBridgeServiceParam {
Expand Down Expand Up @@ -146,7 +155,7 @@ export class SlideEditorBridgeService extends Disposable implements ISlideEditor
const editCellState = this.getEditRectState();
this._currentEditRectState = editCellState;

// editing render controller @_subscribeToCurrentCell
// slide-editing.render-controller.ts@_subscribeToCurrentCell --> activate(-1000, -1000)
this._currentEditRectState$.next(editCellState);
}

Expand Down Expand Up @@ -174,8 +183,9 @@ export class SlideEditorBridgeService extends Disposable implements ISlideEditor

/**
* get info from _currentEditRectInfo
*
* invoked by slide-editing.render-controller.ts@_handleEditorVisible & this@setEditorRect
*/

getEditRectState(): Readonly<Nullable<IEditorBridgeServiceParam>> {
const editorUnitId = DOCS_NORMAL_EDITOR_UNIT_ID_KEY;

Expand Down Expand Up @@ -211,6 +221,26 @@ export class SlideEditorBridgeService extends Disposable implements ISlideEditor
const editorHeight = editorRectInfo.richTextObj.height;
const left = editorRectInfo.richTextObj.left;
const top = editorRectInfo.richTextObj.top;

const canvasOffset = {
left: 0,
top: 0,
};
// canvasOffset will be used in slide-editing.render-controller.ts@_handleEditorVisible
// const mainScene = this._mainScene;
const renderUnit = this._renderManagerService.getRenderById(unitId);
const mainScene = renderUnit?.scene;
const mainViewport = mainScene?.getViewport(SLIDE_KEY.VIEW);
const slideMainRect = mainScene?.getObject(SLIDE_KEY.COMPONENT);
const slidePos = {
x: slideMainRect?.left || 0,
y: slideMainRect?.top || 0,
};
const scrollX = mainViewport?.viewportScrollX || 0;
const scrollY = mainViewport?.viewportScrollY || 0;
canvasOffset.left = slidePos.x - scrollX;
canvasOffset.top = slidePos.y - scrollY;

return {
position: {
startX: left,
Expand All @@ -220,7 +250,7 @@ export class SlideEditorBridgeService extends Disposable implements ISlideEditor
},
scaleX: 1,
scaleY: 1,
canvasOffset: { left: 0, top: 0 },
canvasOffset,
unitId,
editorUnitId,
documentLayoutObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const EditorContainer: React.FC<ICellIEditorProps> = () => {

return (
<div
className={styles.editorContainer}
className={styles.slideEditorContainer}
style={{
left: state.left,
top: state.top,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.editor-container {
.slide-editor-container {
position: absolute;
z-index: 10;
top: 200px;
Expand All @@ -21,7 +21,7 @@
width: 100%;
height: 100%;

background: white;
background: rgba(174, 174, 174, 0.2);

canvas {
position: absolute;
Expand Down

0 comments on commit b2ad5ce

Please sign in to comment.