mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-17 03:03:27 -06:00
refac
This commit is contained in:
@@ -834,6 +834,7 @@
|
||||
});
|
||||
|
||||
import { listDragHandlePlugin } from './RichTextInput/listDragHandlePlugin.js';
|
||||
import BlockHandle from './RichTextInput/BlockHandle.svelte';
|
||||
|
||||
const ListItemDragHandle = Extension.create({
|
||||
name: 'listItemDragHandle',
|
||||
@@ -1549,3 +1550,7 @@
|
||||
dir="auto"
|
||||
class="relative w-full min-w-full {className} {!editable ? 'cursor-not-allowed' : ''}"
|
||||
/>
|
||||
|
||||
{#if dragHandle && editor && editable}
|
||||
<BlockHandle {editor} />
|
||||
{/if}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
<script lang="ts">
|
||||
import { getContext } from 'svelte';
|
||||
import type { Editor } from '@tiptap/core';
|
||||
import type { Transaction } from 'prosemirror-state';
|
||||
import Dropdown from '../Dropdown.svelte';
|
||||
import DropdownMenu from '../DropdownMenu.svelte';
|
||||
import Tooltip from '../Tooltip.svelte';
|
||||
import { listPointerDragKey, moveBlock } from './listDragHandlePlugin';
|
||||
|
||||
export let editor: Editor;
|
||||
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
|
||||
let position = -1;
|
||||
let end = 0;
|
||||
let show = false;
|
||||
let pointerStart: { x: number; y: number } | null = null;
|
||||
let handle: HTMLDivElement;
|
||||
|
||||
function locate(pos: number) {
|
||||
if (!editor.isEditable || editor.isDestroyed) return;
|
||||
const $pos = editor.state.doc.resolve(pos);
|
||||
const start = $pos.depth ? $pos.before(1) : pos;
|
||||
const node = editor.state.doc.nodeAt(start);
|
||||
const dom = editor.view.nodeDOM(start);
|
||||
if (!node || !(dom instanceof HTMLElement)) return;
|
||||
const rect = dom.getBoundingClientRect();
|
||||
const parent = editor.view.dom.parentElement!;
|
||||
const bounds = parent.getBoundingClientRect();
|
||||
handle.style.left = `${rect.left - bounds.left + parent.scrollLeft - 20}px`;
|
||||
handle.style.top = `${rect.top - bounds.top + parent.scrollTop + (rect.height - handle.offsetHeight) / 2}px`;
|
||||
position = start;
|
||||
end = start + node.nodeSize;
|
||||
}
|
||||
|
||||
function mount(node: HTMLDivElement) {
|
||||
const parent = editor.view.dom.parentElement!;
|
||||
const surface = parent.closest('[role="tabpanel"]') ?? parent.parentElement!;
|
||||
parent.appendChild(node);
|
||||
const dragging = () => listPointerDragKey.getState(editor.state)?.dragging;
|
||||
const hover = (event: MouseEvent) => {
|
||||
if (show || dragging() || node.contains(event.target as Node)) return;
|
||||
const bounds = editor.view.dom.getBoundingClientRect();
|
||||
const pos = editor.view.posAtCoords({
|
||||
left: Math.max(bounds.left + 1, Math.min(event.clientX, bounds.right - 1)),
|
||||
top: event.clientY
|
||||
});
|
||||
if (pos) locate(pos.pos);
|
||||
};
|
||||
const leave = () => {
|
||||
if (!editor.isEmpty && !show && !dragging() && !node.contains(document.activeElement))
|
||||
position = -1;
|
||||
};
|
||||
const selection = () => {
|
||||
if (!show && !dragging()) locate(editor.state.selection.from);
|
||||
};
|
||||
const transaction = ({ transaction }: { transaction: Transaction }) => {
|
||||
if (transaction.docChanged || !editor.isEditable) {
|
||||
position = -1;
|
||||
show = false;
|
||||
if (editor.isEmpty) locate(0);
|
||||
}
|
||||
};
|
||||
const resize = new ResizeObserver(() => {
|
||||
if (editor.isEmpty) locate(0);
|
||||
else if (position >= 0) locate(position);
|
||||
});
|
||||
resize.observe(parent);
|
||||
surface.addEventListener('mousemove', hover);
|
||||
surface.addEventListener('mouseleave', leave);
|
||||
editor.on('selectionUpdate', selection);
|
||||
editor.on('transaction', transaction);
|
||||
return {
|
||||
destroy() {
|
||||
resize.disconnect();
|
||||
surface.removeEventListener('mousemove', hover);
|
||||
surface.removeEventListener('mouseleave', leave);
|
||||
editor.off('selectionUpdate', selection);
|
||||
editor.off('transaction', transaction);
|
||||
node.remove();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function move(direction: number) {
|
||||
if (position < 0) return;
|
||||
const $pos = editor.state.doc.resolve(position);
|
||||
const to =
|
||||
direction < 0
|
||||
? position - ($pos.nodeBefore?.nodeSize ?? 0)
|
||||
: end + (editor.state.doc.nodeAt(end)?.nodeSize ?? 0);
|
||||
moveBlock(editor.view, position, to);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div
|
||||
bind:this={handle}
|
||||
use:mount
|
||||
class="absolute z-10 not-prose"
|
||||
style:visibility={position < 0 ? 'hidden' : 'visible'}
|
||||
data-comment-ignore
|
||||
>
|
||||
<Dropdown bind:show align="start" closeOnSelect>
|
||||
<Tooltip content={`${$i18n.t('Drag to move')} · ${$i18n.t('Click for options')}`}>
|
||||
<button
|
||||
type="button"
|
||||
class="pm-list-drag-handle pm-block-drag-handle hover:opacity-100 focus-visible:opacity-100"
|
||||
style="position: static"
|
||||
aria-label={$i18n.t('Block options')}
|
||||
data-block-pos={position}
|
||||
on:mousedown={(event) => {
|
||||
pointerStart = { x: event.clientX, y: event.clientY };
|
||||
const plugin = listPointerDragKey.get(editor.state);
|
||||
if (!show) plugin?.props.handleDOMEvents?.mousedown?.call(plugin, editor.view, event);
|
||||
}}
|
||||
on:click|capture={(event) => {
|
||||
if (
|
||||
event.detail &&
|
||||
pointerStart &&
|
||||
Math.hypot(event.clientX - pointerStart.x, event.clientY - pointerStart.y) > 2
|
||||
) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
pointerStart = null;
|
||||
}}>⋮⋮</button
|
||||
>
|
||||
</Tooltip>
|
||||
<div slot="content">
|
||||
<DropdownMenu className="min-w-36 text-sm">
|
||||
<button disabled={position <= 0} class="disabled:opacity-40" on:click={() => move(-1)}
|
||||
>{$i18n.t('Move up')}</button
|
||||
>
|
||||
<button
|
||||
disabled={end >= editor.state.doc.content.size}
|
||||
class="disabled:opacity-40"
|
||||
on:click={() => move(1)}>{$i18n.t('Move down')}</button
|
||||
>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
</Dropdown>
|
||||
</div>
|
||||
@@ -1,10 +1,35 @@
|
||||
import { Plugin, PluginKey, NodeSelection } from 'prosemirror-state';
|
||||
import { Decoration, DecorationSet } from 'prosemirror-view';
|
||||
import { Fragment } from 'prosemirror-model';
|
||||
import { closeHistory } from 'prosemirror-history';
|
||||
import { yUndoPluginKey } from 'y-prosemirror';
|
||||
|
||||
export const listPointerDragKey = new PluginKey('listPointerDrag');
|
||||
|
||||
/**
|
||||
* @param {Pick<import('prosemirror-view').EditorView, 'state' | 'dispatch' | 'editable' | 'focus'>} view
|
||||
* @param {number} from
|
||||
* @param {number} to
|
||||
*/
|
||||
export function moveBlock(view, from, to) {
|
||||
if (!view.editable) return false;
|
||||
const { state } = view;
|
||||
const node = state.doc.nodeAt(from);
|
||||
if (!node || state.doc.resolve(from).depth || state.doc.resolve(to).depth) return false;
|
||||
if (to >= from && to <= from + node.nodeSize) return false;
|
||||
const tr = state.tr.delete(from, from + node.nodeSize);
|
||||
const target = tr.mapping.map(to);
|
||||
tr.insert(target, node).setSelection(NodeSelection.create(tr.doc, target));
|
||||
const undoManager = yUndoPluginKey.getState(state)?.undoManager;
|
||||
undoManager?.stopCapturing();
|
||||
view.dispatch(closeHistory(tr).scrollIntoView());
|
||||
undoManager?.stopCapturing();
|
||||
view.focus();
|
||||
return true;
|
||||
}
|
||||
|
||||
export function listDragHandlePlugin(options = {}) {
|
||||
let cleanupDrag = () => {};
|
||||
const {
|
||||
itemTypeNames = ['listItem', 'taskItem', 'list_item'],
|
||||
|
||||
@@ -206,10 +231,19 @@ export function listDragHandlePlugin(options = {}) {
|
||||
}
|
||||
|
||||
function infoFromCoords(view, clientX, clientY) {
|
||||
const result = view.posAtCoords({ left: clientX, top: clientY });
|
||||
const block = listPointerDragKey.getState(view.state)?.dragging?.block;
|
||||
const bounds = view.dom.getBoundingClientRect();
|
||||
const result = view.posAtCoords({
|
||||
left: block ? Math.max(bounds.left + 1, Math.min(clientX, bounds.right - 1)) : clientX,
|
||||
top: clientY
|
||||
});
|
||||
if (!result) return null;
|
||||
const $pos = view.state.doc.resolve(result.pos);
|
||||
const li = findListItemAround($pos);
|
||||
const start = $pos.depth ? $pos.before(1) : Math.min(result.pos, view.state.doc.content.size);
|
||||
const node = view.state.doc.nodeAt(start);
|
||||
const li = block
|
||||
? node && { node, start, end: start + node.nodeSize }
|
||||
: findListItemAround($pos);
|
||||
if (!li) return null;
|
||||
|
||||
const dom = /** @type {Element} */ (view.nodeDOM(li.start));
|
||||
@@ -220,8 +254,11 @@ export function listDragHandlePlugin(options = {}) {
|
||||
const xFromLeft = isRTL ? rect.right - clientX : clientX - rect.left;
|
||||
const yInTopHalf = clientY - rect.top < rect.height / 2;
|
||||
|
||||
const mode =
|
||||
xFromLeft <= outdentThresholdX
|
||||
const mode = block
|
||||
? yInTopHalf
|
||||
? 'before'
|
||||
: 'after'
|
||||
: xFromLeft <= outdentThresholdX
|
||||
? 'outdent'
|
||||
: xFromLeft >= intoThresholdX
|
||||
? 'into'
|
||||
@@ -244,6 +281,8 @@ export function listDragHandlePlugin(options = {}) {
|
||||
? buildHandleDecos(tr.doc)
|
||||
: prev.decorations.map(tr.mapping, tr.doc);
|
||||
let next = { ...prev, decorations };
|
||||
// Yjs can replace the entire document. Never drop using positions from before an edit.
|
||||
if (tr.docChanged) next = { ...next, dragging: null, dropTarget: null };
|
||||
const meta = tr.getMeta(listPointerDragKey);
|
||||
if (meta) {
|
||||
if (meta.type === 'set-drag') next = { ...next, dragging: meta.dragging };
|
||||
@@ -254,6 +293,7 @@ export function listDragHandlePlugin(options = {}) {
|
||||
};
|
||||
|
||||
const decorationsProp = (state) => {
|
||||
if (getEditor?.()?.isEditable === false) return null;
|
||||
const ps = listPointerDragKey.getState(state);
|
||||
if (!ps) return null;
|
||||
let deco = ps.decorations;
|
||||
@@ -386,20 +426,30 @@ export function listDragHandlePlugin(options = {}) {
|
||||
return new Plugin({
|
||||
key: listPointerDragKey,
|
||||
state: { init: (_, state) => init(state), apply },
|
||||
view: () => ({
|
||||
update(view) {
|
||||
if (!listPointerDragKey.getState(view.state)?.dragging || !view.editable) cleanupDrag();
|
||||
},
|
||||
destroy() {
|
||||
cleanupDrag();
|
||||
}
|
||||
}),
|
||||
props: {
|
||||
decorations: decorationsProp,
|
||||
handleDOMEvents: {
|
||||
mousedown(view, event) {
|
||||
if (!view.editable || event.button !== 0) return false;
|
||||
const t = /** @type {HTMLElement} */ (event.target);
|
||||
const handle = t.closest?.(`.${classHandle}`);
|
||||
const handle = t.closest?.(`.${classHandle}, .pm-block-drag-handle`);
|
||||
if (!handle) return false;
|
||||
cleanupDrag();
|
||||
event.preventDefault();
|
||||
|
||||
const block = handle.classList.contains('pm-block-drag-handle');
|
||||
const getPos = handle.pmGetPos;
|
||||
if (typeof getPos !== 'function') return true;
|
||||
if (!block && typeof getPos !== 'function') return true;
|
||||
|
||||
const posInside = getPos();
|
||||
const fromStart = posInside - 1;
|
||||
const fromStart = block ? Number(handle.getAttribute('data-block-pos')) : getPos() - 1;
|
||||
|
||||
try {
|
||||
view.dispatch(
|
||||
@@ -409,7 +459,7 @@ export function listDragHandlePlugin(options = {}) {
|
||||
|
||||
const startMouse = { x: event.clientX, y: event.clientY };
|
||||
const ghostEl = ensureGhost(view, fromStart);
|
||||
setDrag(view, { fromStart, startMouse, ghostEl, active: false });
|
||||
setDrag(view, { fromStart, startMouse, ghostEl, active: false, block });
|
||||
|
||||
const onMove = (e) => {
|
||||
const ps = listPointerDragKey.getState(view.state);
|
||||
@@ -443,11 +493,8 @@ export function listDragHandlePlugin(options = {}) {
|
||||
};
|
||||
|
||||
const endDrag = () => {
|
||||
window.removeEventListener('mousemove', onMove, true);
|
||||
window.removeEventListener('mouseup', endDrag, true);
|
||||
|
||||
const ps = listPointerDragKey.getState(view.state);
|
||||
if (ps?.dragging?.ghostEl) ps.dragging.ghostEl.remove();
|
||||
cleanupDrag();
|
||||
|
||||
// Helper: figure out the list item node type name at/around a pos
|
||||
const getListItemTypeNameAt = (doc, pos) => {
|
||||
@@ -465,11 +512,13 @@ export function listDragHandlePlugin(options = {}) {
|
||||
return Array.from(itemTypesSet)[0];
|
||||
};
|
||||
|
||||
if (ps?.dragging && ps?.dropTarget && ps.dragging.active) {
|
||||
if (view.editable && ps?.dragging && ps?.dropTarget && ps.dragging.active) {
|
||||
const { fromStart } = ps.dragging;
|
||||
const { toPos, mode } = ps.dropTarget;
|
||||
|
||||
const res = moveItem(view, fromStart, toPos);
|
||||
const res = block
|
||||
? { ok: moveBlock(view, fromStart, toPos) }
|
||||
: moveItem(view, fromStart, toPos);
|
||||
|
||||
if (res.ok && typeof res.newStart === 'number' && getEditor) {
|
||||
const editor = getEditor();
|
||||
@@ -493,8 +542,24 @@ export function listDragHandlePlugin(options = {}) {
|
||||
clearAll(view);
|
||||
};
|
||||
|
||||
/** @param {KeyboardEvent | FocusEvent} event */
|
||||
const cancelDrag = (event) => {
|
||||
if ('key' in event && event.key !== 'Escape') return;
|
||||
cleanupDrag();
|
||||
clearAll(view);
|
||||
};
|
||||
cleanupDrag = () => {
|
||||
ghostEl.remove();
|
||||
window.removeEventListener('mousemove', onMove, true);
|
||||
window.removeEventListener('mouseup', endDrag, true);
|
||||
window.removeEventListener('keydown', cancelDrag, true);
|
||||
window.removeEventListener('blur', cancelDrag);
|
||||
cleanupDrag = () => {};
|
||||
};
|
||||
window.addEventListener('mousemove', onMove, true);
|
||||
window.addEventListener('mouseup', endDrag, true);
|
||||
window.addEventListener('keydown', cancelDrag, true);
|
||||
window.addEventListener('blur', cancelDrag);
|
||||
return true;
|
||||
},
|
||||
|
||||
|
||||
@@ -1578,7 +1578,7 @@ ${content}
|
||||
bind:this={inputElement}
|
||||
on:editor={(event) => (editor = event.detail)}
|
||||
id={`note-${note.id}-${renderedPage.id}`}
|
||||
className="input-prose-sm px-0.5 flex-1 min-h-[12rem]"
|
||||
className="input-prose-sm ps-4 pe-0.5 pt-2 flex-1 min-h-[12rem]"
|
||||
json={true}
|
||||
bind:value={renderedPage.content.json}
|
||||
html={editorHtml}
|
||||
|
||||
Reference in New Issue
Block a user