Skip to content

Commit

Permalink
fix: 1.解决窗口拖动问题 2.修正鼠标特效位置问题
Browse files Browse the repository at this point in the history
  • Loading branch information
JuckZ committed May 8, 2023
1 parent 03b6620 commit 69ec87f
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/render/CursorEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import {
// An elasticish trail of cursors that will nip to wherever your mouse is
trailingCursor,
} from 'cursor-effects';
import { debounce } from 'lodash-es';
import { randomColor } from '@/utils/common';
import { EditorUtils } from '@/utils/editor';
import type { SettingModel } from 'model/settings';

const cursorEffects: any[] = [];

let oldCursorEffects: any[] = [];
let newCanvas: HTMLCanvasElement;
function enableCursorEffect(effectName) {
const canvasesBefore = Array.from(document.querySelectorAll('canvas'));
let emo;
Expand Down Expand Up @@ -70,19 +72,41 @@ function enableCursorEffect(effectName) {
}
cursorEffects.push(emo);
const canvasesAfter = Array.from(document.querySelectorAll('canvas'));
const newCanvas = canvasesAfter.find(canvas => !canvasesBefore.includes(canvas)) as unknown as HTMLCanvasElement;
newCanvas = canvasesAfter.find(canvas => !canvasesBefore.includes(canvas)) as unknown as HTMLCanvasElement;
// 调整特效层级,解决在侧边栏和设置弹窗时不可见的问题
newCanvas.style.zIndex = '9999';
// newCanvas.style.height = Number.parseFloat(newCanvas?.style.height) + EditorUtils.getTitleBarHeight() + 'px';
// BUG 特效位置不正确
newCanvas.style.top = EditorUtils.getTitleBarHeight() + 'px';
}

export function toggleCursorEffects(target: string) {
const appendHandle = debounce(() => {
if (!document.body.contains(newCanvas)) {
document.body.appendChild(newCanvas);
}
}, 500);

const appendCanvas = (event: MouseEvent) => {
appendHandle();
};
const removeCanvas = (event: MouseEvent) => {
if (document.body.contains(newCanvas)) {
document.body.removeChild(newCanvas);
}
};
oldCursorEffects = [...cursorEffects];
const titlebarEleList = document.querySelectorAll('.titlebar, .workspace-tab-header-container');
if (target != 'none') {
disableCursorEffect();
// mouseenter无法完成该效果
titlebarEleList.forEach(ele => {
ele.addEventListener('mouseenter', removeCanvas);
ele.addEventListener('mouseleave', appendCanvas);
});
enableCursorEffect(target);
} else {
titlebarEleList.forEach(ele => {
ele.removeEventListener('mouseenter', removeCanvas);
ele.removeEventListener('mouseleave', appendCanvas);
});
disableCursorEffect();
}
}
Expand Down

0 comments on commit 69ec87f

Please sign in to comment.