mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-20 16:40:13 +01:00
⚡ Improve pasting performance for large amounts of content https://github.com/siyuan-note/siyuan/issues/15306
This commit is contained in:
parent
9d392bd663
commit
7c692e8273
4 changed files with 362 additions and 187 deletions
|
|
@ -4,10 +4,12 @@ import {transaction, updateTransaction} from "../wysiwyg/transaction";
|
||||||
import {getContenteditableElement} from "../wysiwyg/getBlock";
|
import {getContenteditableElement} from "../wysiwyg/getBlock";
|
||||||
import {
|
import {
|
||||||
fixTableRange,
|
fixTableRange,
|
||||||
focusBlock, focusByRange,
|
focusBlock,
|
||||||
|
focusByRange,
|
||||||
focusByWbr,
|
focusByWbr,
|
||||||
getEditorRange,
|
getEditorRange,
|
||||||
getSelectionOffset, setLastNodeRange,
|
getSelectionOffset,
|
||||||
|
setLastNodeRange,
|
||||||
} from "./selection";
|
} from "./selection";
|
||||||
import {Constants} from "../../constants";
|
import {Constants} from "../../constants";
|
||||||
import {highlightRender} from "../render/highlightRender";
|
import {highlightRender} from "../render/highlightRender";
|
||||||
|
|
@ -257,7 +259,9 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
|
||||||
// 移动端插入嵌入块时,获取到的 range 为旧值
|
// 移动端插入嵌入块时,获取到的 range 为旧值
|
||||||
useProtyleRange = false,
|
useProtyleRange = false,
|
||||||
// 在开头粘贴块则插入上方
|
// 在开头粘贴块则插入上方
|
||||||
insertByCursor = false) => {
|
insertByCursor = false,
|
||||||
|
// 是否需要再次 spin
|
||||||
|
spin = true) => {
|
||||||
if (html === "") {
|
if (html === "") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -364,7 +368,7 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
let innerHTML = unSpinHTML || // 在 table 中插入需要使用转换好的行内元素 https://github.com/siyuan-note/siyuan/issues/9358
|
let innerHTML = unSpinHTML || // 在 table 中插入需要使用转换好的行内元素 https://github.com/siyuan-note/siyuan/issues/9358
|
||||||
protyle.lute.SpinBlockDOM(html) || // 需要再 spin 一次 https://github.com/siyuan-note/siyuan/issues/7118
|
(spin && protyle.lute.SpinBlockDOM(html)) || // 需要再 spin 一次 https://github.com/siyuan-note/siyuan/issues/7118
|
||||||
html; // 空格会被 Spin 不再,需要使用原文
|
html; // 空格会被 Spin 不再,需要使用原文
|
||||||
// 粘贴纯文本时会进行内部转义,这里需要进行反转义 https://github.com/siyuan-note/siyuan/issues/10620
|
// 粘贴纯文本时会进行内部转义,这里需要进行反转义 https://github.com/siyuan-note/siyuan/issues/10620
|
||||||
innerHTML = innerHTML.replace(/;;;lt;;;/g, "<").replace(/;;;gt;;;/g, ">");
|
innerHTML = innerHTML.replace(/;;;lt;;;/g, "<").replace(/;;;gt;;;/g, ">");
|
||||||
|
|
|
||||||
|
|
@ -591,7 +591,7 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
|
||||||
textPlain = textPlain.replace(/\u200D```/g, "```");
|
textPlain = textPlain.replace(/\u200D```/g, "```");
|
||||||
|
|
||||||
const textPlainDom = protyle.lute.Md2BlockDOM(textPlain);
|
const textPlainDom = protyle.lute.Md2BlockDOM(textPlain);
|
||||||
insertHTML(textPlainDom, protyle, false, false, true);
|
insertHTML(textPlainDom, protyle, false, false, true, false /* 不再次 spin 以提升性能 https://github.com/siyuan-note/siyuan/issues/15306 */);
|
||||||
}
|
}
|
||||||
blockRender(protyle, protyle.wysiwyg.element);
|
blockRender(protyle, protyle.wysiwyg.element);
|
||||||
processRender(protyle.wysiwyg.element);
|
processRender(protyle.wysiwyg.element);
|
||||||
|
|
|
||||||
|
|
@ -764,6 +764,11 @@ func loadNodesByStartEnd(tree *parse.Tree, startID, endID string) (nodes []*ast.
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(nodes) >= Conf.Editor.DynamicLoadBlocks {
|
||||||
|
// 如果加载到指定数量的块则停止加载
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -167,6 +167,8 @@ func performTx(tx *Transaction) (ret *TxErr) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
isLargeInsert := tx.processLargeInsert()
|
||||||
|
if !isLargeInsert {
|
||||||
for _, op := range tx.DoOperations {
|
for _, op := range tx.DoOperations {
|
||||||
switch op.Action {
|
switch op.Action {
|
||||||
case "create":
|
case "create":
|
||||||
|
|
@ -316,6 +318,7 @@ func performTx(tx *Transaction) (ret *TxErr) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if cr := tx.commit(); nil != cr {
|
if cr := tx.commit(); nil != cr {
|
||||||
logging.LogErrorf("commit tx failed: %s", cr)
|
logging.LogErrorf("commit tx failed: %s", cr)
|
||||||
|
|
@ -324,6 +327,42 @@ func performTx(tx *Transaction) (ret *TxErr) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *Transaction) processLargeInsert() bool {
|
||||||
|
opSize := len(tx.DoOperations)
|
||||||
|
isLargeInsert := 128 < opSize
|
||||||
|
if isLargeInsert {
|
||||||
|
var previousID string
|
||||||
|
for i, op := range tx.DoOperations {
|
||||||
|
if i == opSize-1 {
|
||||||
|
if "delete" != op.Action {
|
||||||
|
// 最后一个是 delete
|
||||||
|
isLargeInsert = false
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if "insert" != op.Action {
|
||||||
|
isLargeInsert = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if "" == op.PreviousID {
|
||||||
|
isLargeInsert = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if "" == previousID {
|
||||||
|
previousID = op.PreviousID
|
||||||
|
} else if previousID != op.PreviousID {
|
||||||
|
isLargeInsert = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isLargeInsert {
|
||||||
|
tx.doLargeInsert(previousID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isLargeInsert
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *Transaction) doMove(operation *Operation) (ret *TxErr) {
|
func (tx *Transaction) doMove(operation *Operation) (ret *TxErr) {
|
||||||
var err error
|
var err error
|
||||||
id := operation.ID
|
id := operation.ID
|
||||||
|
|
@ -980,6 +1019,127 @@ func syncDelete2AttributeView(node *ast.Node) (changedAvIDs []string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *Transaction) doLargeInsert(previousID string) (ret *TxErr) {
|
||||||
|
tree, err := tx.loadTree(previousID)
|
||||||
|
if nil != err {
|
||||||
|
logging.LogErrorf("load tree [%s] failed: %s", previousID, err)
|
||||||
|
return &TxErr{code: TxErrCodeBlockNotFound, id: previousID}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, operation := range tx.DoOperations {
|
||||||
|
if "insert" != operation.Action {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
data := strings.ReplaceAll(operation.Data.(string), editor.FrontEndCaret, "")
|
||||||
|
subTree := tx.luteEngine.BlockDOM2Tree(data)
|
||||||
|
tx.processGlobalAssets(subTree)
|
||||||
|
|
||||||
|
insertedNode := subTree.Root.FirstChild
|
||||||
|
if nil == insertedNode {
|
||||||
|
return &TxErr{code: TxErrCodeBlockNotFound, msg: "invalid data tree", id: tree.ID}
|
||||||
|
}
|
||||||
|
var remains []*ast.Node
|
||||||
|
for remain := insertedNode.Next; nil != remain; remain = remain.Next {
|
||||||
|
if ast.NodeKramdownBlockIAL != remain.Type {
|
||||||
|
if "" == remain.ID {
|
||||||
|
remain.ID = ast.NewNodeID()
|
||||||
|
remain.SetIALAttr("id", remain.ID)
|
||||||
|
}
|
||||||
|
remains = append(remains, remain)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if "" == insertedNode.ID {
|
||||||
|
insertedNode.ID = ast.NewNodeID()
|
||||||
|
insertedNode.SetIALAttr("id", insertedNode.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
node := treenode.GetNodeInTree(tree, previousID)
|
||||||
|
if nil == node {
|
||||||
|
logging.LogErrorf("get node [%s] in tree [%s] failed", previousID, tree.Root.ID)
|
||||||
|
return &TxErr{code: TxErrCodeBlockNotFound, id: previousID}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ast.NodeHeading == node.Type && "1" == node.IALAttr("fold") {
|
||||||
|
children := treenode.HeadingChildren(node)
|
||||||
|
if l := len(children); 0 < l {
|
||||||
|
node = children[l-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ast.NodeList == insertedNode.Type && nil != node.Parent && ast.NodeList == node.Parent.Type {
|
||||||
|
insertedNode = insertedNode.FirstChild
|
||||||
|
}
|
||||||
|
for i := len(remains) - 1; 0 <= i; i-- {
|
||||||
|
remain := remains[i]
|
||||||
|
node.InsertAfter(remain)
|
||||||
|
}
|
||||||
|
node.InsertAfter(insertedNode)
|
||||||
|
|
||||||
|
createdUpdated(insertedNode)
|
||||||
|
tx.nodes[insertedNode.ID] = insertedNode
|
||||||
|
tx.trees[tree.ID] = tree
|
||||||
|
|
||||||
|
// 收集引用的定义块 ID
|
||||||
|
refDefIDs := getRefDefIDs(insertedNode)
|
||||||
|
// 推送定义节点引用计数
|
||||||
|
for _, defID := range refDefIDs {
|
||||||
|
task.AppendAsyncTaskWithDelay(task.SetDefRefCount, util.SQLFlushInterval, refreshRefCount, defID)
|
||||||
|
}
|
||||||
|
|
||||||
|
upsertAvBlockRel(insertedNode)
|
||||||
|
|
||||||
|
// 复制为副本时将该副本块插入到数据库中 https://github.com/siyuan-note/siyuan/issues/11959
|
||||||
|
avs := insertedNode.IALAttr(av.NodeAttrNameAvs)
|
||||||
|
for _, avID := range strings.Split(avs, ",") {
|
||||||
|
if !ast.IsNodeIDPattern(avID) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
AddAttributeViewBlock(tx, []map[string]interface{}{{
|
||||||
|
"id": insertedNode.ID,
|
||||||
|
"isDetached": false,
|
||||||
|
}}, avID, "", previousID, false)
|
||||||
|
ReloadAttrView(avID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ast.NodeAttributeView == insertedNode.Type {
|
||||||
|
// 插入数据库块时需要重新绑定其中已经存在的块
|
||||||
|
// 比如剪切操作时,会先进行 delete 数据库解绑块,这里需要重新绑定 https://github.com/siyuan-note/siyuan/issues/13031
|
||||||
|
attrView, parseErr := av.ParseAttributeView(insertedNode.AttributeViewID)
|
||||||
|
if nil == parseErr {
|
||||||
|
trees, toBindNodes := tx.getAttrViewBoundNodes(attrView)
|
||||||
|
for _, toBindNode := range toBindNodes {
|
||||||
|
t := trees[toBindNode.ID]
|
||||||
|
bindBlockAv0(tx, insertedNode.AttributeViewID, toBindNode, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置视图 https://github.com/siyuan-note/siyuan/issues/15279
|
||||||
|
v := attrView.GetView(attrView.ViewID)
|
||||||
|
if nil != v {
|
||||||
|
insertedNode.AttributeViewType = string(v.LayoutType)
|
||||||
|
attrs := parse.IAL2Map(insertedNode.KramdownIAL)
|
||||||
|
if "" == attrs[av.NodeAttrView] {
|
||||||
|
attrs[av.NodeAttrView] = v.ID
|
||||||
|
err = setNodeAttrs(insertedNode, tree, attrs)
|
||||||
|
if err != nil {
|
||||||
|
logging.LogWarnf("set node [%s] attrs failed: %s", operation.BlockID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
operation.ID = insertedNode.ID
|
||||||
|
operation.ParentID = insertedNode.Parent.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = tx.writeTree(tree); nil != err {
|
||||||
|
return &TxErr{code: TxErrCodeWriteTree, msg: err.Error(), id: tree.ID}
|
||||||
|
}
|
||||||
|
return tx.doDelete(tx.DoOperations[len(tx.DoOperations)-1])
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
|
func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
|
||||||
var bt *treenode.BlockTree
|
var bt *treenode.BlockTree
|
||||||
bts := treenode.GetBlockTrees([]string{operation.ParentID, operation.PreviousID, operation.NextID})
|
bts := treenode.GetBlockTrees([]string{operation.ParentID, operation.PreviousID, operation.NextID})
|
||||||
|
|
@ -1005,42 +1165,7 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
|
||||||
|
|
||||||
data := strings.ReplaceAll(operation.Data.(string), editor.FrontEndCaret, "")
|
data := strings.ReplaceAll(operation.Data.(string), editor.FrontEndCaret, "")
|
||||||
subTree := tx.luteEngine.BlockDOM2Tree(data)
|
subTree := tx.luteEngine.BlockDOM2Tree(data)
|
||||||
|
tx.processGlobalAssets(subTree)
|
||||||
if !tx.isGlobalAssetsInit {
|
|
||||||
tx.assetsDir = getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), filepath.Dir(filepath.Join(util.DataDir, bt.BoxID, bt.Path)))
|
|
||||||
tx.isGlobalAssets = strings.HasPrefix(tx.assetsDir, filepath.Join(util.DataDir, "assets"))
|
|
||||||
tx.isGlobalAssetsInit = true
|
|
||||||
}
|
|
||||||
if !tx.isGlobalAssets {
|
|
||||||
// 本地资源文件需要移动到用户手动建立的 assets 下 https://github.com/siyuan-note/siyuan/issues/2410
|
|
||||||
ast.Walk(subTree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
||||||
if !entering {
|
|
||||||
return ast.WalkContinue
|
|
||||||
}
|
|
||||||
|
|
||||||
if ast.NodeLinkDest == n.Type && bytes.HasPrefix(n.Tokens, []byte("assets/")) {
|
|
||||||
assetP := gulu.Str.FromBytes(n.Tokens)
|
|
||||||
assetPath, e := GetAssetAbsPath(assetP)
|
|
||||||
if nil != e {
|
|
||||||
logging.LogErrorf("get path of asset [%s] failed: %s", assetP, err)
|
|
||||||
return ast.WalkContinue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.HasPrefix(assetPath, filepath.Join(util.DataDir, "assets")) {
|
|
||||||
// 非全局 assets 则跳过
|
|
||||||
return ast.WalkContinue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 只有全局 assets 才移动到相对 assets
|
|
||||||
targetP := filepath.Join(tx.assetsDir, filepath.Base(assetPath))
|
|
||||||
if e = filelock.Rename(assetPath, targetP); err != nil {
|
|
||||||
logging.LogErrorf("copy path of asset from [%s] to [%s] failed: %s", assetPath, targetP, err)
|
|
||||||
return ast.WalkContinue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ast.WalkContinue
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
insertedNode := subTree.Root.FirstChild
|
insertedNode := subTree.Root.FirstChild
|
||||||
if nil == insertedNode {
|
if nil == insertedNode {
|
||||||
|
|
@ -1189,6 +1314,47 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *Transaction) processGlobalAssets(tree *parse.Tree) {
|
||||||
|
if !tx.isGlobalAssetsInit {
|
||||||
|
tx.assetsDir = getAssetsDir(filepath.Join(util.DataDir, tree.Box), filepath.Dir(filepath.Join(util.DataDir, tree.Box, tree.Path)))
|
||||||
|
tx.isGlobalAssets = strings.HasPrefix(tx.assetsDir, filepath.Join(util.DataDir, "assets"))
|
||||||
|
tx.isGlobalAssetsInit = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if tx.isGlobalAssets {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 本地资源文件需要移动到用户手动建立的 assets 下 https://github.com/siyuan-note/siyuan/issues/2410
|
||||||
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
|
if !entering {
|
||||||
|
return ast.WalkContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
if ast.NodeLinkDest == n.Type && bytes.HasPrefix(n.Tokens, []byte("assets/")) {
|
||||||
|
assetP := gulu.Str.FromBytes(n.Tokens)
|
||||||
|
assetPath, e := GetAssetAbsPath(assetP)
|
||||||
|
if nil != e {
|
||||||
|
logging.LogErrorf("get path of asset [%s] failed: %s", assetP, e)
|
||||||
|
return ast.WalkContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(assetPath, filepath.Join(util.DataDir, "assets")) {
|
||||||
|
// 非全局 assets 则跳过
|
||||||
|
return ast.WalkContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只有全局 assets 才移动到相对 assets
|
||||||
|
targetP := filepath.Join(tx.assetsDir, filepath.Base(assetPath))
|
||||||
|
if e = filelock.Rename(assetPath, targetP); e != nil {
|
||||||
|
logging.LogErrorf("copy path of asset from [%s] to [%s] failed: %s", assetPath, targetP, e)
|
||||||
|
return ast.WalkContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ast.WalkContinue
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
|
func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
|
||||||
id := operation.ID
|
id := operation.ID
|
||||||
tree, err := tx.loadTree(id)
|
tree, err := tx.loadTree(id)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue