Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyaoL committed Mar 3, 2023
1 parent 0c17984 commit 7f28289
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 35 deletions.
20 changes: 15 additions & 5 deletions src/YaliEditor/src/ir/IRDeletekeyProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
*/
import YaliEditor from '../index'
import {
IRfindClosestLi,
IRfindClosestMdBlock,
IRfindClosestMdInline
IRfindClosestMdInline,
IRfindClosestQuote
} from '../util/findElement';
import CONSTANTS from "../constant/constants";
import rangy from "rangy";
import { isMdBlockFence, isMdBlockTable, isMdBlockParagraph, isMdBlockMath, isMdBlockHr, isMdBlockHTML, isMdInline, isMdInlineFont, isMdInlineLink, isMdBlockListItem, isMdInlineImg, isMdBlockMeta, isMdBlockCode, isMdBlockHeading, isMdInlineEmoji } from "../util/inspectElement";
import { isMdBlockFence, isMdBlockTable, isMdBlockParagraph, isMdBlockMath, isMdBlockHr, isMdBlockHTML, isMdInline, isMdInlineFont, isMdInlineLink, isMdBlockListItem, isMdInlineImg, isMdBlockMeta, isMdBlockCode, isMdBlockHeading, isMdInlineEmoji, isMdBlockQuote } from "../util/inspectElement";
import { KeyProcessor } from './KeyProcessor'
import { sortBy } from 'lodash';

Expand Down Expand Up @@ -37,9 +39,7 @@ class IRDeletekeyProcessor implements KeyProcessor {
this.editor.ir.focueProcessor.updateFocusElement()
return true
}

//
//退化失败后,mdBlock-Math和mdBlock-Fance将不做任何处理

if (isMdInlineLink(mdInline) && this.editor.ir.state.linkDelete(mdBlock, mdInline)) {
this.editor.ir.observer.flush()
return true
Expand Down Expand Up @@ -78,7 +78,17 @@ class IRDeletekeyProcessor implements KeyProcessor {
return true
}

const li = IRfindClosestLi(mdBlock)
const quote = IRfindClosestQuote(mdBlock)
if (isMdBlockListItem(li) && this.editor.ir.state.listItmeDelete(li, mdInline)) {
this.editor.ir.observer.flush()
return true
}else if(isMdBlockQuote(quote) && this.editor.ir.state.quoteBlockDelete(quote,mdInline)) {
this.editor.ir.observer.flush()
return true
}

this.editor.ir.focueProcessor.updateFocusElement()

return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/YaliEditor/src/ir/IRInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class IRInputProcessor {
if (event.isComposing) {
return;
}



const mdBlock = this.editor.ir.focueProcessor.selectedBlockMdElement
const mdInline = this.editor.ir.focueProcessor.selectedInlineMdElement
//根据输入位置发布不同的事件
Expand Down Expand Up @@ -83,7 +84,6 @@ class IRInputProcessor {
}

this.editor.ir.observer.flush()

}

}
Expand Down
29 changes: 26 additions & 3 deletions src/YaliEditor/src/ir/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
p {
position: relative;
min-height: 22.4px;
white-space: pre-wrap;
/*white-space: pre-wrap;*/
padding-bottom: 6px;
padding-top: 6px;
margin-block-start: 0;
Expand Down Expand Up @@ -87,17 +87,22 @@
background-color: var(--yali-code-font-background-color);
font-size: large;
}
}



}

ul[md-block="bullet_list"],ul[md-block="ordered_list"]{
padding-inline-start: 25px;
}


p.md-focus::before {
color: var(--yali-md-focus-before-color);
border: 1px solid;
border-radius: 3px;
position: absolute;
left: -1.357143rem;
left: -21px;
top: 10px;
font-size: 11px;
font-weight: 500;
Expand All @@ -108,6 +113,24 @@
content: "P";
}

pre[md-block="fence"].md-focus::before {
color: var(--yali-md-focus-before-color);
border: 1px solid;
border-radius: 3px;
position: absolute;
left: -21px;
top: 6px;
font-size: 11px;
font-weight: 500;
padding-left: 2px;
padding-right: 2px;
vertical-align: bottom;
line-height: normal;
content: "F";
}



