Skip to content

Commit

Permalink
feat: 리스트 인덴트 같이 붙여넣기 가능
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludovico7 committed Dec 5, 2024
1 parent 4589340 commit e16e731
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions client/src/features/editor/hooks/useCopyAndPaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ export const useCopyAndPaste = ({
[],
);

const indentChecker = (text: string) => {
const indent = text.match(/^(?:( {3})|( {6})|( {9}))/);
if (indent) {
return indent[0].length / 3;
}
return 0;
};

const handlePaste = useCallback(
(e: React.ClipboardEvent<HTMLDivElement>, blockRef: HTMLDivElement | null, block: Block) => {
e.preventDefault();
Expand Down Expand Up @@ -138,37 +146,30 @@ export const useCopyAndPaste = ({
(b) => b.id === block.id,
);
const textList = text.split("\n");
let prevQuote = false;
textList.forEach((line, index) => {
textList.forEach((line) => {
if (line.length === 0) return;
let currentLine = line;
const newBlock = editorCRDT.localInsert(currentBlockIndex, "");
if (currentLine.startsWith("- [ ]")) {
currentLine = currentLine.slice(2);
}
const newBlock = editorCRDT.localInsert(currentBlockIndex, "");
if (indentChecker(currentLine) > 0) {
newBlock.node.indent = indentChecker(currentLine);
currentLine = currentLine.slice(indentChecker(currentLine) * 3 + 1);
}
sendBlockInsertOperation({
type: "blockInsert",
node: newBlock.node,
pageId,
});
line.split("").forEach((char, index) => {
currentLine.split("").forEach((char, index) => {
sendCharInsertOperation(
newBlock.node.crdt.localInsert(index, char, newBlock.node.id, pageId),
);
});
const isMarkdownGrammer = checkMarkdownPattern(currentLine);
if (isMarkdownGrammer && newBlock.node.type === "p") {
let markdownText = isMarkdownGrammer.length;
if (isMarkdownGrammer.type === "blockquote" && prevQuote === false) {
prevQuote = true;
}
if (
isMarkdownGrammer.type === "blockquote" &&
prevQuote === true &&
currentLine === ">"
) {
prevQuote = false;
return;
}
if (isMarkdownGrammer.type === "checkbox" && currentLine.startsWith("[ ]")) {
markdownText += 1;
}
Expand All @@ -186,10 +187,8 @@ export const useCopyAndPaste = ({
} else {
let grammarCount = 0;
if (text.startsWith("- [ ]")) {
console.log("checkbox");
text = text.slice(2);
}
console.log(text);
// 텍스트를 한 글자씩 순차적으로 삽입
text.split("").forEach((char, index) => {
const insertPosition = caretPosition + index;
Expand Down

0 comments on commit e16e731

Please sign in to comment.