Skip to content

Commit

Permalink
format and ts-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
beixiyo committed Nov 30, 2024
1 parent 4f765e8 commit 08d7a81
Show file tree
Hide file tree
Showing 40 changed files with 2,005 additions and 1,933 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"author": "CJL",
"license": "MIT",
"devDependencies": {
"@jl-org/ts-tool": "^0.0.8",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
Expand Down
24 changes: 21 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/NoteBoard/NoteBoard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clearAllCvs, getImg } from '@/canvasTool/tools'
import { cutImg, getCvsImg } from '@/canvasTool/handleImg'
import { mergeOpts, setCanvas } from './tools'
import type { CanvasAttrs, Mode, DrawImgOpts, ImgInfo, RecordPath, CanvasItem, ShotParams, NoteBoardOptions } from './type'
import type { CanvasAttrs, Mode, DrawImgOpts, ImgInfo, RecordPath, CanvasItem, ShotParams, NoteBoardOptions, NoteBoardOptionsRequired } from './type'
import { createUnReDoList, excludeKeys, getCircleCursor } from '@/utils'


Expand Down Expand Up @@ -33,7 +33,7 @@ export class NoteBoard {
/** 图片画板 上下文 */
imgCtx = this.imgCanvas.getContext('2d') as CanvasRenderingContext2D
/** 绘制的图片尺寸信息 */
imgInfo: ImgInfo
imgInfo?: ImgInfo

/** 存储的所有 Canvas 信息 */
canvasList: CanvasItem[] = [
Expand All @@ -49,7 +49,7 @@ export class NoteBoard {
}
]

opts: NoteBoardOptions
opts: NoteBoardOptionsRequired

mode: Mode = 'draw'
/** 开启鼠标滚轮缩放 */
Expand Down Expand Up @@ -84,7 +84,7 @@ export class NoteBoard {
unReDoList = createUnReDoList<RecordPath[]>()
private recordPath: RecordPath[] = []

constructor(opts?: NoteBoardOptions) {
constructor(opts: NoteBoardOptions) {
this.opts = mergeOpts(opts)

const {
Expand Down Expand Up @@ -398,10 +398,12 @@ export class NoteBoard {
}

if (ctx) {
// @ts-ignore
ctx[k] = attr
}
else {
for (const item of this.canvasList) {
// @ts-ignore
item.ctx[k] = attr
}
}
Expand Down
60 changes: 35 additions & 25 deletions src/NoteBoard/NoteBoardWithShape.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clearAllCvs, getImg } from '@/canvasTool/tools'
import { cutImg, getCvsImg } from '@/canvasTool/handleImg'
import { mergeOpts, setCanvas } from './tools'
import type { CanvasAttrs, Mode, DrawImgOpts, ImgInfo, RecordPath, CanvasItem, ShotParams, NoteBoardOptions, DrawMapVal } from './type'
import type { CanvasAttrs, Mode, DrawImgOpts, ImgInfo, RecordPath, CanvasItem, ShotParams, NoteBoardOptions, DrawMapVal, NoteBoardOptionsRequired } from './type'
import { excludeKeys, getCircleCursor, UnRedoLinkedList } from '@/utils'
import { DrawShape } from '@/Shapes'

Expand Down Expand Up @@ -32,7 +32,7 @@ export class NoteBoardWithShape extends DrawShape {
/** 图片画板 上下文 */
imgCtx = this.imgCanvas.getContext('2d') as CanvasRenderingContext2D
/** 绘制的图片尺寸信息 */
imgInfo: ImgInfo
imgInfo?: ImgInfo

/** 存储的所有 Canvas 信息 */
canvasList: CanvasItem[] = [
Expand All @@ -48,7 +48,7 @@ export class NoteBoardWithShape extends DrawShape {
}
]

opts: NoteBoardOptions
opts: NoteBoardOptionsRequired

mode: Mode = 'draw'

Expand All @@ -72,10 +72,10 @@ export class NoteBoardWithShape extends DrawShape {
/**
* 历史记录
*/
unReDoList = new UnRedoLinkedList<RecordPath[]>()
recordPath: RecordPath[]
history = new UnRedoLinkedList<RecordPath[]>()
recordPath?: RecordPath[]

constructor(opts?: NoteBoardOptions) {
constructor(opts: NoteBoardOptions) {
super()
super.initial({
canvas: this.canvas,
Expand Down Expand Up @@ -112,7 +112,8 @@ export class NoteBoardWithShape extends DrawShape {
* 确保有数组后再执行
*/
setTimeout(() => {
const lastRecord = this.unReDoList.tailValue
const lastRecord = this.history.tailValue
if (!lastRecord) return
lastRecord[lastRecord.length - 1]?.shapes.push(...this.shapes)
})
},
Expand Down Expand Up @@ -276,38 +277,40 @@ export class NoteBoardWithShape extends DrawShape {
* 撤销
*/
undo() {
const recordPath = this.unReDoList.undo()
const recordPath = this.history.undo()
if (!recordPath?.value) {
this.clear(false)
return
}

const drawFn = this.drawFn.unRedo
const { shapes, shape } = drawFn({ type: 'undo' })
this.recordPath = recordPath.value
const drawFn = this.drawFn?.unRedo
if (!drawFn) return
const data = drawFn({ type: 'undo' })

this.opts.onUndo?.({
mode: this.mode,
shape,
shapes,
...data,
})
}

/**
* 重做
*/
redo() {
const recordPath = this.unReDoList.redo()
const recordPath = this.history.redo()
if (!recordPath?.value) {
return
}

const drawFn = this.drawFn.unRedo
const { shapes, shape } = drawFn({ type: 'redo' })
this.recordPath = recordPath.value
const drawFn = this.drawFn?.unRedo
if (!drawFn) return

const data = drawFn({ type: 'redo' })
this.opts.onRedo?.({
mode: this.mode,
shape,
shapes,
...data,
})
}

Expand Down Expand Up @@ -447,10 +450,12 @@ export class NoteBoardWithShape extends DrawShape {
}

if (ctx) {
// @ts-ignore
ctx[k] = attr
}
else {
for (const item of this.canvasList) {
// @ts-ignore
item.ctx[k] = attr
}
}
Expand All @@ -469,6 +474,7 @@ export class NoteBoardWithShape extends DrawShape {
***************************************************/

private drawRecord() {
if (!this.recordPath) return
const { ctx } = this
const currentMode = this.mode

Expand All @@ -484,7 +490,9 @@ export class NoteBoardWithShape extends DrawShape {
}
}

const lastRecord = this.unReDoList.tailValue
const lastRecord = this.history.tailValue
if (!lastRecord) return

const shapes = lastRecord[lastRecord.length - 1]?.shapes
shapes?.length && this.drawShapes(false, shapes)
this.setMode(currentMode)
Expand Down Expand Up @@ -541,9 +549,9 @@ export class NoteBoardWithShape extends DrawShape {
* 添加记录
*/
if (this.canAddRecord) {
const lastRecord = this.unReDoList.tailValue
const lastRecord = this.history.tailValue

this.unReDoList.add([
this.history.add([
...(lastRecord || []),
{
canvasAttrs: excludeKeys(
Expand Down Expand Up @@ -608,22 +616,24 @@ export class NoteBoardWithShape extends DrawShape {

const { offsetX, offsetY } = e
const { ctx, drawStart } = this
const lastRecord = this.unReDoList.tailValue
const lastRecord = this.history.tailValue

ctx.moveTo(drawStart.x, drawStart.y)
ctx.lineTo(offsetX, offsetY)

ctx.lineWidth = this.opts.lineWidth
ctx.stroke()

lastRecord[lastRecord.length - 1].path.push({
moveTo: [drawStart.x, drawStart.y],
lineTo: [offsetX, offsetY]
})
this.drawStart = {
x: offsetX,
y: offsetY,
}

if (!lastRecord) return
lastRecord[lastRecord.length - 1].path.push({
moveTo: [drawStart.x, drawStart.y],
lineTo: [offsetX, offsetY]
})
}

private onMouseup = (e: MouseEvent) => {
Expand Down
31 changes: 17 additions & 14 deletions src/NoteBoard/tools.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { getDPR } from '@/canvasTool'
import type { NoteBoardOptions } from './type'
import type { NoteBoardOptions, NoteBoardOptionsRequired } from './type'


export function mergeOpts(
opts: NoteBoardOptions,
) {
): NoteBoardOptionsRequired {

const defaultOpts: NoteBoardOptionsRequired = {
width: 800 * getDPR(),
height: 600 * getDPR(),
minScale: 0.5,
maxScale: 8,

lineWidth: 1,
strokeStyle: '#000',
lineCap: 'round' as CanvasLineCap,
globalCompositeOperation: 'source-over'
}

return {
... {
width: 800 * getDPR(),
height: 600 * getDPR(),
minScale: 0.5,
maxScale: 8,

lineWidth: 1,
strokeStyle: '#000',
lineCap: 'round' as CanvasLineCap,
globalCompositeOperation: 'source-over'
} as NoteBoardOptions,
...defaultOpts,
...opts,
}
}
Expand All @@ -26,7 +29,7 @@ export function setCanvas(canvas: HTMLCanvasElement, width: number, height: numb
canvas.height = height
canvas.style.position = 'absolute'

const parent = canvas.parentElement,
const parent = canvas.parentElement!,
{ offsetHeight, offsetWidth } = parent

// 居中
Expand Down
Loading

0 comments on commit 08d7a81

Please sign in to comment.