🐛 Fixed the issue of block creation

This commit is contained in:
2025-06-19 13:40:25 +08:00
parent a62ea251cd
commit f2894b2a89
8 changed files with 82 additions and 62 deletions

View File

@@ -22,7 +22,7 @@ onMounted(async () => {
<template>
<div class="app-container">
<WindowTitleBar />
<div class="app-content" :style="{ marginTop: systemStore.titleBarHeight }">
<div class="app-content">
<router-view/>
</div>
</div>

View File

@@ -127,11 +127,7 @@ onUnmounted(() => {
border-bottom: 1px solid var(--toolbar-border, #d0d0d0);
user-select: none;
-webkit-user-select: none;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1000;
width: 100%;
font-family: 'Ubuntu', 'Cantarell', 'DejaVu Sans', system-ui, sans-serif;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

View File

@@ -132,11 +132,7 @@ onUnmounted(() => {
border-bottom: 1px solid var(--toolbar-border, rgba(0, 0, 0, 0.1));
user-select: none;
-webkit-user-select: none;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1000;
width: 100%;
font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', system-ui, sans-serif;
-webkit-context-menu: none;

View File

@@ -126,11 +126,7 @@ onUnmounted(() => {
border-bottom: 1px solid var(--toolbar-border);
user-select: none;
-webkit-user-select: none;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1000;
width: 100%;
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
-webkit-context-menu: none;

View File

@@ -182,13 +182,15 @@ const blockLayer = layer({
const preventFirstBlockFromBeingDeleted = EditorState.changeFilter.of((tr: any) => {
const protect: number[] = [];
// 获取块状态
// 获取块状态并获取第一个块的分隔符大小
const blocks = tr.startState.field(blockState);
if (blocks && blocks.length > 0) {
const firstBlock = blocks[0];
// 保护第一个块的分隔符区域
if (firstBlock && firstBlock.delimiter) {
protect.push(firstBlock.delimiter.from, firstBlock.delimiter.to);
const firstBlockDelimiterSize = firstBlock.delimiter.to;
// 保护第一个块分隔符区域(从 0 到 firstBlockDelimiterSize
if (firstBlockDelimiterSize > 0) {
protect.push(0, firstBlockDelimiterSize);
}
}
@@ -210,19 +212,19 @@ const preventFirstBlockFromBeingDeleted = EditorState.changeFilter.of((tr: any)
* 使用 transactionFilter 来确保选择不会在第一个块之前
*/
const preventSelectionBeforeFirstBlock = EditorState.transactionFilter.of((tr: any) => {
// 获取块状态
// 获取块状态并获取第一个块的分隔符大小
const blocks = tr.startState.field(blockState);
if (!blocks || blocks.length === 0) {
return tr;
}
const firstBlock = blocks[0];
if (!firstBlock || !firstBlock.delimiter) {
const firstBlockDelimiterSize = firstBlock.delimiter.to;
if (firstBlockDelimiterSize <= 0) {
return tr;
}
const firstBlockDelimiterSize = firstBlock.delimiter.to;
// 检查选择范围,如果在第一个块之前,则调整到第一个块的内容开始位置
if (tr.selection) {
tr.selection.ranges.forEach((range: any) => {

View File

@@ -90,7 +90,13 @@ export function getBlocksFromSyntaxTree(state: EditorState): Block[] | null {
}
});
return blocks.length > 0 ? blocks : null;
if (blocks.length > 0) {
// 设置第一个块分隔符的大小
firstBlockDelimiterSize = blocks[0].delimiter.to;
return blocks;
}
return null;
}
// 跟踪第一个分隔符的大小
@@ -129,13 +135,20 @@ export function getBlocksFromString(state: EditorState): Block[] {
const delim = "\n∞∞∞";
let pos = 0;
// 检查文档是否以分隔符开始
if (!content.startsWith("∞∞∞")) {
// 检查文档是否以分隔符开始(不带前导换行符)
if (content.startsWith("∞∞∞")) {
// 文档直接以分隔符开始,调整为标准格式
pos = 0;
} else if (content.startsWith("\n∞∞∞")) {
// 文档以换行符+分隔符开始这是标准格式从位置0开始解析
pos = 0;
} else {
// 如果文档不以分隔符开始,查找第一个分隔符
const firstDelimPos = content.indexOf(delim);
if (firstDelimPos === -1) {
// 如果没有找到分隔符,整个文档作为一个文本块
firstBlockDelimiterSize = 0;
return [{
language: {
name: 'text',
@@ -181,13 +194,32 @@ export function getBlocksFromString(state: EditorState): Block[] {
}
while (pos < doc.length) {
const blockStart = content.indexOf(delim, pos);
if (blockStart !== pos) {
// 如果在当前位置没有找到分隔符,可能是文档结尾
break;
let blockStart: number;
if (pos === 0 && content.startsWith("∞∞∞")) {
// 处理文档开头直接是分隔符的情况(不带前导换行符)
blockStart = 0;
} else if (pos === 0 && content.startsWith("\n∞∞∞")) {
// 处理文档开头是换行符+分隔符的情况(标准格式)
blockStart = 0;
} else {
blockStart = content.indexOf(delim, pos);
if (blockStart !== pos) {
// 如果在当前位置没有找到分隔符,可能是文档结尾
break;
}
}
// 确定语言开始位置
let langStart: number;
if (pos === 0 && content.startsWith("∞∞∞")) {
// 文档直接以分隔符开始,跳过 ∞∞∞
langStart = blockStart + 3;
} else {
// 标准情况,跳过 \n∞∞∞
langStart = blockStart + delim.length;
}
const langStart = blockStart + delim.length;
const delimiterEnd = content.indexOf("\n", langStart);
if (delimiterEnd < 0) {
console.error("Error parsing blocks. Delimiter didn't end with newline");
@@ -230,11 +262,6 @@ export function getBlocksFromString(state: EditorState): Block[] {
blocks.push(block);
pos = blockEnd;
// 设置第一个块分隔符的大小(只有当这是第一个有分隔符的块时)
if (blocks.length === 1 && firstBlockDelimiterSize === undefined) {
firstBlockDelimiterSize = block.delimiter.to;
}
}
// 如果没有找到任何块,创建一个默认块
@@ -258,7 +285,10 @@ export function getBlocksFromString(state: EditorState): Block[] {
},
});
firstBlockDelimiterSize = 0;
}
} else {
// 设置第一个块分隔符的大小
firstBlockDelimiterSize = blocks[0].delimiter.to;
}
return blocks;
}