This commit is contained in:
Tom Moor committed 2023-09-12 22:23:38 -04:00
1 parent b80ee89588
commit cca4e256e0
4 files changed
+276 -38

No files matched your search

+8
View File
@@ -11,4 +11,12 @@ declare module "prosemirror-view" {
plainText: boolean,
$context: ResolvedPos
);
export function __serializeForClipboard(
view: EditorView,
slice: Slice
): {
dom: Element;
text: string;
};
}
+55 -38
View File
@@ -1,6 +1,7 @@
/* eslint-disable no-irregular-whitespace */
import { lighten, transparentize } from "polished";
import styled, { DefaultTheme, css, keyframes } from "styled-components";
import breakpoint from "styled-components-breakpoint";
export type Props = {
rtl: boolean;
@@ -249,6 +250,45 @@ const findAndReplaceStyle = () => css`
}
`;
const dragAndDropStyle = (props: Props) => css`
.drag-handle {
display: none;
pointer-events: none;
position: fixed;
opacity: 1;
transition: opacity ease-in 100ms;
border-radius: 4px;
background: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3QgeD0iOCIgeT0iNyIgd2lkdGg9IjMiIGhlaWdodD0iMiIgcng9IjEiIGZpbGw9IiM0RTVDNkUiLz4KPHJlY3QgeD0iOCIgeT0iMTEiIHdpZHRoPSIzIiBoZWlnaHQ9IjIiIHJ4PSIxIiBmaWxsPSIjNEU1QzZFIi8+CjxyZWN0IHg9IjgiIHk9IjE1IiB3aWR0aD0iMyIgaGVpZ2h0PSIyIiByeD0iMSIgZmlsbD0iIzRFNUM2RSIvPgo8cmVjdCB4PSIxMyIgeT0iNyIgd2lkdGg9IjMiIGhlaWdodD0iMiIgcng9IjEiIGZpbGw9IiM0RTVDNkUiLz4KPHJlY3QgeD0iMTMiIHk9IjExIiB3aWR0aD0iMyIgaGVpZ2h0PSIyIiByeD0iMSIgZmlsbD0iIzRFNUM2RSIvPgo8cmVjdCB4PSIxMyIgeT0iMTUiIHdpZHRoPSIzIiBoZWlnaHQ9IjIiIHJ4PSIxIiBmaWxsPSIjNEU1QzZFIi8+Cjwvc3ZnPgo=");
background-repeat: no-repeat;
background-position: 0 2px;
width: 24px;
height: 24px;
z-index: 50;
cursor: grab;
&:hover {
color: ${props.theme.textSecondary};
transition: background-color 0.2s;
}
&:active {
color: ${props.theme.text};
transition: background-color 0.2s;
}
&.hidden {
opacity: 0;
pointer-events: none;
transition: none;
}
${breakpoint("tablet")`
display: block;
pointer-events: auto;
`}
}
`;
const style = (props: Props) => `
flex-grow: ${props.grow ? 1 : 0};
justify-content: start;
@@ -466,9 +506,16 @@ li {
caret-color: transparent;
}
.ProseMirror-selectednode {
outline: 2px solid
${props.readOnly ? "transparent" : props.theme.selected};
.ProseMirror:not(.dragging) .ProseMirror-selectednode {
outline: 4px solid ${
props.readOnly ? "transparent" : transparentize(0.9, props.theme.accent)
};
border-radius: 4px;
background-color: ${
props.readOnly ? "transparent" : transparentize(0.9, props.theme.accent)
};
transition: background-color 100ms;
box-shadow: none;
}
/* Make sure li selections wrap around markers */
@@ -885,8 +932,8 @@ a:hover {
ul,
ol {
margin: ${props.rtl ? "0 -26px 0 0.1em" : "0 0.1em 0 -26px"};
padding: ${props.rtl ? "0 48px 0 0" : "0 0 0 48px"};
margin: 0;
padding: ${props.rtl ? "0 22px 0 0" : "0 0 0 22px"};
}
ol ol {
@@ -899,8 +946,7 @@ ol ol ol {
ul.checkbox_list {
padding: 0;
margin-left: ${props.rtl ? "0" : "-24px"};
margin-right: ${props.rtl ? "-24px" : "0"};
margin: 0;
}
ul li,
@@ -920,48 +966,17 @@ ol li {
ul.checkbox_list > li {
display: flex;
list-style: none;
padding-${props.rtl ? "right" : "left"}: 24px;
}
ul.checkbox_list > li.checked > div > p {
color: ${props.theme.textTertiary};
}
ul li::before,
ol li::before {
background: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3QgeD0iOCIgeT0iNyIgd2lkdGg9IjMiIGhlaWdodD0iMiIgcng9IjEiIGZpbGw9IiM0RTVDNkUiLz4KPHJlY3QgeD0iOCIgeT0iMTEiIHdpZHRoPSIzIiBoZWlnaHQ9IjIiIHJ4PSIxIiBmaWxsPSIjNEU1QzZFIi8+CjxyZWN0IHg9IjgiIHk9IjE1IiB3aWR0aD0iMyIgaGVpZ2h0PSIyIiByeD0iMSIgZmlsbD0iIzRFNUM2RSIvPgo8cmVjdCB4PSIxMyIgeT0iNyIgd2lkdGg9IjMiIGhlaWdodD0iMiIgcng9IjEiIGZpbGw9IiM0RTVDNkUiLz4KPHJlY3QgeD0iMTMiIHk9IjExIiB3aWR0aD0iMyIgaGVpZ2h0PSIyIiByeD0iMSIgZmlsbD0iIzRFNUM2RSIvPgo8cmVjdCB4PSIxMyIgeT0iMTUiIHdpZHRoPSIzIiBoZWlnaHQ9IjIiIHJ4PSIxIiBmaWxsPSIjNEU1QzZFIi8+Cjwvc3ZnPgo=") no-repeat;
background-position: 0 2px;
content: "";
display: ${props.readOnly ? "none" : "inline-block"};
cursor: grab;
width: 24px;
height: 24px;
position: absolute;
${props.rtl ? "right" : "left"}: -40px;
opacity: 0;
transition: opacity 200ms ease-in-out;
}
ul li[draggable=true]::before,
ol li[draggable=true]::before {
cursor: grabbing;
}
ul > li.counter-2::before,
ol li.counter-2::before {
${props.rtl ? "right" : "left"}: -50px;
}
ul > li.hovering::before,
ol li.hovering::before {
opacity: 0.5;
}
ul li.ProseMirror-selectednode::after,
ol li.ProseMirror-selectednode::after {
display: none;
}
ul.checkbox_list > li::before {
${props.rtl ? "right" : "left"}: 0;
}
@@ -1461,6 +1476,7 @@ del[data-operation-index] {
.placeholder:before,
.block-menu-trigger,
.heading-actions,
.drag-handle,
button.show-source-button,
h1:not(.placeholder):before,
h2:not(.placeholder):before,
@@ -1502,6 +1518,7 @@ const EditorContainer = styled.div<Props>`
${codeMarkCursor}
${codeBlockStyle}
${findAndReplaceStyle}
${dragAndDropStyle}
`;
export default EditorContainer;
+211
View File
@@ -0,0 +1,211 @@
import { NodeSelection, Plugin } from "prosemirror-state";
import { EditorView, __serializeForClipboard } from "prosemirror-view";
import Extension from "../lib/Extension";
export type DragHandleOptions = {
/**
* The width of the drag handle
*/
dragHandleWidth: number;
};
function absoluteRect(node: Element) {
const data = node.getBoundingClientRect();
return {
top: data.top,
left: data.left,
width: data.width,
};
}
function nodeDOMAtCoords(coords: { x: number; y: number }) {
return document
.elementsFromPoint(coords.x, coords.y)
.find(
(elem: Element) =>
elem.parentElement?.matches?.(".ProseMirror") ||
elem.matches(
[
"li",
"p:not(:first-child)",
"pre",
"blockquote",
"h1, h2, h3, h4, h5, h6",
].join(", ")
)
);
}
function nodePosAtDOM(node: Element, view: EditorView) {
const boundingRect = node.getBoundingClientRect();
return view.posAtCoords({
left: boundingRect.left + 1,
top: boundingRect.top + 1,
})?.inside;
}
function DragHandle(options: DragHandleOptions) {
let dragHandleElement: HTMLElement | null = null;
function handleDragStart(event: DragEvent, view: EditorView) {
view.focus();
if (!event.dataTransfer) {
return;
}
const node = nodeDOMAtCoords({
x: event.clientX + 50 + options.dragHandleWidth,
y: event.clientY,
});
if (!(node instanceof Element)) {
return;
}
const nodePos = nodePosAtDOM(node, view);
if (!nodePos || nodePos < 0) {
return;
}
view.dispatch(
view.state.tr.setSelection(NodeSelection.create(view.state.doc, nodePos))
);
const slice = view.state.selection.content();
const { dom, text } = __serializeForClipboard(view, slice);
event.dataTransfer.clearData();
event.dataTransfer.setData("text/html", dom.innerHTML);
event.dataTransfer.setData("text/plain", text);
event.dataTransfer.effectAllowed = "copyMove";
event.dataTransfer.setDragImage(node, 0, 0);
view.dragging = { slice, move: event.ctrlKey };
}
function handleClick(event: MouseEvent, view: EditorView) {
view.focus();
view.dom.classList.remove("dragging");
const node = nodeDOMAtCoords({
x: event.clientX + 50 + options.dragHandleWidth,
y: event.clientY,
});
if (!(node instanceof Element)) {
return;
}
const nodePos = nodePosAtDOM(node, view);
if (!nodePos) {
return;
}
view.dispatch(
view.state.tr.setSelection(NodeSelection.create(view.state.doc, nodePos))
);
}
function hideDragHandle() {
if (dragHandleElement) {
dragHandleElement.classList.add("hidden");
}
}
function showDragHandle() {
if (dragHandleElement) {
dragHandleElement.classList.remove("hidden");
}
}
return new Plugin({
view: (view) => {
dragHandleElement = document.createElement("div");
dragHandleElement.draggable = true;
dragHandleElement.dataset.dragHandle = "";
dragHandleElement.classList.add("drag-handle");
dragHandleElement.addEventListener("dragstart", (e) =>
handleDragStart(e, view)
);
dragHandleElement.addEventListener("click", (e) => handleClick(e, view));
hideDragHandle();
view?.dom?.parentElement?.appendChild(dragHandleElement);
return {
destroy: () => {
dragHandleElement?.remove?.();
dragHandleElement = null;
},
};
},
props: {
handleDOMEvents: {
mousemove: (view, event) => {
if (!view.editable) {
return;
}
const node = nodeDOMAtCoords({
x: event.clientX + 50 + options.dragHandleWidth,
y: event.clientY,
});
if (!(node instanceof Element)) {
hideDragHandle();
return;
}
const style = window.getComputedStyle(node);
const lineHeight = parseInt(style.lineHeight, 10);
const paddingTop = parseInt(style.paddingTop, 10);
const rect = absoluteRect(node);
rect.top += (lineHeight - 24) / 2;
rect.top += paddingTop;
// Li markers
if (node.matches("ul:not([data-type=taskList]) li, ol li")) {
rect.left -= options.dragHandleWidth;
}
rect.width = options.dragHandleWidth;
if (!dragHandleElement) {
return;
}
dragHandleElement.style.left = `${rect.left - rect.width}px`;
dragHandleElement.style.top = `${rect.top}px`;
showDragHandle();
},
keydown: () => {
hideDragHandle();
},
mousewheel: () => {
hideDragHandle();
},
// dragging class is used for CSS
dragstart: (view) => {
view.dom.classList.add("dragging");
},
drop: (view) => {
view.dom.classList.remove("dragging");
},
dragend: (view) => {
view.dom.classList.remove("dragging");
},
},
},
});
}
export default class DragAndDrop extends Extension {
get plugins() {
return [DragHandle({ dragHandleWidth: 24 })];
}
}
+2
View File
@@ -1,6 +1,7 @@
import BlockMenu from "../extensions/BlockMenu";
import ClipboardTextSerializer from "../extensions/ClipboardTextSerializer";
import DateTime from "../extensions/DateTime";
import DragAndDrop from "../extensions/DragAndDrop";
import FindAndReplace from "../extensions/FindAndReplace";
import History from "../extensions/History";
import Keys from "../extensions/Keys";
@@ -111,6 +112,7 @@ export const richExtensions: Nodes = [
MathBlock,
PreventTab,
FindAndReplace,
DragAndDrop,
];
/**