@aomao/plugin-bold
Advanced tools
+21
| The MIT License (MIT) | ||
| Copyright (c) 2021-present AoMao (me@aomao.com) | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
+3
-3
| { | ||
| "name": "@aomao/plugin-bold", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "main": "src/index.ts", | ||
@@ -8,5 +8,5 @@ "author": "me@aomao.com", | ||
| "dependencies": { | ||
| "@aomao/engine": "^1.0.0" | ||
| "@aomao/engine": "^1.1.0" | ||
| }, | ||
| "gitHead": "681660591b39385b2cb5d1ebfdf7e80a4d314de6" | ||
| "gitHead": "66c11eac09986c498cb97f6684ed3610fbb27621" | ||
| } |
+7
-64
@@ -1,4 +0,3 @@ | ||
| import { NodeInterface, Plugin } from '@aomao/engine'; | ||
| import { Mark } from '@aomao/engine'; | ||
| const TAG_NAME = 'strong'; | ||
| export type Options = { | ||
@@ -8,70 +7,14 @@ hotkey?: string | Array<string>; | ||
| }; | ||
| export default class extends Plugin<Options> { | ||
| execute() { | ||
| if (!this.engine) return; | ||
| const mark = `<${TAG_NAME} />`; | ||
| const { change } = this.engine; | ||
| if (!this.queryState()) { | ||
| change.addMark(mark); | ||
| } else { | ||
| change.removeMark(mark); | ||
| } | ||
| export default class extends Mark<Options> { | ||
| static get pluginName() { | ||
| return 'bold'; | ||
| } | ||
| queryState() { | ||
| if (!this.engine) return; | ||
| const { change } = this.engine; | ||
| return change.marks.some(node => node.name === TAG_NAME); | ||
| } | ||
| tagName = 'strong'; | ||
| markdown = this.options.markdown !== false ? '**' : ''; | ||
| hotkey() { | ||
| return this.options.hotkey || 'mod+b'; | ||
| } | ||
| schema() { | ||
| return TAG_NAME; | ||
| } | ||
| //设置markdown | ||
| onKeydownSpace(event: KeyboardEvent, node: NodeInterface, text: string) { | ||
| if ( | ||
| !this.engine || | ||
| !text || | ||
| !text.match(/[*]$/) || | ||
| this.options.markdown === false || | ||
| node.type !== Node.TEXT_NODE | ||
| ) | ||
| return; | ||
| const { change } = this.engine; | ||
| let range = change.getRange(); | ||
| const markdownKey = '**'; | ||
| const key = markdownKey.replace(/(\*)/g, '\\$1'); | ||
| const match = new RegExp(`^(.*)${key}(.+?)${key}$`).exec(text); | ||
| if (match) { | ||
| const visibleChar = match[1] && /\S$/.test(match[1]); | ||
| const codeChar = match[2]; | ||
| event.preventDefault(); | ||
| let leftText = text.substr( | ||
| 0, | ||
| text.length - codeChar.length - 2 * markdownKey.length, | ||
| ); | ||
| node.get<Text>()!.splitText( | ||
| (leftText + codeChar).length + 2 * markdownKey.length, | ||
| ); | ||
| if (visibleChar) { | ||
| leftText += ' '; | ||
| } | ||
| node[0].nodeValue = leftText + codeChar; | ||
| range.setStart(node[0], leftText.length); | ||
| range.setEnd(node[0], (leftText + codeChar).length); | ||
| change.select(range); | ||
| this.execute(); | ||
| range = change.getRange(); | ||
| range.collapse(false); | ||
| change.select(range); | ||
| change.insertText('\xa0'); | ||
| return false; | ||
| } | ||
| return; | ||
| } | ||
| } |
3
50%1689
-18.29%15
-78.87%Updated