Skip to content

Commit

Permalink
feat: add word tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Jun 2, 2023
1 parent f189c83 commit 2a3c2e2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/assets/images/word-tool.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/editor/core/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class Command {
private static setLocale: CommandAdapt['setLocale']
private static getCatalog: CommandAdapt['getCatalog']
private static locationCatalog: CommandAdapt['locationCatalog']
private static wordTool: CommandAdapt['wordTool']

constructor(adapt: CommandAdapt) {
Command.mode = adapt.mode.bind(adapt)
Expand Down Expand Up @@ -170,6 +171,7 @@ export class Command {
Command.setLocale = adapt.setLocale.bind(adapt)
Command.getCatalog = adapt.getCatalog.bind(adapt)
Command.locationCatalog = adapt.locationCatalog.bind(adapt)
Command.wordTool = adapt.wordTool.bind(adapt)
}

// 全局命令
Expand Down Expand Up @@ -495,4 +497,8 @@ export class Command {
return Command.locationCatalog(titleId)
}

public executeWordTool() {
return Command.wordTool()
}

}
30 changes: 29 additions & 1 deletion src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WRAP, ZERO } from '../../dataset/constant/Common'
import { NBSP, WRAP, ZERO } from '../../dataset/constant/Common'
import { EDITOR_ELEMENT_STYLE_ATTR } from '../../dataset/constant/Element'
import { titleSizeMapping } from '../../dataset/constant/Title'
import { defaultWatermarkOption } from '../../dataset/constant/Watermark'
Expand Down Expand Up @@ -1698,4 +1698,32 @@ export class CommandAdapt {
})
}

public wordTool() {
const elementList = this.draw.getMainElementList()
let isApply = false
for (let i = 0; i < elementList.length; i++) {
const element = elementList[i]
// 删除空行、行首空格
if (element.value === ZERO) {
while (i + 1 < elementList.length) {
const nextElement = elementList[i + 1]
if (nextElement.value !== ZERO && nextElement.value !== NBSP) break
elementList.splice(i + 1, 1)
isApply = true
}
}
}
if (!isApply) {
// 避免输入框光标丢失
const isCollapsed = this.range.getIsCollapsed()
this.draw.getCursor().drawCursor({
isShow: isCollapsed
})
} else {
this.draw.render({
isSetCursor: false
})
}
}

}
5 changes: 5 additions & 0 deletions src/editor/core/range/RangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export class RangeManager {
this.setRange(-1, -1)
}

public getIsCollapsed(): boolean {
const { startIndex, endIndex } = this.range
return startIndex === endIndex
}

public getSelection(): IElement[] | null {
const { startIndex, endIndex } = this.range
if (startIndex === endIndex) return null
Expand Down
10 changes: 10 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,16 @@ window.onload = function () {
}
})
}
},
{
name: '格式整理',
icon: 'word-tool',
when: (payload) => {
return !payload.isReadonly
},
callback: (command: Command) => {
command.executeWordTool()
}
}
])

Expand Down
4 changes: 4 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -905,4 +905,8 @@ ul {

.ce-contextmenu-signature {
background-image: url('./assets/images/signature.svg');
}

.ce-contextmenu-word-tool {
background-image: url('./assets/images/word-tool.svg');
}

0 comments on commit 2a3c2e2

Please sign in to comment.