Skip to content

Commit e483c17

Browse files
committed
feat(case): add case plugin
1 parent 3112d5e commit e483c17

File tree

11 files changed

+298
-0
lines changed

11 files changed

+298
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,17 @@ command.executeLoadDiagram({
107107
onDestroy?: (message?: any) => void
108108
})
109109
```
110+
111+
- @hufe921/canvas-editor-plugin-case
112+
113+
```javascript
114+
import Editor from '@hufe921/canvas-editor'
115+
import casePlugin from '@hufe921/canvas-editor-plugin-case'
116+
117+
const instance = new Editor()
118+
instance.use(casePlugin)
119+
120+
command.executeUpperCase()
121+
122+
command.executeLowerCase()
123+
```

packages/case/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021-present, hufe
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

packages/case/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<h1 align="center">canvas-editor-plugin-case</h1>
2+
3+
<p align="center">uppercase and lowercase plugin for canvas-editor</p>
4+
5+
## usage
6+
7+
```bash
8+
npm i @hufe921/canvas-editor-plugin-case --save
9+
```
10+
11+
```javascript
12+
import Editor from '@hufe921/canvas-editor'
13+
import casePlugin from '@hufe921/canvas-editor-plugin-case'
14+
15+
const instance = new Editor()
16+
instance.use(casePlugin)
17+
18+
command.executeUpperCase()
19+
20+
command.executeLowerCase()
21+
```

packages/case/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
<title>canvas-editor-plugin-case</title>
9+
</head>
10+
11+
<body>
12+
<div id="app">
13+
<div id="editor"></div>
14+
</div>
15+
<script type="module" src="/src/main.ts"></script>
16+
</body>
17+
18+
</html>

packages/case/package.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "@hufe921/canvas-editor-plugin-case",
3+
"author": "Hufe",
4+
"license": "MIT",
5+
"version": "0.0.1",
6+
"description": "uppercase and lowercase plugin for canvas-editor",
7+
"type": "module",
8+
"publishConfig": {
9+
"registry": "https://registry.npmjs.org/",
10+
"access": "public"
11+
},
12+
"files": [
13+
"dist",
14+
"README.md",
15+
"LICENSE",
16+
"package.json"
17+
],
18+
"typings": "./dist/src/case/index.d.ts",
19+
"main": "./dist/case.umd.cjs",
20+
"module": "./dist/case.js",
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/Hufe921/canvas-editor-plugin.git"
24+
},
25+
"keywords": [
26+
"canvas-editor",
27+
"case"
28+
],
29+
"scripts": {
30+
"dev": "vite",
31+
"build": "vite build --mode lib",
32+
"release": "node scripts/release.js",
33+
"type:check": "tsc --noEmit"
34+
},
35+
"peerDependencies": {
36+
"@hufe921/canvas-editor": ">=0.9.88"
37+
},
38+
"devDependencies": {
39+
"@hufe921/canvas-editor": "0.9.88",
40+
"@rollup/plugin-typescript": "^10.0.1",
41+
"@types/color": "^3.0.4",
42+
"@typescript-eslint/eslint-plugin": "4.33.0",
43+
"@typescript-eslint/parser": "4.33.0",
44+
"eslint": "7.32.0",
45+
"simple-git-hooks": "^2.8.1",
46+
"typescript": "^5.0.2",
47+
"vite": "^4.3.9",
48+
"vite-plugin-css-injected-by-js": "^3.3.0"
49+
}
50+
}

packages/case/src/case/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Command, Editor, ElementType } from '@hufe921/canvas-editor'
2+
3+
declare module '@hufe921/canvas-editor' {
4+
interface Command {
5+
executeUpperCase(): void
6+
executeLowerCase(): void
7+
}
8+
}
9+
10+
const TEXTLIKE_ELEMENT_TYPE: ElementType[] = [
11+
ElementType.TEXT,
12+
ElementType.SUBSCRIPT,
13+
ElementType.SUPERSCRIPT
14+
]
15+
16+
enum CaseType {
17+
UPPER = 'upper',
18+
LOWER = 'lower'
19+
}
20+
21+
function toCase(command: Command, type: CaseType) {
22+
const rangeContext = command.getRangeContext()
23+
if (!rangeContext?.selectionElementList?.length) return
24+
const selectionElementList = rangeContext.selectionElementList
25+
const isExistNonTextElement = selectionElementList.some(
26+
element => element.type && !TEXTLIKE_ELEMENT_TYPE.includes(element.type)
27+
)
28+
if (isExistNonTextElement) return
29+
// 缓存旧光标数据
30+
const oldRange = command.getRange()
31+
// 修改选中元素大小写
32+
selectionElementList.forEach(element => {
33+
element.value =
34+
type === CaseType.UPPER
35+
? element.value.toUpperCase()
36+
: element.value.toLowerCase()
37+
})
38+
// 插入后进行光标选择
39+
command.executeInsertElementList(selectionElementList)
40+
command.executeReplaceRange(oldRange)
41+
}
42+
43+
export default function casePlugin(editor: Editor) {
44+
const command = editor.command
45+
46+
command.executeUpperCase = () => {
47+
toCase(command, CaseType.UPPER)
48+
}
49+
50+
command.executeLowerCase = () => {
51+
toCase(command, CaseType.LOWER)
52+
}
53+
}

packages/case/src/main.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Editor, { Command } from '@hufe921/canvas-editor'
2+
import casePlugin from './case'
3+
4+
window.onload = function () {
5+
const container = document.querySelector<HTMLDivElement>('#editor')!
6+
const instance = new Editor(container, {
7+
main: []
8+
})
9+
instance.use(casePlugin)
10+
instance.register.contextMenuList([
11+
{
12+
name: '转成大写',
13+
when: payload => {
14+
return !payload.isReadonly && payload.editorHasSelection
15+
},
16+
callback: (command: Command) => {
17+
command.executeUpperCase()
18+
}
19+
},
20+
{
21+
name: '转成小写',
22+
when: payload => {
23+
return !payload.isReadonly && payload.editorHasSelection
24+
},
25+
callback: (command: Command) => {
26+
command.executeLowerCase()
27+
}
28+
}
29+
])
30+
}

packages/case/src/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

packages/case/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": ["../../tsconfig.json"],
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"rootDir": ""
6+
},
7+
"include": [
8+
"./src/**/*.ts"
9+
]
10+
}

packages/case/vite.config.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { defineConfig } from 'vite'
2+
import typescript from '@rollup/plugin-typescript'
3+
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
4+
import * as path from 'path'
5+
6+
export default defineConfig(({ mode }) => {
7+
const name = 'case'
8+
if (mode === 'lib') {
9+
return {
10+
plugins: [
11+
cssInjectedByJsPlugin({
12+
styleId: `${name}-style`,
13+
topExecutionPriority: true
14+
}),
15+
{
16+
...typescript({
17+
tsconfig: './tsconfig.json',
18+
include: ['./src/case/**']
19+
}),
20+
apply: 'build',
21+
declaration: true,
22+
declarationDir: 'types/',
23+
rootDir: '/'
24+
}
25+
],
26+
build: {
27+
lib: {
28+
name,
29+
fileName: name,
30+
entry: path.resolve(__dirname, 'src/case/index.ts')
31+
},
32+
rollupOptions: {
33+
output: {
34+
sourcemap: true
35+
},
36+
external: ['@hufe921/canvas-editor']
37+
}
38+
}
39+
}
40+
}
41+
return {
42+
base: `/${name}/`,
43+
server: {
44+
host: '0.0.0.0'
45+
}
46+
}
47+
})

pnpm-lock.yaml

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)