feat:set the cursor style when dragging text
This commit is contained in:
@@ -1,16 +1,23 @@
|
|||||||
import { CURSOR_AGENT_HEIGHT } from '../../dataset/constant/Cursor'
|
import { CURSOR_AGENT_HEIGHT } from '../../dataset/constant/Cursor'
|
||||||
import { EDITOR_PREFIX } from '../../dataset/constant/Editor'
|
import { EDITOR_PREFIX } from '../../dataset/constant/Editor'
|
||||||
|
import { DeepRequired } from '../../interface/Common'
|
||||||
|
import { ICursorOption } from '../../interface/Cursor'
|
||||||
import { IEditorOption } from '../../interface/Editor'
|
import { IEditorOption } from '../../interface/Editor'
|
||||||
import { Draw } from '../draw/Draw'
|
import { Draw } from '../draw/Draw'
|
||||||
import { CanvasEvent } from '../event/CanvasEvent'
|
import { CanvasEvent } from '../event/CanvasEvent'
|
||||||
import { Position } from '../position/Position'
|
import { Position } from '../position/Position'
|
||||||
import { CursorAgent } from './CursorAgent'
|
import { CursorAgent } from './CursorAgent'
|
||||||
|
|
||||||
|
export type IDrawCursorOption = ICursorOption &
|
||||||
|
{
|
||||||
|
isBlink?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export class Cursor {
|
export class Cursor {
|
||||||
|
|
||||||
private draw: Draw
|
private draw: Draw
|
||||||
private container: HTMLDivElement
|
private container: HTMLDivElement
|
||||||
private options: Required<IEditorOption>
|
private options: DeepRequired<IEditorOption>
|
||||||
private position: Position
|
private position: Position
|
||||||
private cursorDom: HTMLDivElement
|
private cursorDom: HTMLDivElement
|
||||||
private cursorAgent: CursorAgent
|
private cursorAgent: CursorAgent
|
||||||
@@ -43,12 +50,16 @@ export class Cursor {
|
|||||||
return this.getAgentDom().value = ''
|
return this.getAgentDom().value = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
public drawCursor() {
|
public drawCursor(payload?: IDrawCursorOption) {
|
||||||
const isReadonly = this.draw.isReadonly()
|
|
||||||
const cursorPosition = this.position.getCursorPosition()
|
const cursorPosition = this.position.getCursorPosition()
|
||||||
if (!cursorPosition) return
|
if (!cursorPosition) return
|
||||||
|
const { scale, cursor } = this.options
|
||||||
|
const {
|
||||||
|
color,
|
||||||
|
width,
|
||||||
|
isBlink = true
|
||||||
|
} = { ...cursor, ...payload }
|
||||||
// 设置光标代理
|
// 设置光标代理
|
||||||
const { scale } = this.options
|
|
||||||
const height = this.draw.getHeight()
|
const height = this.draw.getHeight()
|
||||||
const pageGap = this.draw.getPageGap()
|
const pageGap = this.draw.getPageGap()
|
||||||
const { metrics, coordinate: { leftTop, rightTop }, ascent, pageNo } = cursorPosition
|
const { metrics, coordinate: { leftTop, rightTop }, ascent, pageNo } = cursorPosition
|
||||||
@@ -68,13 +79,21 @@ export class Cursor {
|
|||||||
agentCursorDom.style.left = `${cursorLeft}px`
|
agentCursorDom.style.left = `${cursorLeft}px`
|
||||||
agentCursorDom.style.top = `${cursorTop + cursorHeight - CURSOR_AGENT_HEIGHT * scale}px`
|
agentCursorDom.style.top = `${cursorTop + cursorHeight - CURSOR_AGENT_HEIGHT * scale}px`
|
||||||
// 模拟光标显示
|
// 模拟光标显示
|
||||||
|
const isReadonly = this.draw.isReadonly()
|
||||||
|
this.cursorDom.style.width = `${width}px`
|
||||||
|
this.cursorDom.style.backgroundColor = color
|
||||||
this.cursorDom.style.left = `${cursorLeft}px`
|
this.cursorDom.style.left = `${cursorLeft}px`
|
||||||
this.cursorDom.style.top = `${cursorTop}px`
|
this.cursorDom.style.top = `${cursorTop}px`
|
||||||
this.cursorDom.style.display = isReadonly ? 'none' : 'block'
|
this.cursorDom.style.display = isReadonly ? 'none' : 'block'
|
||||||
this.cursorDom.style.height = `${cursorHeight}px`
|
this.cursorDom.style.height = `${cursorHeight}px`
|
||||||
setTimeout(() => {
|
const animationClassName = `${EDITOR_PREFIX}-cursor--animation`
|
||||||
this.cursorDom.classList.add(`${EDITOR_PREFIX}-cursor--animation`)
|
if (isBlink) {
|
||||||
}, 200)
|
setTimeout(() => {
|
||||||
|
this.cursorDom.classList.add(animationClassName)
|
||||||
|
}, 200)
|
||||||
|
} else {
|
||||||
|
this.cursorDom.classList.remove(animationClassName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public recoveryCursor() {
|
public recoveryCursor() {
|
||||||
|
|||||||
@@ -34,7 +34,12 @@ function dragover(evt: DragEvent | MouseEvent, host: CanvasEvent) {
|
|||||||
position.setCursorPosition(positionList[curIndex])
|
position.setCursorPosition(positionList[curIndex])
|
||||||
}
|
}
|
||||||
const cursor = draw.getCursor()
|
const cursor = draw.getCursor()
|
||||||
cursor.drawCursor()
|
const { cursor: { dragColor, dragWidth } } = draw.getOptions()
|
||||||
|
cursor.drawCursor({
|
||||||
|
width: dragWidth,
|
||||||
|
color: dragColor,
|
||||||
|
isBlink: false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
@@ -1 +1,10 @@
|
|||||||
|
import { ICursorOption } from '../../interface/Cursor'
|
||||||
|
|
||||||
export const CURSOR_AGENT_HEIGHT = 12
|
export const CURSOR_AGENT_HEIGHT = 12
|
||||||
|
|
||||||
|
export const defaultCursorOption: Readonly<Required<ICursorOption>> = {
|
||||||
|
width: 1,
|
||||||
|
color: '#000000',
|
||||||
|
dragWidth: 2,
|
||||||
|
dragColor: '#0000FF'
|
||||||
|
}
|
||||||
+8
-1
@@ -29,6 +29,8 @@ import { KeyMap } from './dataset/enum/KeyMap'
|
|||||||
import { BlockType } from './dataset/enum/Block'
|
import { BlockType } from './dataset/enum/Block'
|
||||||
import { IBlock } from './interface/Block'
|
import { IBlock } from './interface/Block'
|
||||||
import { ILang } from './interface/i18n/I18n'
|
import { ILang } from './interface/i18n/I18n'
|
||||||
|
import { ICursorOption } from './interface/Cursor'
|
||||||
|
import { defaultCursorOption } from './dataset/constant/Cursor'
|
||||||
|
|
||||||
export default class Editor {
|
export default class Editor {
|
||||||
|
|
||||||
@@ -54,6 +56,10 @@ export default class Editor {
|
|||||||
...defaultCheckboxOption,
|
...defaultCheckboxOption,
|
||||||
...options.checkbox
|
...options.checkbox
|
||||||
}
|
}
|
||||||
|
const cursorOptions: Required<ICursorOption> = {
|
||||||
|
...defaultCursorOption,
|
||||||
|
...options.cursor
|
||||||
|
}
|
||||||
|
|
||||||
const editorOptions: DeepRequired<IEditorOption> = {
|
const editorOptions: DeepRequired<IEditorOption> = {
|
||||||
mode: EditorMode.EDIT,
|
mode: EditorMode.EDIT,
|
||||||
@@ -93,7 +99,8 @@ export default class Editor {
|
|||||||
header: headerOptions,
|
header: headerOptions,
|
||||||
watermark: waterMarkOptions,
|
watermark: waterMarkOptions,
|
||||||
control: controlOptions,
|
control: controlOptions,
|
||||||
checkbox: checkboxOptions
|
checkbox: checkboxOptions,
|
||||||
|
cursor: cursorOptions
|
||||||
}
|
}
|
||||||
formatElementList(elementList, {
|
formatElementList(elementList, {
|
||||||
editorOptions
|
editorOptions
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
export interface ICursorOption {
|
||||||
|
width?: number;
|
||||||
|
color?: string;
|
||||||
|
dragWidth?: number;
|
||||||
|
dragColor?: string;
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import { IElement } from '..'
|
|||||||
import { EditorMode, PageMode } from '../dataset/enum/Editor'
|
import { EditorMode, PageMode } from '../dataset/enum/Editor'
|
||||||
import { ICheckboxOption } from './Checkbox'
|
import { ICheckboxOption } from './Checkbox'
|
||||||
import { IControlOption } from './Control'
|
import { IControlOption } from './Control'
|
||||||
|
import { ICursorOption } from './Cursor'
|
||||||
import { IHeader } from './Header'
|
import { IHeader } from './Header'
|
||||||
import { IMargin } from './Margin'
|
import { IMargin } from './Margin'
|
||||||
import { IWatermark } from './Watermark'
|
import { IWatermark } from './Watermark'
|
||||||
@@ -44,6 +45,7 @@ export interface IEditorOption {
|
|||||||
watermark?: IWatermark;
|
watermark?: IWatermark;
|
||||||
control?: IControlOption;
|
control?: IControlOption;
|
||||||
checkbox?: ICheckboxOption;
|
checkbox?: ICheckboxOption;
|
||||||
|
cursor?: ICursorOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IEditorResult {
|
export interface IEditorResult {
|
||||||
|
|||||||
Reference in New Issue
Block a user