Skip to content

Commit 2c1f378

Browse files
committed
feat: draw white space marker #1329
1 parent 20dc863 commit 2c1f378

File tree

10 files changed

+73
-3
lines changed

10 files changed

+73
-3
lines changed

docs/en/guide/option.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ interface IEditorOption {
7474
zone?: IZoneOption // Zone option。{tipDisabled?:boolean;}
7575
background?: IBackgroundOption // Background option. {color?:string; image?:string; size?:BackgroundSize; repeat?:BackgroundRepeat; applyPageNumbers?:number[]}。default: {color: '#FFFFFF'}
7676
lineBreak?: ILineBreakOption // LineBreak option. {disabled?:boolean; color?:string; lineWidth?:number;}
77+
whiteSpace?: IWhiteSpaceOption // WhiteSpace option. {disabled?:boolean; color?:string; radius?:number;}
7778
separator?: ISeparatorOption // Separator option. {lineWidth?:number; strokeStyle?:string;}
7879
lineNumber?: ILineNumberOption // LineNumber option. {size?:number; font?:string; color?:string; disabled?:boolean; right?:number}
7980
pageBorder?: IPageBorderOption // PageBorder option. {color?:string; lineWidth:number; padding?:IPadding; disabled?:boolean;}

docs/guide/option.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ interface IEditorOption {
7474
zone?: IZoneOption // 编辑器区域配置。{tipDisabled?:boolean;}
7575
background?: IBackgroundOption // 背景配置。{color?:string; image?:string; size?:BackgroundSize; repeat?:BackgroundRepeat; applyPageNumbers?:number[]}。默认:{color: '#FFFFFF'}
7676
lineBreak?: ILineBreakOption // 换行符配置。{disabled?:boolean; color?:string; lineWidth?:number;}
77+
whiteSpace?: IWhiteSpaceOption // 空格符配置。{disabled?:boolean; color?:string; radius?:number;}
7778
separator?: ISeparatorOption // 分隔符配置。{lineWidth?:number; strokeStyle?:string;}
7879
lineNumber?: ILineNumberOption // 行号配置。{size?:number; font?:string; color?:string; disabled?:boolean; right?:number}
7980
pageBorder?: IPageBorderOption // 页面边框配置。{color?:string; lineWidth:number; padding?:IPadding; disabled?:boolean;}

src/editor/core/draw/Draw.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ import { EventBusMap } from '../../interface/EventBus'
103103
import { Group } from './interactive/Group'
104104
import { Override } from '../override/Override'
105105
import { FlexDirection, ImageDisplay } from '../../dataset/enum/Common'
106-
import { PUNCTUATION_REG } from '../../dataset/constant/Regular'
106+
import {
107+
PUNCTUATION_REG,
108+
WHITE_SPACE_REG
109+
} from '../../dataset/constant/Regular'
107110
import { LineBreakParticle } from './particle/LineBreakParticle'
111+
import { WhiteSpaceParticle } from './particle/WhiteSpaceParticle'
108112
import { MouseObserver } from '../observer/MouseObserver'
109113
import { LineNumber } from './frame/LineNumber'
110114
import { PageBorder } from './frame/PageBorder'
@@ -171,6 +175,7 @@ export class Draw {
171175
private blockParticle: BlockParticle
172176
private listParticle: ListParticle
173177
private lineBreakParticle: LineBreakParticle
178+
private whiteSpaceParticle: WhiteSpaceParticle
174179
private control: Control
175180
private pageBorder: PageBorder
176181
private workerManager: WorkerManager
@@ -253,6 +258,7 @@ export class Draw {
253258
this.blockParticle = new BlockParticle(this)
254259
this.listParticle = new ListParticle(this)
255260
this.lineBreakParticle = new LineBreakParticle(this)
261+
this.whiteSpaceParticle = new WhiteSpaceParticle(this)
256262
this.control = new Control(this)
257263
this.pageBorder = new PageBorder(this)
258264
this.graffiti = new Graffiti(this, data.graffiti)
@@ -2128,7 +2134,8 @@ export class Draw {
21282134
scale,
21292135
table: { tdPadding },
21302136
group,
2131-
lineBreak
2137+
lineBreak,
2138+
whiteSpace
21322139
} = this.options
21332140
const {
21342141
rowList,
@@ -2137,7 +2144,8 @@ export class Draw {
21372144
positionList,
21382145
startIndex,
21392146
zone,
2140-
isDrawLineBreak = !lineBreak.disabled
2147+
isDrawLineBreak = !lineBreak.disabled,
2148+
isDrawWhiteSpace = !whiteSpace.disabled
21412149
} = payload
21422150
const isPrintMode = this.isPrintMode()
21432151
const isGraffitiMode = this.isGraffitiMode()
@@ -2279,6 +2287,10 @@ export class Draw {
22792287
) {
22802288
this.lineBreakParticle.render(ctx, element, x, y + curRow.height / 2)
22812289
}
2290+
// 空白符绘制
2291+
if (isDrawWhiteSpace && WHITE_SPACE_REG.test(element.value)) {
2292+
this.whiteSpaceParticle.render(ctx, element, x, y + curRow.height / 2)
2293+
}
22822294
// 边框绘制(目前仅支持控件)
22832295
if (element.control?.border) {
22842296
// 不同控件边框立刻绘制
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { DeepRequired } from '../../../interface/Common'
2+
import { IEditorOption } from '../../../interface/Editor'
3+
import { IRowElement } from '../../../interface/Row'
4+
import { Draw } from '../Draw'
5+
6+
export class WhiteSpaceParticle {
7+
private options: DeepRequired<IEditorOption>
8+
9+
constructor(draw: Draw) {
10+
this.options = draw.getOptions()
11+
}
12+
13+
public render(
14+
ctx: CanvasRenderingContext2D,
15+
element: IRowElement,
16+
x: number,
17+
y: number
18+
) {
19+
const {
20+
scale,
21+
whiteSpace: { color, radius }
22+
} = this.options
23+
const metrics = element.metrics
24+
ctx.save()
25+
ctx.fillStyle = color
26+
ctx.beginPath()
27+
ctx.arc(x + metrics.width / 2, y, radius * scale, 0, Math.PI * 2)
28+
ctx.fill()
29+
ctx.closePath()
30+
ctx.restore()
31+
}
32+
}

src/editor/dataset/constant/Regular.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ export const PUNCTUATION_REG =
1919
export const START_LINE_BREAK_REG = new RegExp(`^[${ZERO}\n]`)
2020

2121
export const NON_NUMBER_STR_REG = /[^0-9\+\-\.eE,]/
22+
23+
export const WHITE_SPACE_REG = /\s/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { IWhiteSpaceOption } from '../../interface/WhiteSpace'
2+
3+
export const defaultWhiteSpaceOption: Readonly<Required<IWhiteSpaceOption>> = {
4+
disabled: true,
5+
color: '#CCCCCC',
6+
radius: 1
7+
}

src/editor/interface/Draw.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface IDrawRowPayload {
3737
innerWidth: number
3838
zone?: EditorZone
3939
isDrawLineBreak?: boolean
40+
isDrawWhiteSpace?: boolean
4041
}
4142

4243
export interface IDrawFloatPayload {

src/editor/interface/Editor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { IElement } from './Element'
3030
import { LocationPosition } from '../dataset/enum/Common'
3131
import { IRange } from './Range'
3232
import { IGraffitiData, IGraffitiOption } from './Graffiti'
33+
import { IWhiteSpaceOption } from './WhiteSpace'
3334

3435
export interface IEditorData {
3536
header?: IElement[]
@@ -99,6 +100,7 @@ export interface IEditorOption {
99100
zone?: IZoneOption
100101
background?: IBackgroundOption
101102
lineBreak?: ILineBreakOption
103+
whiteSpace?: IWhiteSpaceOption
102104
separator?: ISeparatorOption
103105
lineNumber?: ILineNumberOption
104106
pageBorder?: IPageBorderOption

src/editor/interface/WhiteSpace.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface IWhiteSpaceOption {
2+
disabled?: boolean
3+
color?: string
4+
radius?: number
5+
}

src/editor/utils/option.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import { IBadgeOption } from '../interface/Badge'
5151
import { defaultModeRuleOption } from '../dataset/constant/Editor'
5252
import { IGraffitiOption } from '../interface/Graffiti'
5353
import { defaultGraffitiOption } from '../dataset/constant/Graffiti'
54+
import { IWhiteSpaceOption } from '../interface/WhiteSpace'
55+
import { defaultWhiteSpaceOption } from '../dataset/constant/WhiteSpace'
5456

5557
export function mergeOption(
5658
options: IEditorOption = {}
@@ -119,6 +121,10 @@ export function mergeOption(
119121
...defaultLineBreak,
120122
...options.lineBreak
121123
}
124+
const whiteSpaceOptions: Required<IWhiteSpaceOption> = {
125+
...defaultWhiteSpaceOption,
126+
...options.whiteSpace
127+
}
122128
const separatorOptions: Required<ISeparatorOption> = {
123129
...defaultSeparatorOption,
124130
...options.separator
@@ -216,6 +222,7 @@ export function mergeOption(
216222
zone: zoneOptions,
217223
background: backgroundOptions,
218224
lineBreak: lineBreakOptions,
225+
whiteSpace: whiteSpaceOptions,
219226
separator: separatorOptions,
220227
lineNumber: lineNumberOptions,
221228
pageBorder: pageBorderOptions,

0 commit comments

Comments
 (0)