Skip to content

Commit

Permalink
Merge branch 'release/1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
pipisebastian committed Dec 5, 2024
2 parents 2688668 + be88b84 commit 6c2581c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 42 deletions.
98 changes: 58 additions & 40 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 @@ -128,7 +136,7 @@ export const useCopyAndPaste = ({

editorCRDT.currentBlock!.crdt.currentCaret = caretPosition + metadata.length;
} else {
const text = e.clipboardData.getData("text/plain");
let text = e.clipboardData.getData("text/plain");
if (!block || text.length === 0) return;

const caretPosition = block.crdt.currentCaret;
Expand All @@ -138,52 +146,49 @@ export const useCopyAndPaste = ({
(b) => b.id === block.id,
);
const textList = text.split("\n");
textList.forEach((line, index) => {
if (index === 0) {
line.split("").forEach((char, index) => {
const charNode = block.crdt.localInsert(index, char, block.id, pageId);
sendCharInsertOperation({
type: "charInsert",
node: charNode.node,
blockId: block.id,
pageId,
});
});
const isMarkdownGrammer = checkMarkdownPattern(line);
if (isMarkdownGrammer && block.type === "p") {
block.type = isMarkdownGrammer.type;
sendBlockUpdateOperation(editorCRDT.localUpdate(block, pageId));
for (let i = 0; i < isMarkdownGrammer.length; i++) {
sendCharDeleteOperation(block.crdt.localDelete(0, block.id, pageId));
}
textList.forEach((line) => {
if (line.length === 0) return;
let currentLine = line;
const newBlock = editorCRDT.localInsert(currentBlockIndex, "");
if (currentLine.startsWith("- [ ]")) {
currentLine = currentLine.slice(2);
}
if (indentChecker(currentLine) > 0) {
newBlock.node.indent = indentChecker(currentLine);
currentLine = currentLine.slice(indentChecker(currentLine) * 3 + 1);
}
sendBlockInsertOperation({
type: "blockInsert",
node: newBlock.node,
pageId,
});
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 === "checkbox" && currentLine.startsWith("[ ]")) {
markdownText += 1;
}
} else {
const newBlock = editorCRDT.localInsert(currentBlockIndex, "");
sendBlockInsertOperation({
type: "blockInsert",
node: newBlock.node,
pageId,
});
line.split("").forEach((char, index) => {
sendCharInsertOperation(
newBlock.node.crdt.localInsert(index, char, newBlock.node.id, pageId),
newBlock.node.type = isMarkdownGrammer.type;
sendBlockUpdateOperation(editorCRDT.localUpdate(newBlock.node, pageId));
for (let i = 0; i <= markdownText; i++) {
sendCharDeleteOperation(
newBlock.node.crdt.localDelete(0, newBlock.node.id, pageId),
);
});
const isMarkdownGrammer = checkMarkdownPattern(line);
if (isMarkdownGrammer && newBlock.node.type === "p") {
newBlock.node.type = isMarkdownGrammer.type;
sendBlockUpdateOperation(editorCRDT.localUpdate(newBlock.node, pageId));
for (let i = 0; i < isMarkdownGrammer.length; i++) {
sendCharDeleteOperation(
newBlock.node.crdt.localDelete(0, newBlock.node.id, pageId),
);
}
}
}
currentBlockIndex += 1;
});
editorCRDT.LinkedList.updateAllOrderedListIndices();
} else {
let grammarCount = 0;
if (text.startsWith("- [ ]")) {
text = text.slice(2);
}
// ํ…์ŠคํŠธ๋ฅผ ํ•œ ๊ธ€์ž์”ฉ ์ˆœ์ฐจ์ ์œผ๋กœ ์‚ฝ์ž…
text.split("").forEach((char, index) => {
const insertPosition = caretPosition + index;
Expand All @@ -195,8 +200,21 @@ export const useCopyAndPaste = ({
pageId,
});
});
const isMarkdownGrammer = checkMarkdownPattern(text);
if (isMarkdownGrammer && block.type === "p") {
block.type = isMarkdownGrammer.type;
let markdownText = isMarkdownGrammer.length;
sendBlockUpdateOperation(editorCRDT.localUpdate(block, pageId));
if (isMarkdownGrammer.type === "checkbox" && text.startsWith("[ ]")) {
markdownText += 1;
}
for (let i = 0; i < markdownText; i++) {
sendCharDeleteOperation(block.crdt.localDelete(0, block.id, pageId));
grammarCount += 1;
}
}
// ์บ๋Ÿฟ ์œ„์น˜ ์—…๋ฐ์ดํŠธ
editorCRDT.currentBlock!.crdt.currentCaret = caretPosition + text.length;
editorCRDT.currentBlock!.crdt.currentCaret = caretPosition + text.length - grammarCount;
}
}

Expand Down
7 changes: 6 additions & 1 deletion client/src/features/workSpace/hooks/usePagesManage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
import { Page as CRDTPage } from "@noctaCrdt/Page";
import { WorkSpace } from "@noctaCrdt/WorkSpace";
import { useEffect, useState, useRef, useCallback } from "react";
import { PAGE, SIDE_BAR } from "@src/constants/size";
import { useSocketStore } from "@src/stores/useSocketStore";
import { useToastStore } from "@src/stores/useToastStore";
import { Page } from "@src/types/page";
import { PAGE, SIDE_BAR } from "@src/constants/size";

const PAGE_OFFSET = 60;

Expand Down Expand Up @@ -242,6 +242,11 @@ export const usePagesManage = (workspace: WorkSpace | null, clientId: number | n
page.id === pageId ? { ...page, isVisible: false, isLoaded: false } : page,
),
);
// Socket.IO๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์— ํŽ˜์ด์ง€ ํ‡ด์žฅ ์•Œ๋ฆผ
const socketStore = useSocketStore.getState();
if (socketStore.socket) {
socketStore.socket.emit("leave/page", { pageId });
}
};
const updatePage = (
pageId: string,
Expand Down
14 changes: 13 additions & 1 deletion client/src/utils/caretUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,19 @@ export const setCaretPosition = ({
}

const range = document.createRange();
range.setStart(targetNode, targetOffset);
try {
range.setStart(targetNode, targetOffset);
} catch (rangeError) {
// setStart์— ์‹คํŒจํ•œ ๊ฒฝ์šฐ, ์ฒซ ๋ฒˆ์งธ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์„œ position ์œ„์น˜์— ์บ๋Ÿฟ ์„ค์ •
const firstTextNode = walker.firstChild();
if (firstTextNode) {
const textLength = firstTextNode.textContent?.length || 0;
range.setStart(firstTextNode, Math.min(position, textLength));
} else {
// ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ํŽธ์ง‘ ๊ฐ€๋Šฅํ•œ div์˜ ์‹œ์ž‘์ ์— ์„ค์ •
range.setStart(editableDiv, 0);
}
}
range.collapse(true);

selection.removeAllRanges();
Expand Down

0 comments on commit 6c2581c

Please sign in to comment.