Skip to content

Commit bd45a96

Browse files
authored
fix(app/block-editor): operate column block as row
Merge commits below into one: * fix(block-editor): show slash commands * chore: lint * fix(block-editor): drag-menu with columns * fix(block-editor): operation columns * chore: ts * fix(block-editor): columns insert selection
1 parent 86763c7 commit bd45a96

File tree

8 files changed

+45
-12
lines changed

8 files changed

+45
-12
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
public-hoist-pattern[]=*@nextui-org/*
2+
public-hoist-pattern[]=*@tiptap/*

src/shared/components/control/block-editor/DragHandle.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,14 @@ function DragHandle() {
7979

8080
const depth = $pos.depth;
8181
// if depth is 0, we are inserting a new block at the top level
82-
const insertPos = depth === 0 ? $pos.pos + 1 : $pos.after(depth);
82+
let insertPos = depth === 0 ? $pos.pos + 1 : $pos.after(depth);
83+
84+
const node = $pos.node();
85+
if (node.type.name === 'column') {
86+
const columnBlock = $pos.node(1);
87+
if (columnBlock.type.name !== 'columnBlock') return false;
88+
insertPos = $pos.pos + columnBlock.content.size;
89+
}
8390

8491
if (insertPos >= 0 && insertPos <= state.doc.content.size) {
8592
commands.insertContentAt(insertPos, editor.schema.nodes.paragraph.create());
@@ -108,8 +115,17 @@ function DragHandle() {
108115
const depth = $pos.depth;
109116

110117
// if depth is 0, we are deleting the block itself
111-
const start = depth === 0 ? $pos.pos : $pos.before(depth);
112-
const end = depth === 0 ? $pos.pos + 1 : $pos.after(depth);
118+
let start = depth === 0 ? $pos.pos : $pos.before(depth);
119+
let end = depth === 0 ? $pos.pos + 1 : $pos.after(depth);
120+
121+
const node = $pos.node();
122+
if (node.type.name === 'column') {
123+
const columnBlock = $pos.node(1);
124+
125+
if (columnBlock.type.name !== 'columnBlock') return false;
126+
start -= 1;
127+
end = start + columnBlock.nodeSize;
128+
}
113129

114130
// Ensure we're not trying to delete beyond document boundaries
115131
if (start >= 0 && end <= state.doc.content.size && start < end) {

src/shared/components/control/block-editor/helper.js renamed to src/shared/components/control/block-editor/helper.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import { isFunction, isPlainObject } from '../../../utils';
1818

19-
let uploadHandler;
19+
let uploadHandler: any;
2020

21-
function setUploadHandler(handler) {
21+
function setUploadHandler(handler: any) {
2222
if (!isFunction(handler)) {
2323
return;
2424
}
@@ -36,15 +36,15 @@ function getInitialBlockData() {
3636
return { type: 'doc', content: [] };
3737
}
3838

39-
function isBlockDataValid(data) {
39+
function isBlockDataValid(data: any) {
4040
return isPlainObject(data) && data.type === 'doc' && Array.isArray(data.content);
4141
}
4242

43-
function unwrapBlockData({ data }) {
43+
function unwrapBlockData({ data }: { data: any }) {
4444
return data;
4545
}
4646

47-
function wrapBlockData(data) {
47+
function wrapBlockData(data: any) {
4848
return { version: BLOCK_DATA_SPEC_VERSION, data };
4949
}
5050

src/shared/components/control/block-editor/slash.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { TextSelection } from '@tiptap/pm/state';
1718
import {
1819
CheckSquare,
1920
Code,
@@ -47,7 +48,22 @@ function createColumnItems() {
4748
description: `Create ${count} columns block.`,
4849
searchTerms: ['columnBlock', 'column'],
4950
icon: <ItemIcon size={18} />,
50-
command: ({ editor, range }) => editor.chain().focus().deleteRange(range).setColumns(count).run(),
51+
command: ({ editor, range }) =>
52+
editor
53+
.chain()
54+
.focus()
55+
.deleteRange(range)
56+
.setColumns(count)
57+
.command(({ editor, tr, dispatch }) => {
58+
if (!dispatch) return false;
59+
const selection = editor.view.state.selection;
60+
61+
tr.setSelection(TextSelection.create(tr.doc, selection.$from.pos + 1, selection.$to.pos));
62+
dispatch(tr);
63+
64+
return true;
65+
})
66+
.run(),
5167
}));
5268
}
5369

src/shared/components/control/block-editor/upload.js renamed to src/shared/components/control/block-editor/upload.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { toast } from 'react-toastify';
2020
import { isFunction } from '../../../utils';
2121
import { getUploadHandler } from './helper';
2222

23-
function onUpload(file) {
23+
function onUpload(file: File) {
2424
const uploadHandler = getUploadHandler();
2525

2626
if (!isFunction(uploadHandler)) {
@@ -31,14 +31,14 @@ function onUpload(file) {
3131

3232
return new Promise((resolve, reject) => {
3333
req
34-
.then(res => {
34+
.then((res: any) => {
3535
if (res.success) {
3636
resolve(res.data);
3737
} else {
3838
throw new Error('Error uploading image. Please try again.');
3939
}
4040
})
41-
.catch(err => {
41+
.catch((err: any) => {
4242
reject(err);
4343
return err.message;
4444
});

0 commit comments

Comments
 (0)