@agent-native/toolkit 0.10.8 → 0.10.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -148,6 +148,8 @@ type DragSession = {
148
148
  sourceNodeSize: number;
149
149
  startX: number;
150
150
  startY: number;
151
+ pointerOffsetX: number;
152
+ pointerOffsetY: number;
151
153
  dragging: boolean;
152
154
  preview: HTMLElement | null;
153
155
  dropLine: HTMLElement | null;
@@ -194,6 +196,47 @@ let activeHoverRegistration: DragHandleRegistration | null = null;
194
196
  const clamp = (value: number, min: number, max: number) =>
195
197
  Math.min(Math.max(value, min), max);
196
198
 
199
+ export const dragPreviewTransform = ({
200
+ clientX,
201
+ clientY,
202
+ pointerOffsetX,
203
+ pointerOffsetY,
204
+ }: {
205
+ clientX: number;
206
+ clientY: number;
207
+ pointerOffsetX: number;
208
+ pointerOffsetY: number;
209
+ }) =>
210
+ `translate3d(${clientX - pointerOffsetX}px, ${clientY - pointerOffsetY}px, 0)`;
211
+
212
+ const collapseAndBlurEditorAfterDrop = (
213
+ view: EditorView,
214
+ preferredPos: number,
215
+ ) => {
216
+ const pos = clamp(preferredPos, 0, view.state.doc.content.size);
217
+ const selection = TextSelection.near(view.state.doc.resolve(pos), 1);
218
+ if (!view.state.selection.eq(selection)) {
219
+ view.dispatch(view.state.tr.setSelection(selection));
220
+ }
221
+
222
+ const nativeSelection = window.getSelection();
223
+ const selectionBelongsToEditor = [
224
+ nativeSelection?.anchorNode,
225
+ nativeSelection?.focusNode,
226
+ ].some((node) => node && (node === view.dom || view.dom.contains(node)));
227
+ if (selectionBelongsToEditor) {
228
+ nativeSelection?.removeAllRanges();
229
+ }
230
+ const activeElement = document.activeElement;
231
+ if (
232
+ activeElement instanceof HTMLElement &&
233
+ (activeElement === view.dom || view.dom.contains(activeElement))
234
+ ) {
235
+ activeElement.blur();
236
+ }
237
+ view.dom.blur();
238
+ };
239
+
197
240
  const editorArea = (registration: DragHandleRegistration) => {
198
241
  const rect = registration.view.dom.getBoundingClientRect();
199
242
  return rect.width * rect.height;
@@ -977,7 +1020,12 @@ export const DragHandle = Extension.create<DragHandleOptions>({
977
1020
  ) => {
978
1021
  if (!session.preview) return;
979
1022
 
980
- session.preview.style.transform = `translate3d(${clientX + 12}px, ${clientY + 10}px, 0)`;
1023
+ session.preview.style.transform = dragPreviewTransform({
1024
+ clientX,
1025
+ clientY,
1026
+ pointerOffsetX: session.pointerOffsetX,
1027
+ pointerOffsetY: session.pointerOffsetY,
1028
+ });
981
1029
  };
982
1030
 
983
1031
  const updateDropLine = (
@@ -1174,6 +1222,7 @@ export const DragHandle = Extension.create<DragHandleOptions>({
1174
1222
  ) {
1175
1223
  const sourceNode = session.view.state.doc.nodeAt(sourceStart);
1176
1224
  if (sourceNode) {
1225
+ let dropCommitted = false;
1177
1226
  const sourceRegistration = registrationForView(session.view);
1178
1227
  const transferData = sourceRegistration?.getDragTransferData?.({
1179
1228
  view: session.view,
@@ -1198,7 +1247,7 @@ export const DragHandle = Extension.create<DragHandleOptions>({
1198
1247
  false);
1199
1248
 
1200
1249
  if (handled) {
1201
- target.view.focus();
1250
+ dropCommitted = true;
1202
1251
  } else if (target.view === session.view) {
1203
1252
  const insertPos =
1204
1253
  dropPos > sourceStart ? dropPos - sourceNode.nodeSize : dropPos;
@@ -1206,10 +1255,8 @@ export const DragHandle = Extension.create<DragHandleOptions>({
1206
1255
  .delete(sourceStart, sourceEnd)
1207
1256
  .insert(insertPos, sourceNode);
1208
1257
 
1209
- tr.setSelection(NodeSelection.create(tr.doc, insertPos));
1210
-
1211
1258
  session.view.dispatch(tr.scrollIntoView());
1212
- session.view.focus();
1259
+ dropCommitted = true;
1213
1260
  } else {
1214
1261
  try {
1215
1262
  const targetNode = target.view.state.schema.nodeFromJSON(
@@ -1225,9 +1272,6 @@ export const DragHandle = Extension.create<DragHandleOptions>({
1225
1272
  dropPos,
1226
1273
  targetNode,
1227
1274
  );
1228
- insertTr.setSelection(
1229
- NodeSelection.create(insertTr.doc, dropPos),
1230
- );
1231
1275
  target.view.dispatch(insertTr.scrollIntoView());
1232
1276
 
1233
1277
  const deleteTr = session.view.state.tr.delete(
@@ -1235,12 +1279,19 @@ export const DragHandle = Extension.create<DragHandleOptions>({
1235
1279
  sourceEnd,
1236
1280
  );
1237
1281
  session.view.dispatch(deleteTr);
1238
- target.view.focus();
1282
+ dropCommitted = true;
1239
1283
  } catch {
1240
1284
  // If the target schema cannot accept this node, leave the
1241
1285
  // source document untouched.
1242
1286
  }
1243
1287
  }
1288
+
1289
+ if (dropCommitted) {
1290
+ collapseAndBlurEditorAfterDrop(target.view, dropPos);
1291
+ if (target.view !== session.view) {
1292
+ collapseAndBlurEditorAfterDrop(session.view, sourceStart);
1293
+ }
1294
+ }
1244
1295
  }
1245
1296
  }
1246
1297
  } else if (commit && !session.dragging && event) {
@@ -1356,6 +1407,7 @@ export const DragHandle = Extension.create<DragHandleOptions>({
1356
1407
  if (!sourceNode) return;
1357
1408
 
1358
1409
  e.preventDefault();
1410
+ const sourceRect = currentBlock.getBoundingClientRect();
1359
1411
  dragSession = {
1360
1412
  view: editorView,
1361
1413
  sourceBlock: currentBlock,
@@ -1363,6 +1415,8 @@ export const DragHandle = Extension.create<DragHandleOptions>({
1363
1415
  sourceNodeSize: sourceNode.nodeSize,
1364
1416
  startX: e.clientX,
1365
1417
  startY: e.clientY,
1418
+ pointerOffsetX: e.clientX - sourceRect.left,
1419
+ pointerOffsetY: e.clientY - sourceRect.top,
1366
1420
  dragging: false,
1367
1421
  preview: null,
1368
1422
  dropLine: null,