Skip to content

Commit

Permalink
fix: paste slice into the heading node #94
Browse files Browse the repository at this point in the history
  • Loading branch information
HMarzban committed May 3, 2023
1 parent 30163f4 commit a40dbb7
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions packages/nextjs/components/TipTap/extentions/clipboardPast.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,47 +62,48 @@ export default (slice, editor) => {
// if user cursor is in the heading,
// move the cursor to the contentWrapper and do the rest
if ($from.parent.type.name === CONTENT_HEADING_TYPE) {
const firstLine = doc.nodeAt(start + 2)

let resolveNextBlock = tr.doc.resolve(start + 2)

newPosResolver = resolveNextBlock

// if the heading block does not contain contentWrapper as a first child
// then create a contentWrapper block
if (firstLine.type.name === HEADING_TYPE) {
const contentWrapperBlock = {
type: CONTENT_WRAPPER_TYPE,
content: [
{
type: PARAGRAPH_TYPE
}
]

const nextPos = $from.after();
const $nextPos = tr.doc.resolve(nextPos);

// Check if the next node is a contentWrapper
if ($nextPos.nodeAfter && $nextPos.nodeAfter.type.name === CONTENT_WRAPPER_TYPE) {
const contentWrapperStart = nextPos + 1; // +1 to move inside the contentWrapper

// Find the first paragraph or any other block inside the contentWrapper
let firstBlockInsideContentWrapper = $nextPos.nodeAfter.firstChild;

// if the heading block does not contain contentWrapper as a first child
// then create a contentWrapper block
if (!firstBlockInsideContentWrapper || firstBlockInsideContentWrapper.type.name === HEADING_TYPE) {
const contentWrapperBlock = {
type: CONTENT_WRAPPER_TYPE,
content: [
{
type: PARAGRAPH_TYPE
}
]
};

const node = createNodeFromJSON(contentWrapperBlock, state.schema);
tr.insert(contentWrapperStart, node);
firstBlockInsideContentWrapper = node.firstChild;
}

const node = createNodeFromJSON(contentWrapperBlock, state.schema)
if (firstBlockInsideContentWrapper) {
const firstBlockStart = contentWrapperStart + 1; // +1 to move inside the first block

tr.insert(start, node)
resolveNextBlock = tr.doc.resolve(start + 2)
}
// Move the cursor to the first line of the contentWrapper
const newSelection = new TextSelection(tr.doc.resolve(firstBlockStart));
tr.setSelection(newSelection);

// put the selection to the first line of contentWrapper block
if (resolveNextBlock.parent.type.name === CONTENT_WRAPPER_TYPE) {
tr.setSelection(TextSelection.near(resolveNextBlock))
// Update the variables accordingly
newPosResolver = $nextPos;
$from = newSelection.$from;
start = $from.pos;
}
}
}

// If caret selection move to contentWrapper, create a new selection
if (newPosResolver) {
$from = (new Selection(
newPosResolver,
newPosResolver
)).$from
}

start = $from.pos

// return Slice.empty

const titleNode = $from.doc.nodeAt($from.start(1) - 1)
Expand All @@ -119,9 +120,6 @@ export default (slice, editor) => {
// if there is no heading block, then just return
if (headings.length <= 0) return slice

// return Slice.empty
let shouldNested = false

tr.delete(to, titleEndPos)

if (paragraphs.length !== 0) {
Expand Down

0 comments on commit a40dbb7

Please sign in to comment.