pre {
border-radius: 5px;
background-color: var(--yali-pre-background-color);
Expand Down
1 change: 1 addition & 0 deletions src/YaliEditor/src/state/fenceCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const fenceCtrl = IRState => {
}

IRState.prototype.fenceEnter = function (mdBlock: HTMLElement) {

const editorTool = (this.editor.editorTool as EditorTool)
if (isMdBlockFence(mdBlock) && mdBlock.hasAttribute(constants.ATTR_MD_LIKE)) {
//创建代码块
Expand Down
24 changes: 22 additions & 2 deletions src/YaliEditor/src/state/listCtrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getUniqueKey } from "@/markdown-it-plugin/markdown-it-key-generator"
import rangy from "rangy"
import YaLiEditor from ".."
import { isMdBlockFence, isMdBlockMath, isMdBlockParagraph } from "../util/inspectElement"

const listCtrl = IRState => {

Expand All @@ -16,12 +18,30 @@ const listCtrl = IRState => {
}


IRState.prototype.listItmeDelete = function () {
return true
IRState.prototype.listItmeDelete = function (li:HTMLElement) {
const editor = this.editor as YaLiEditor
const block = editor.ir.focueProcessor.getSelectedBlockMdElement(false)

if(block && isMdBlockParagraph(block) && this.editor.domTool.isTextEmptyElement(block) && !block.nextElementSibling && !block.previousElementSibling){
const ul = li.parentElement
li.remove()
if(ul.childElementCount == 0){
const p = editor.markdownTool.nodeDegenerateToP(ul)
p.focus()
}
return true
}

return false
}

IRState.prototype.listItmeEnter = function (li) {

const editor = this.editor as YaLiEditor
const block = editor.ir.focueProcessor.getSelectedBlockMdElement(false)

if(isMdBlockFence(block) || isMdBlockMath(block)) return false

const sel = rangy.getSelection()
let { start, end } = this.editor.domTool.splitElementAtCursor(li)

Expand Down
2 changes: 1 addition & 1 deletion src/YaliEditor/src/state/mathBlockCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const mathBlockCtrl = IRState => {
}

IRState.prototype.mathBlockInput = function(mdBlock,mdInline,event){
return true
return false
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/YaliEditor/src/state/metaBlockCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const metaBlockCtrl = IRState => {
}

IRState.prototype.metaBlockInput = function(mdBlock,mdInline,event){
return true
return false
}
}

Expand Down
51 changes: 33 additions & 18 deletions src/YaliEditor/src/state/paragraphCtrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import YaLiEditor from "..";
import { isMdBlockFence, isMdBlockHr, isMdBlockListItem, isMdBlockParagraph, isMdInlineFont, isMdInlineLink } from "../util/inspectElement";
import { isMdBlockFence, isMdBlockHr, isMdBlockListItem, isMdBlockMath, isMdBlockParagraph, isMdInlineFont, isMdInlineLink } from "../util/inspectElement";
import IRState from "./IRState";
import rangy from "rangy";
import { createParagraph } from "../util/createElement";
Expand All @@ -8,13 +8,20 @@ import { IRfindClosestLi } from "../util/findElement";

const paragraphCtrl = IRState => {

IRState.prototype.paragraphDelete = function (mdBlock) {
IRState.prototype.paragraphDelete = function (mdBlock: HTMLElement) {

const sel = rangy.getSelection()
const r = sel.getRangeAt(0).cloneRange() as RangyRange
const start = r.startContainer
const mdBlockPreviousElement = mdBlock.previousElementSibling



//在listItem下,交付给listItem处理
if (this.editor.domTool.isTextEmptyElement(mdBlock) && isMdBlockListItem(mdBlock.parentElement)) {
return false
}

//临近Hr附近
if (this.editor.domTool.isTextEmptyElement(mdBlock) && isMdBlockHr(mdBlockPreviousElement)) {
mdBlockPreviousElement.remove()
Expand All @@ -37,7 +44,7 @@ const paragraphCtrl = IRState => {
editor.ir.renderer.codemirrorManager.viewFocus(mdBlockPreviousElement.id)
return true
}

if (this.editor.ir.rootElement.childElementCount == 1 && this.editor.domTool.isTextEmptyElement(mdBlock)) {
return true
}
Expand Down Expand Up @@ -112,41 +119,43 @@ const paragraphCtrl = IRState => {
}
}

IRState.prototype.paragraphInput = function (mdBlock:HTMLElement, mdInline, event) {
if(event.data == " ") return false
IRState.prototype.paragraphInput = function (mdBlock: HTMLElement, mdInline, event) {
if (event.data == " ") return false

const sel = rangy.getSelection()
const r = sel.getRangeAt(0)
const mark = r.getBookmark(mdBlock)
const expectLength = mdBlock.textContent.length

const block = this.paragraphRefresh(mdBlock, mdInline) as HTMLElement
if (block){
if (block) {
(mark as any).containerNode = block
//compute expectLength
if(block.textContent.length != expectLength){
const bias = block.textContent.length-expectLength
mark.end = mark.end+bias
mark.start = mark.start+bias
if (block.textContent.length != expectLength) {
const bias = block.textContent.length - expectLength
mark.end = mark.end + bias
mark.start = mark.start + bias
}
}

try {
r.moveToBookmark(mark)
sel.setSingleRange(r)

} catch (err) {
sel.collapse(mdBlock, mdBlock.childNodes.length)
}
this.editor.ir.focueProcessor.updateFocusElement()


return block
}

IRState.prototype.paragraphRefresh = function (mdBlock:HTMLElement, mdInline) {
IRState.prototype.paragraphRefresh = function (mdBlock: HTMLElement, mdInline) {
if (!isMdBlockParagraph(mdBlock)) return false

const editor = this.editor as YaLiEditor

//尝试刷新行内的所有文本
if (this.editor.markdownTool.reRenderInlineElementAtBlock(mdBlock)) {
//this.editor.ir.focueProcessor.updateFocusElement()
Expand All @@ -155,12 +164,18 @@ const paragraphCtrl = IRState => {

//尝试对整个块进行转换
mdBlock = this.editor.markdownTool.mdBlockTransform(mdBlock) as HTMLElement



if (!mdBlock) return false
/*if(mdBlock.hasAttribute("md-like")){
(this.editor as YaLiEditor).editorTool.createSuggestionPopper(mdBlock,[{value:"fsasdfasdf"}])
}*/

if (isMdBlockMath(mdBlock)) {
const container = mdBlock.querySelector(".markdown-it-code-beautiful")
editor.ir.renderer.codemirrorManager.refreshStateCacheByElement(container)
editor.ir.focueProcessor.updateFocusMdBlockByStart(mdBlock)
setTimeout(() => {
editor.ir.renderer.codemirrorManager.viewFocus(container.id)
})
}

return mdBlock
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/YaliEditor/src/state/quoteBlockCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
const quoteBlockCtrl = IRState => {

IRState.prototype.quoteBlockDelete = function (mdBlock) {
return true
return false
}

IRState.prototype.quoteBlockEnter = function(mdBlock){
return true

return false
}

IRState.prototype.quoteBlockInput = function(mdBlock,mdInline,event){
return true
return false
}
}

Expand Down

0 comments on commit 7f28289

Please sign in to comment.