Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/extensions/behavior/Clipboard/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {Fragment, type Node, type ResolvedPos, type Schema, Slice} from 'prosemirror-model';
import {type EditorState, Plugin, type Selection} from 'prosemirror-state';
import type {EditorView} from 'prosemirror-view';

import {type Parser, type Serializer, trackTransactionMetrics} from '../../../core';
import {type Logger2, globalLogger} from '../../../logger';
import '../../../types/spec';
import {tryCatch} from '../../../utils/helpers';
import {isNodeSelection, isTextSelection, isWholeSelection} from '../../../utils/selection';
import {type Parser, type Serializer, trackTransactionMetrics} from '#core';
import {Fragment, type Node, type ResolvedPos, type Schema, Slice} from '#pm/model';
import {type EditorState, Plugin, type Selection} from '#pm/state';
import type {EditorView} from '#pm/view';
import {type Logger2, globalLogger} from 'src/logger';
import 'src/types/spec';
import {tryCatch} from 'src/utils/helpers';
import {isNodeSelection, isTextSelection, isWholeSelection} from 'src/utils/selection';

import {BaseNode, pType} from '../../base/BaseSchema';

import {isInsideCode} from './code';
import {trimEmptyTableCells} from './table';
import {DataTransferType, extractTextContentFromHtml, isIosSafariShare, trimContent} from './utils';

export type ClipboardPluginOptions = {
Expand Down Expand Up @@ -236,6 +237,7 @@ function serializeSelected(view: EditorView, serializer: Serializer): SerializeR

if (sel.empty) return null;

// handle selection in codeblocks
if (getSharedDepthNode(sel).type.spec.code) {
const fragment = sel.content().content;
return {text: fragment.textBetween(0, fragment.size)};
Expand All @@ -255,16 +257,12 @@ function setClipboardData(data: DataTransfer, result: SerializeResult) {
}

function getCopyContent(state: EditorState): Slice {
const sel = state.selection;
let sel = state.selection;

if (isWholeSelection(sel)) {
return new Slice(state.doc.content, 0, 0);
}

if (isTextSelectionWithinSameTextBlock(sel)) {
return new Slice(createFragmentFromInlineSelection(state), 0, 0);
}

if (isNodeSelection(sel)) {
const {node} = sel;
if (node.type.spec.complex) {
Expand All @@ -289,6 +287,14 @@ function getCopyContent(state: EditorState): Slice {
}
}

if (isTextSelection(sel)) {
sel = trimEmptyTableCells(sel);
}

if (isTextSelectionWithinSameTextBlock(sel)) {
return new Slice(createFragmentFromInlineSelection(state, sel), 0, 0);
}

let slice = getSelectionContent(sel);

// replace first node with paragraph if needed
Expand Down Expand Up @@ -330,8 +336,7 @@ function isTextSelectionWithinSameTextBlock(sel: Selection) {
return isTextSelection(sel) && sel.$from.sameParent(sel.$to) && sel.$from.parent.isTextblock;
}

function createFragmentFromInlineSelection(state: EditorState) {
const sel = state.selection;
function createFragmentFromInlineSelection(state: EditorState, sel: Selection) {
const inlineSlice = state.doc.slice(sel.from, sel.to);
const isComplexTextblock = Boolean(sel.$from.parent.type.spec.complex);
const isAllContentSelected = sel.$from.parent.content.eq(inlineSlice.content);
Expand Down
Loading
Loading