fix: group images a blank line apart into a grid, and keep blank lines on ungroup

Images separated by a blank line load with an empty paragraph between
them, so the Grid button never found two neighboring image blocks and
did nothing in an existing note. Grouping now spans a single empty
paragraph between image blocks and takes the one above the first image,
which would otherwise be saved as a marker line above the grid.

Ungroup gave each image its own paragraph but no blank line, so the
images were saved directly under the preceding paragraph and became part
of it on reload. Top-level grids now ungroup into images a blank line
apart, the reverse of grouping them.

Refs #254
This commit is contained in:
Yuri Karamian committed 2026-10-09 23:06:53 +02:00
1 parent f4c6649c0a
commit 455b059fb5
3 files changed
+57 -7

No files matched your search

+18 -3
View File
@@ -6139,7 +6139,11 @@
state.doc.forEach((node, offset) => blocks.push({ node, offset }));
const { pos } = imageToolbar;
const target = blocks.findIndex((block) => pos >= block.offset && pos < block.offset + block.node.nodeSize);
const range = expandImageRange(blocks.map((block) => ({ isImage: countBlockImages(block.node) > 0, imageCount: countBlockImages(block.node) })), target);
const range = expandImageRange(blocks.map((block) => ({
isImage: countBlockImages(block.node) > 0,
imageCount: countBlockImages(block.node),
isEmpty: block.node.type.name === 'paragraph' && block.node.childCount === 0,
})), target);
if (!range) return;
const images: ProseMirrorNode[] = [];
for (let i = range.start; i <= range.end; i++) {
@@ -6152,14 +6156,25 @@
imageToolbar = null;
}
/** Replace a grid with one image paragraph per image. */
/**
* Replace a grid with one image paragraph per image. At the top level each image gets the empty
* paragraph that stands for the blank line above it, so the images are saved a blank line apart
* instead of being glued to the paragraph above (the reverse of grouping them).
*/
function ungroupImageGridAt(gridPos: number) {
if (!editor) return;
const { state } = editor;
const grid = state.doc.nodeAt(gridPos);
if (!grid || grid.type.name !== 'imageGrid') return;
const resolved = state.doc.resolve(gridPos);
const before = resolved.nodeBefore;
const topLevel = resolved.depth === 0;
const paragraphs: ProseMirrorNode[] = [];
grid.forEach((image) => paragraphs.push(state.schema.nodes.paragraph.create(null, image)));
grid.forEach((image, _offset, index) => {
const blankLineAbove = index > 0 || (!!before && !(before.type.name === 'paragraph' && before.childCount === 0));
if (topLevel && blankLineAbove) paragraphs.push(state.schema.nodes.paragraph.create());
paragraphs.push(state.schema.nodes.paragraph.create(null, image));
});
editor.view.dispatch(state.tr.replaceWith(gridPos, gridPos + grid.nodeSize, paragraphs));
imageToolbar = null;
}
+21 -4
View File
@@ -105,10 +105,15 @@ export function defaultGridCols(imageCount: number): number {
export interface ImageBlockInfo {
isImage: boolean;
imageCount: number;
/** An empty paragraph. One of these before an image is how the editor holds the blank line above it. */
isEmpty?: boolean;
}
/**
* Grow from `target` across adjacent image-only blocks (image paragraphs and existing grids).
* Grow from `target` across neighboring image-only blocks (image paragraphs and existing grids).
* Images a blank line apart load with one empty paragraph between them, so a single empty paragraph
* between two image blocks is part of the range, and so is the one directly above the first image:
* left behind, it would be saved as an empty-paragraph marker above the grid.
* Returns the block range and its image total, or null unless it holds at least two images.
*/
export function expandImageRange(
@@ -116,13 +121,25 @@ export function expandImageRange(
target: number,
): { start: number; end: number; total: number } | null {
if (target < 0 || target >= blocks.length || !blocks[target].isImage) return null;
const isImage = (index: number) => index >= 0 && index < blocks.length && blocks[index].isImage;
const isEmpty = (index: number) => index >= 0 && index < blocks.length && !!blocks[index].isEmpty;
let start = target;
let end = target;
while (start - 1 >= 0 && blocks[start - 1].isImage) start -= 1;
while (end + 1 < blocks.length && blocks[end + 1].isImage) end += 1;
for (;;) {
if (isImage(start - 1)) start -= 1;
else if (isEmpty(start - 1) && isImage(start - 2)) start -= 2;
else break;
}
for (;;) {
if (isImage(end + 1)) end += 1;
else if (isEmpty(end + 1) && isImage(end + 2)) end += 2;
else break;
}
let total = 0;
for (let i = start; i <= end; i++) total += blocks[i].imageCount;
return total < 2 ? null : { start, end, total };
if (total < 2) return null;
if (isEmpty(start - 1)) start -= 1;
return { start, end, total };
}
/**
+18
View File
@@ -225,6 +225,24 @@ test('groups neighboring image blocks only when there are at least two images',
assert.equal(expandImageRange(blocks, 0), null);
});
test('groups images a blank line apart, which load with an empty paragraph between them', () => {
const text = { isImage: false, imageCount: 0 };
const image = { isImage: true, imageCount: 1 };
const empty = { isImage: false, imageCount: 0, isEmpty: true };
// text, blank line, image, blank line, image, blank line, image, text
const blocks = [text, empty, image, empty, image, empty, image, text];
for (const target of [2, 4, 6]) {
// The empty paragraph above the first image goes too, or it would be saved as a marker above the grid.
assert.deepEqual(expandImageRange(blocks, target), { start: 1, end: 6, total: 3 });
}
// Two empty paragraphs are deliberate spacing and are not bridged.
assert.equal(expandImageRange([image, empty, empty, image], 0), null);
// An empty paragraph that does not lead to another image stays out of the range.
assert.deepEqual(expandImageRange([image, image, empty, text], 0), { start: 0, end: 1, total: 2 });
assert.deepEqual(expandImageRange([empty, image, empty, image], 1), { start: 0, end: 3, total: 2 });
assert.deepEqual(expandImageRange([image, empty, image], 2), { start: 0, end: 2, total: 2 });
});
test('drag reorder targets account for the removed item', () => {
assert.equal(reorderTarget(0, 3, 3), 2, 'drop at the end');
assert.equal(reorderTarget(2, 0, 3), 0, 'drop at the start');