Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: don't move table toolbars with scroll

thecodrr18a5800652eb
diff --git a/packages/editor/dist/components/popuppresenter/index.js b/packages/editor/dist/components/popuppresenter/index.js
index 48c22b5d4..3c8ce5b4a 100644
--- a/packages/editor/dist/components/popuppresenter/index.js
+++ b/packages/editor/dist/components/popuppresenter/index.js
@@ -122,7 +122,7 @@ export function PopupPresenter(props) {
display: "flex",
width: "fit-content",
height: "fit-content",
- position: "absolute",
+ position: "fixed",
backgroundColor: undefined,
padding: 0,
zIndex: 0,
diff --git a/packages/editor/dist/extensions/attachment/component.js b/packages/editor/dist/extensions/attachment/component.js
index 50179910c..787da5e06 100644
--- a/packages/editor/dist/extensions/attachment/component.js
+++ b/packages/editor/dist/extensions/attachment/component.js
@@ -21,18 +21,11 @@ export function AttachmentComponent(props) {
var editor = props.editor, node = props.node, selected = props.selected;
var _a = node.attrs, hash = _a.hash, filename = _a.filename, size = _a.size, progress = _a.progress;
var elementRef = useRef();
- // const isActive = editor.isActive("attachment", { hash });
- // const [isToolbarVisible, setIsToolbarVisible] = useState<boolean>();
- // useEffect(() => {
- // setIsToolbarVisible(isActive);
- // }, [isActive]);
- console.log(progress);
return (_jsxs(_Fragment, { children: [_jsxs(Box, __assign({ ref: elementRef, as: "span", contentEditable: false, variant: "body", sx: {
display: "inline-flex",
overflow: "hidden",
position: "relative",
justifyContent: "center",
- zIndex: 1,
userSelect: "none",
alignItems: "center",
backgroundColor: "bgSecondary",
diff --git a/packages/editor/dist/extensions/image/component.js b/packages/editor/dist/extensions/image/component.js
index d323279ad..cb0aaae9a 100644
--- a/packages/editor/dist/extensions/image/component.js
+++ b/packages/editor/dist/extensions/image/component.js
@@ -45,32 +45,13 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
-var __read = (this && this.__read) || function (o, n) {
- var m = typeof Symbol === "function" && o[Symbol.iterator];
- if (!m) return o;
- var i = m.call(o), r, ar = [], e;
- try {
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
- }
- catch (error) { e = { error: error }; }
- finally {
- try {
- if (r && !r.done && (m = i["return"])) m.call(i);
- }
- finally { if (e) throw e.error; }
- }
- return ar;
-};
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Box, Flex, Image } from "rebass";
import { Resizable } from "re-resizable";
-import { ToolButton } from "../../toolbar/components/tool-button";
-import { useEffect, useRef, useState } from "react";
-import { ResponsivePresenter } from "../../components/responsive";
-import { ImageProperties } from "../../toolbar/popups/image-properties";
-import { Popup } from "../../toolbar/components/popup";
+import { useEffect, useRef } from "react";
import { Icon } from "../../toolbar/components/icon";
import { Icons } from "../../toolbar/icons";
+import { ImageToolbar } from "../../toolbar/floatingmenus/image";
export function ImageComponent(props) {
var _this = this;
var editor = props.editor, updateAttributes = props.updateAttributes, node = props.node, selected = props.selected;
@@ -116,13 +97,18 @@ export function ImageComponent(props) {
width: ref.clientWidth,
height: ref.clientHeight,
});
- }, lockAspectRatio: true }, { children: [selected && (_jsx(Flex, __assign({ sx: { position: "relative", justifyContent: "end" } }, { children: _jsx(ImageToolbar, { editor: editor, float: float, align: align, height: height || 0, width: width || 0 }) }))), _jsx(Icon, { className: "drag-handle", "data-drag-handle": true, draggable: true, path: Icons.dragHandle, sx: {
+ }, lockAspectRatio: true }, { children: [selected && (_jsx(Flex, __assign({ sx: { position: "relative", justifyContent: "end" } }, { children: _jsx(Flex, __assign({ sx: {
+ position: "absolute",
+ top: -40,
+ mb: 2,
+ alignItems: "end",
+ } }, { children: _jsx(ImageToolbar, { editor: editor }) })) }))), _jsx(Icon, { className: "drag-handle", "data-drag-handle": true, draggable: true, path: Icons.dragHandle, sx: {
cursor: "grab",
position: "absolute",
top: 2,
left: 2,
zIndex: 999,
- opacity: 0,
+ opacity: selected ? 1 : 0,
} }), _jsx(Image, __assign({ "data-drag-image": true, ref: imageRef, alt: alt, title: title, width: "100%", height: "100%", sx: {
border: selected
? "2px solid var(--primary)"
@@ -130,53 +116,6 @@ export function ImageComponent(props) {
borderRadius: "default",
} }, props))] })) })) }));
}
-function ImageToolbar(props) {
- var editor = props.editor, float = props.float, height = props.height, width = props.width;
- var _a = __read(useState(false), 2), isOpen = _a[0], setIsOpen = _a[1];
- var ref = useRef();
- return (_jsxs(Flex, __assign({ ref: ref, sx: {
- flexDirection: "column",
- position: "absolute",
- top: -40,
- mb: 2,
- zIndex: 9999,
- alignItems: "end",
- } }, { children: [_jsxs(Flex, __assign({ sx: {
- bg: "background",
- boxShadow: "menu",
- flexWrap: "nowrap",
- borderRadius: "default",
- mb: 2,
- } }, { children: [_jsxs(Flex, __assign({ className: "toolbar-group", sx: {
- pr: 1,
- mr: 1,
- borderRight: "1px solid var(--border)",
- ":last-of-type": { mr: 0, pr: 0, borderRight: "none" },
- } }, { children: [_jsx(ToolButton, { toggled: false, title: "Align left", id: "alignLeft", icon: "alignLeft", onClick: function () {
- return editor.chain().focus().setImageAlignment({ align: "left" }).run();
- } }), float ? null : (_jsx(ToolButton, { toggled: false, title: "Align center", id: "alignCenter", icon: "alignCenter", onClick: function () {
- return editor
- .chain()
- .focus()
- .setImageAlignment({ align: "center" })
- .run();
- } })), _jsx(ToolButton, { toggled: false, title: "Align right", id: "alignRight", icon: "alignRight", onClick: function () {
- return editor.chain().focus().setImageAlignment({ align: "right" }).run();
- } })] })), _jsx(Flex, __assign({ className: "toolbar-group", sx: {
- pr: 1,
- mr: 1,
- borderRight: "1px solid var(--border)",
- ":last-of-type": { mr: 0, pr: 0, borderRight: "none" },
- } }, { children: _jsx(ToolButton, { toggled: isOpen, title: "Image properties", id: "imageProperties", icon: "more", onClick: function () { return setIsOpen(function (s) { return !s; }); } }) }))] })), _jsx(ResponsivePresenter, __assign({ isOpen: isOpen, desktop: "menu", mobile: "sheet", onClose: function () { return setIsOpen(false); }, blocking: true, focusOnRender: false, position: {
- target: ref.current || "mouse",
- align: "start",
- location: "below",
- yOffset: 10,
- isTargetAbsolute: true,
- } }, { children: _jsx(Popup, __assign({ title: "Image properties", onClose: function () {
- setIsOpen(false);
- } }, { children: _jsx(ImageProperties, __assign({}, props)) })) }))] })));
-}
function dataUriToBlobURL(dataurl) {
return __awaiter(this, void 0, void 0, function () {
var response, blob;
diff --git a/packages/editor/dist/extensions/react/reactportalprovider.d.ts b/packages/editor/dist/extensions/react/reactportalprovider.d.ts
index b3df1ee34..62e1d0f5c 100644
--- a/packages/editor/dist/extensions/react/reactportalprovider.d.ts
+++ b/packages/editor/dist/extensions/react/reactportalprovider.d.ts
@@ -23,7 +23,7 @@ export declare class PortalProvider extends React.Component<BasePortalProviderPr
static displayName: string;
portalProviderAPI: PortalProviderAPI;
constructor(props: BasePortalProviderProps);
- render(): React.ReactChild | JSX.Element | null;
+ render(): JSX.Element | React.ReactChild | null;
componentDidUpdate(): void;
}
export declare class PortalRenderer extends React.Component<{
diff --git a/packages/editor/dist/extensions/table/actions.d.ts b/packages/editor/dist/extensions/table/actions.d.ts
new file mode 100644
index 000000000..c4a829775
--- /dev/null
+++ b/packages/editor/dist/extensions/table/actions.d.ts
@@ -0,0 +1,6 @@
+import { Editor } from "@tiptap/core";
+declare function moveColumnRight(editor: Editor): void;
+declare function moveColumnLeft(editor: Editor): void;
+declare function moveRowDown(editor: Editor): void;
+declare function moveRowUp(editor: Editor): void;
+export { moveColumnLeft, moveColumnRight, moveRowDown, moveRowUp };
diff --git a/packages/editor/dist/extensions/table/actions.js b/packages/editor/dist/extensions/table/actions.js
new file mode 100644
index 000000000..2774ab1fe
--- /dev/null
+++ b/packages/editor/dist/extensions/table/actions.js
@@ -0,0 +1,108 @@
+import { selectedRect } from "prosemirror-tables";
+function moveColumnRight(editor) {
+ var tr = editor.state.tr;
+ var rect = selectedRect(editor.state);
+ if (rect.right === rect.map.width)
+ return;
+ var transaction = moveColumn(tr, rect, rect.left, rect.left + 1);
+ if (!transaction)
+ return;
+ editor.view.dispatch(transaction);
+}
+function moveColumnLeft(editor) {
+ var tr = editor.state.tr;
+ var rect = selectedRect(editor.state);
+ if (rect.left === 0)
+ return;
+ var transaction = moveColumn(tr, rect, rect.left, rect.left - 1);
+ if (!transaction)
+ return;
+ editor.view.dispatch(transaction);
+}
+function moveRowDown(editor) {
+ var tr = editor.state.tr;
+ var rect = selectedRect(editor.state);
+ if (rect.top + 1 === rect.map.height)
+ return;
+ var transaction = moveRow(tr, rect, rect.top, rect.top + 1);
+ if (!transaction)
+ return;
+ editor.view.dispatch(transaction);
+}
+function moveRowUp(editor) {
+ var tr = editor.state.tr;
+ var rect = selectedRect(editor.state);
+ if (rect.top === 0)
+ return;
+ var transaction = moveRow(tr, rect, rect.top, rect.top - 1);
+ if (!transaction)
+ return;
+ editor.view.dispatch(transaction);
+}
+function moveColumn(tr, rect, from, to) {
+ var fromCells = getColumnCells(rect, from);
+ var toCells = getColumnCells(rect, to);
+ return moveCells(tr, rect, fromCells, toCells);
+}
+function getColumnCells(_a, col) {
+ var map = _a.map, table = _a.table;
+ var cells = [];
+ for (var row = 0; row < map.height;) {
+ var index = row * map.width + col;
+ if (index >= map.map.length)
+ break;
+ var pos = map.map[index];
+ var cell = table.nodeAt(pos);
+ if (!cell)
+ continue;
+ cells.push({ cell: cell, pos: pos });
+ row += cell.attrs.rowspan;
+ console.log(cell.textContent);
+ }
+ return cells;
+}
+function moveRow(tr, rect, from, to) {
+ var fromCells = getRowCells(rect, from);
+ var toCells = getRowCells(rect, to);
+ return moveCells(tr, rect, fromCells, toCells);
+}
+function getRowCells(_a, row) {
+ var map = _a.map, table = _a.table;
+ var cells = [];
+ for (var col = 0, index = row * map.width; col < map.width; col++, index++) {
+ if (index >= map.map.length)
+ break;
+ var pos = map.map[index];
+ var cell = table.nodeAt(pos);
+ if (!cell)
+ continue;
+ cells.push({ cell: cell, pos: pos });
+ col += cell.attrs.colspan - 1;
+ }
+ return cells;
+}
+function moveCells(tr, rect, fromCells, toCells) {
+ if (fromCells.length !== toCells.length)
+ return;
+ var mapStart = tr.mapping.maps.length;
+ for (var i = 0; i < toCells.length; ++i) {
+ var fromCell = fromCells[i];
+ var toCell = toCells[i];
+ var fromStart = tr.mapping
+ .slice(mapStart)
+ .map(rect.tableStart + fromCell.pos);
+ var fromEnd = fromStart + fromCell.cell.nodeSize;
+ var fromSlice = tr.doc.slice(fromStart, fromEnd);
+ var toStart = tr.mapping
+ .slice(mapStart)
+ .map(rect.tableStart + toCell.pos);
+ var toEnd = toStart + toCell.cell.nodeSize;
+ var toSlice = tr.doc.slice(toStart, toEnd);
+ tr.replace(toStart, toEnd, fromSlice);
+ fromStart = tr.mapping.slice(mapStart).map(rect.tableStart + fromCell.pos);
+ fromEnd = fromStart + fromCell.cell.nodeSize;
+ tr.replace(fromStart, fromEnd, toSlice);
+ }
+ return tr;
+}
+export { moveColumnLeft, moveColumnRight, moveRowDown, moveRowUp };
diff --git a/packages/editor/dist/extensions/table/component.d.ts b/packages/editor/dist/extensions/table/component.d.ts
new file mode 100644
index 000000000..75bda7e5d
--- /dev/null
+++ b/packages/editor/dist/extensions/table/component.d.ts
@@ -0,0 +1,6 @@
+/// <reference types="react" />
+import { SelectionBasedReactNodeViewProps } from "../react";
+import { Editor } from "@tiptap/core";
+import { NodeView } from "prosemirror-view";
+export declare function TableComponent(props: SelectionBasedReactNodeViewProps): JSX.Element;
+export declare function TableNodeView(editor: Editor): NodeView;
diff --git a/packages/editor/dist/extensions/table/component.js b/packages/editor/dist/extensions/table/component.js
new file mode 100644
index 000000000..cdcdaac5b
--- /dev/null
+++ b/packages/editor/dist/extensions/table/component.js
@@ -0,0 +1,149 @@
+var __extends = (this && this.__extends) || (function () {
+ var extendStatics = function (d, b) {
+ extendStatics = Object.setPrototypeOf ||
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
+ return extendStatics(d, b);
+ };
+ return function (d, b) {
+ if (typeof b !== "function" && b !== null)
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
+ extendStatics(d, b);
+ function __() { this.constructor = d; }
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
+ };
+})();
+var __assign = (this && this.__assign) || function () {
+ __assign = Object.assign || function(t) {
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
+ s = arguments[i];
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
+ t[p] = s[p];
+ }
+ return t;
+ };
+ return __assign.apply(this, arguments);
+};
+import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
+import { Flex } from "rebass";
+import { SelectionBasedNodeView, } from "../react";
+import { useEffect, useRef } from "react";
+import { updateColumnsOnResize } from "prosemirror-tables";
+import { InsertColumnRight, InsertRowBelow, RowProperties, TableProperties, } from "../../toolbar/tools/table";
+import { getToolDefinition } from "../../toolbar/tool-definitions";
+import { getPosition } from "../../utils/position";
+import { findSelectedDOMNode } from "../../toolbar/utils/prosemirror";
+export function TableComponent(props) {
+ var editor = props.editor, node = props.node, forwardRef = props.forwardRef;
+ var colgroupRef = useRef(null);
+ var tableRef = useRef();
+ var selected = editor.isActive("table");
+ useEffect(function () {
+ if (!colgroupRef.current || !tableRef.current)
+ return;
+ updateColumnsOnResize(node, colgroupRef.current, tableRef.current, 50);
+ }, [node]);
+ return (_jsxs(_Fragment, { children: [selected && (_jsxs(_Fragment, { children: [_jsx(TableRowToolbar, { editor: editor, table: tableRef.current }), _jsx(TableColumnToolbar, { editor: editor, table: tableRef.current })] })), _jsx("div", __assign({ className: "tableWrapper" }, { children: _jsx("table", __assign({ ref: function (ref) {
+ forwardRef === null || forwardRef === void 0 ? void 0 : forwardRef(ref);
+ tableRef.current = ref || undefined;
+ } }, { children: _jsx("colgroup", { ref: colgroupRef }) })) }))] }));
+}
+export function TableNodeView(editor) {
+ var TableNode = /** @class */ (function (_super) {
+ __extends(TableNode, _super);
+ function TableNode(node, cellMinWidth) {
+ var _this = _super.call(this, node, editor, function () { return 0; }, // todo
+ editor.storage.portalProviderAPI, editor.storage.eventDispatcher, {
+ component: TableComponent,
+ shouldUpdate: function (prev, next) {
+ return prev.type === next.type;
+ },
+ contentDOMFactory: function () {
+ var dom = document.createElement("tbody");
+ return { dom: dom };
+ },
+ wrapperFactory: function () {
+ var dom = document.createElement("div");
+ dom.style.position = "relative";
+ return dom;
+ },
+ }) || this;
+ _super.prototype.init.call(_this);
+ return _this;
+ }
+ return TableNode;
+ }(SelectionBasedNodeView));
+ return TableNode;
+}
+function TableRowToolbar(props) {
+ var editor = props.editor;
+ var rowToolsRef = useRef(null);
+ useEffect(function () {
+ if (!rowToolsRef.current) {
+ return;
+ }
+ var currentRow = findSelectedDOMNode(editor, ["tableRow"]);
+ if (!currentRow)
+ return;
+ var pos = getPosition(rowToolsRef.current, {
+ location: "left",
+ target: currentRow,
+ align: "start",
+ xOffset: -5,
+ yOffset: -3,
+ });
+ rowToolsRef.current.style.top = "".concat(pos.top, "px");
+ rowToolsRef.current.style.left = "".concat(pos.left, "px");
+ }, [editor.state.selection, rowToolsRef.current]);
+ return (_jsxs(Flex, __assign({ ref: rowToolsRef, sx: {
+ zIndex: 999,
+ top: 0,
+ left: 0,
+ position: "absolute",
+ bg: "background",
+ flexWrap: "nowrap",
+ borderRadius: "default",
+ opacity: 0.3,
+ ":hover": {
+ opacity: 1,
+ },
+ } }, { children: [_jsx(RowProperties, __assign({}, getToolDefinition("rowProperties"), { icon: "more", variant: "small", editor: editor })), _jsx(InsertRowBelow, __assign({}, getToolDefinition("insertRowBelow"), { editor: editor, variant: "small" }))] })));
+}
+function TableColumnToolbar(props) {
+ var editor = props.editor, table = props.table;
+ var columnToolsRef = useRef(null);
+ useEffect(function () {
+ if (!columnToolsRef.current || !table) {
+ return;
+ }
+ var currentCell = findSelectedDOMNode(editor, [
+ "tableCell",
+ "tableHeader",
+ ]);
+ if (!currentCell)
+ return;
+ // tableRef.current
+ var pos = getPosition(columnToolsRef.current, {
+ location: "top",
+ align: "center",
+ target: currentCell,
+ yAnchor: table,
+ yOffset: 2,
+ });
+ columnToolsRef.current.style.left = "".concat(pos.left, "px");
+ columnToolsRef.current.style.top = "".concat(pos.top, "px");
+ }, [editor.state.selection, columnToolsRef.current, table]);
+ return (_jsxs(Flex, __assign({ ref: columnToolsRef, sx: {
+ zIndex: 999,
+ top: 0,
+ left: 0,
+ position: "absolute",
+ bg: "background",
+ flexWrap: "nowrap",
+ borderRadius: "default",
+ opacity: 0.3,
+ ":hover": {
+ opacity: 1,
+ },
+ } }, { children: [_jsx(TableProperties, { editor: editor, title: "tableProperties", icon: "more", variant: "small" }), _jsx(InsertColumnRight, __assign({}, getToolDefinition("insertColumnRight"), { editor: editor, variant: "small", icon: "plus" }))] })));
+}
diff --git a/packages/editor/dist/extensions/table/index.d.ts b/packages/editor/dist/extensions/table/index.d.ts
new file mode 100644
index 000000000..0e948df9e
--- /dev/null
+++ b/packages/editor/dist/extensions/table/index.d.ts
@@ -0,0 +1 @@
+export * from "./table";
diff --git a/packages/editor/dist/extensions/table/index.js b/packages/editor/dist/extensions/table/index.js
new file mode 100644
index 000000000..0e948df9e
--- /dev/null
+++ b/packages/editor/dist/extensions/table/index.js
@@ -0,0 +1 @@
+export * from "./table";
diff --git a/packages/editor/dist/extensions/table/table.d.ts b/packages/editor/dist/extensions/table/table.d.ts
new file mode 100644
index 000000000..14bae9647
--- /dev/null
+++ b/packages/editor/dist/extensions/table/table.d.ts
@@ -0,0 +1,55 @@
+import { Node, ParentConfig } from "@tiptap/core";
+import { NodeView } from "prosemirror-view";
+export interface TableOptions {
+ HTMLAttributes: Record<string, any>;
+ resizable: boolean;
+ handleWidth: number;
+ cellMinWidth: number;
+ View: NodeView;
+ lastColumnResizable: boolean;
+ allowTableNodeSelection: boolean;
+}
+declare module "@tiptap/core" {
+ interface Commands<ReturnType> {
+ table: {
+ insertTable: (options?: {
+ rows?: number;
+ cols?: number;
+ withHeaderRow?: boolean;
+ }) => ReturnType;
+ addColumnBefore: () => ReturnType;
+ addColumnAfter: () => ReturnType;
+ deleteColumn: () => ReturnType;
+ addRowBefore: () => ReturnType;
+ addRowAfter: () => ReturnType;
+ deleteRow: () => ReturnType;
+ deleteTable: () => ReturnType;
+ mergeCells: () => ReturnType;
+ splitCell: () => ReturnType;
+ toggleHeaderColumn: () => ReturnType;
+ toggleHeaderRow: () => ReturnType;
+ toggleHeaderCell: () => ReturnType;
+ mergeOrSplit: () => ReturnType;
+ setCellAttribute: (name: string, value: any) => ReturnType;
+ goToNextCell: () => ReturnType;
+ goToPreviousCell: () => ReturnType;
+ fixTables: () => ReturnType;
+ setCellSelection: (position: {
+ anchorCell: number;
+ headCell?: number;
+ }) => ReturnType;
+ };
+ }
+ interface NodeConfig<Options, Storage> {
+ /**
+ * Table Role
+ */
+ tableRole?: string | ((this: {
+ name: string;
+ options: Options;
+ storage: Storage;
+ parent: ParentConfig<NodeConfig<Options>>["tableRole"];
+ }) => string);
+ }
+}
+export declare const Table: Node<TableOptions, any>;
diff --git a/packages/editor/dist/extensions/table/table.js b/packages/editor/dist/extensions/table/table.js
new file mode 100644
index 000000000..45cffd4d3
--- /dev/null
+++ b/packages/editor/dist/extensions/table/table.js
@@ -0,0 +1,247 @@
+var __read = (this && this.__read) || function (o, n) {
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
+ if (!m) return o;
+ var i = m.call(o), r, ar = [], e;
+ try {
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
+ }
+ catch (error) { e = { error: error }; }
+ finally {
+ try {
+ if (r && !r.done && (m = i["return"])) m.call(i);
+ }
+ finally { if (e) throw e.error; }
+ }
+ return ar;
+};
+var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
+ if (ar || !(i in from)) {
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
+ ar[i] = from[i];
+ }
+ }
+ return to.concat(ar || Array.prototype.slice.call(from));
+};
+import { callOrReturn, getExtensionField, mergeAttributes, Node, } from "@tiptap/core";
+import { TextSelection } from "prosemirror-state";
+import { addColumnAfter, addColumnBefore, addRowAfter, addRowBefore, CellSelection, columnResizing, deleteColumn, deleteRow, deleteTable, fixTables, goToNextCell, mergeCells, setCellAttr, splitCell, tableEditing, toggleHeader, toggleHeaderCell, } from "prosemirror-tables";
+import { TableNodeView } from "./component";
+import { createTable } from "./utils/createTable";
+import { deleteTableWhenAllCellsSelected } from "./utils/deleteTableWhenAllCellsSelected";
+export var Table = Node.create({
+ name: "table",
+ // @ts-ignore
+ addOptions: function () {
+ return {
+ HTMLAttributes: {},
+ resizable: false,
+ handleWidth: 5,
+ cellMinWidth: 25,
+ // TODO: fix
+ View: null,
+ lastColumnResizable: true,
+ allowTableNodeSelection: false,
+ };
+ },
+ content: "tableRow+",
+ tableRole: "table",
+ isolating: true,
+ group: "block",
+ parseHTML: function () {
+ return [{ tag: "table" }];
+ },
+ renderHTML: function (_a) {
+ var HTMLAttributes = _a.HTMLAttributes;
+ return [
+ "table",
+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
+ ["tbody", 0],
+ ];
+ },
+ addCommands: function () {
+ return {
+ insertTable: function (_a) {
+ var _b = _a === void 0 ? {} : _a, _c = _b.rows, rows = _c === void 0 ? 3 : _c, _d = _b.cols, cols = _d === void 0 ? 3 : _d, _e = _b.withHeaderRow, withHeaderRow = _e === void 0 ? true : _e;
+ return function (_a) {
+ var tr = _a.tr, dispatch = _a.dispatch, editor = _a.editor;
+ var node = createTable(editor.schema, rows, cols, withHeaderRow);
+ if (dispatch) {
+ var offset = tr.selection.anchor + 1;
+ tr.replaceSelectionWith(node)
+ .scrollIntoView()
+ .setSelection(TextSelection.near(tr.doc.resolve(offset)));
+ }
+ return true;
+ };
+ },
+ addColumnBefore: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return addColumnBefore(state, dispatch);
+ };
+ },
+ addColumnAfter: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return addColumnAfter(state, dispatch);
+ };
+ },
+ deleteColumn: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return deleteColumn(state, dispatch);
+ };
+ },
+ addRowBefore: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return addRowBefore(state, dispatch);
+ };
+ },
+ addRowAfter: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return addRowAfter(state, dispatch);
+ };
+ },
+ deleteRow: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return deleteRow(state, dispatch);
+ };
+ },
+ deleteTable: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return deleteTable(state, dispatch);
+ };
+ },
+ mergeCells: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return mergeCells(state, dispatch);
+ };
+ },
+ splitCell: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return splitCell(state, dispatch);
+ };
+ },
+ toggleHeaderColumn: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return toggleHeader("column")(state, dispatch);
+ };
+ },
+ toggleHeaderRow: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return toggleHeader("row")(state, dispatch);
+ };
+ },
+ toggleHeaderCell: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return toggleHeaderCell(state, dispatch);
+ };
+ },
+ mergeOrSplit: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ if (mergeCells(state, dispatch)) {
+ return true;
+ }
+ return splitCell(state, dispatch);
+ };
+ },
+ setCellAttribute: function (name, value) {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return setCellAttr(name, value)(state, dispatch);
+ };
+ },
+ goToNextCell: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return goToNextCell(1)(state, dispatch);
+ };
+ },
+ goToPreviousCell: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ return goToNextCell(-1)(state, dispatch);
+ };
+ },
+ fixTables: function () {
+ return function (_a) {
+ var state = _a.state, dispatch = _a.dispatch;
+ if (dispatch) {
+ fixTables(state);
+ }
+ return true;
+ };
+ },
+ setCellSelection: function (position) {
+ return function (_a) {
+ var tr = _a.tr, dispatch = _a.dispatch;
+ if (dispatch) {
+ var selection = CellSelection.create(tr.doc, position.anchorCell, position.headCell);
+ // @ts-ignore
+ tr.setSelection(selection);
+ }
+ return true;
+ };
+ },
+ };
+ },
+ addKeyboardShortcuts: function () {
+ var _this = this;
+ return {
+ Tab: function () {
+ if (_this.editor.commands.goToNextCell()) {
+ return true;
+ }
+ if (!_this.editor.can().addRowAfter()) {
+ return false;
+ }
+ return _this.editor.chain().addRowAfter().goToNextCell().run();
+ },
+ "Shift-Tab": function () { return _this.editor.commands.goToPreviousCell(); },
+ Backspace: deleteTableWhenAllCellsSelected,
+ "Mod-Backspace": deleteTableWhenAllCellsSelected,
+ Delete: deleteTableWhenAllCellsSelected,
+ "Mod-Delete": deleteTableWhenAllCellsSelected,
+ };
+ },
+ addProseMirrorPlugins: function () {
+ var isResizable = this.options.resizable && this.editor.isEditable;
+ return __spreadArray(__spreadArray([], __read((isResizable
+ ? [
+ columnResizing({
+ handleWidth: this.options.handleWidth,
+ cellMinWidth: this.options.cellMinWidth,
+ View: TableNodeView(this.editor),
+ // TODO: PR for @types/prosemirror-tables
+ // @ts-ignore (incorrect type)
+ lastColumnResizable: this.options.lastColumnResizable,
+ }),
+ ]
+ : [])), false), [
+ tableEditing({
+ allowTableNodeSelection: this.options.allowTableNodeSelection,
+ }),
+ ], false);
+ },
+ extendNodeSchema: function (extension) {
+ var context = {
+ name: extension.name,
+ options: extension.options,
+ storage: extension.storage,
+ };
+ return {
+ tableRole: callOrReturn(getExtensionField(extension, "tableRole", context)),
+ };
+ },
+});
diff --git a/packages/editor/dist/extensions/table/utils/createCell.d.ts b/packages/editor/dist/extensions/table/utils/createCell.d.ts
new file mode 100644
index 000000000..8fd76c847
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/createCell.d.ts
@@ -0,0 +1,2 @@
+import { Fragment, Node as ProsemirrorNode, NodeType } from "prosemirror-model";
+export declare function createCell(cellType: NodeType, cellContent?: Fragment | ProsemirrorNode | Array<ProsemirrorNode>): ProsemirrorNode | null | undefined;
diff --git a/packages/editor/dist/extensions/table/utils/createCell.js b/packages/editor/dist/extensions/table/utils/createCell.js
new file mode 100644
index 000000000..c330c52c0
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/createCell.js
@@ -0,0 +1,6 @@
+export function createCell(cellType, cellContent) {
+ if (cellContent) {
+ return cellType.createChecked(null, cellContent);
+ }
+ return cellType.createAndFill();
+}
diff --git a/packages/editor/dist/extensions/table/utils/createTable.d.ts b/packages/editor/dist/extensions/table/utils/createTable.d.ts
new file mode 100644
index 000000000..6b7f74696
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/createTable.d.ts
@@ -0,0 +1,2 @@
+import { Fragment, Node as ProsemirrorNode, Schema } from "prosemirror-model";
+export declare function createTable(schema: Schema, rowsCount: number, colsCount: number, withHeaderRow: boolean, cellContent?: Fragment | ProsemirrorNode | Array<ProsemirrorNode>): ProsemirrorNode;
diff --git a/packages/editor/dist/extensions/table/utils/createTable.js b/packages/editor/dist/extensions/table/utils/createTable.js
new file mode 100644
index 000000000..29d0c4681
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/createTable.js
@@ -0,0 +1,24 @@
+import { createCell } from "./createCell";
+import { getTableNodeTypes } from "./getTableNodeTypes";
+export function createTable(schema, rowsCount, colsCount, withHeaderRow, cellContent) {
+ var types = getTableNodeTypes(schema);
+ var headerCells = [];
+ var cells = [];
+ for (var index = 0; index < colsCount; index += 1) {
+ var cell = createCell(types.cell, cellContent);
+ if (cell) {
+ cells.push(cell);
+ }
+ if (withHeaderRow) {
+ var headerCell = createCell(types.header_cell, cellContent);
+ if (headerCell) {
+ headerCells.push(headerCell);
+ }
+ }
+ }
+ var rows = [];
+ for (var index = 0; index < rowsCount; index += 1) {
+ rows.push(types.row.createChecked(null, withHeaderRow && index === 0 ? headerCells : cells));
+ }
+ return types.table.createChecked(null, rows);
+}
diff --git a/packages/editor/dist/extensions/table/utils/deleteTableWhenAllCellsSelected.d.ts b/packages/editor/dist/extensions/table/utils/deleteTableWhenAllCellsSelected.d.ts
new file mode 100644
index 000000000..db62ac4af
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/deleteTableWhenAllCellsSelected.d.ts
@@ -0,0 +1,2 @@
+import { KeyboardShortcutCommand } from '@tiptap/core';
+export declare const deleteTableWhenAllCellsSelected: KeyboardShortcutCommand;
diff --git a/packages/editor/dist/extensions/table/utils/deleteTableWhenAllCellsSelected.js b/packages/editor/dist/extensions/table/utils/deleteTableWhenAllCellsSelected.js
new file mode 100644
index 000000000..921a5bdc4
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/deleteTableWhenAllCellsSelected.js
@@ -0,0 +1,27 @@
+import { findParentNodeClosestToPos } from '@tiptap/core';
+import { isCellSelection } from './isCellSelection';
+export var deleteTableWhenAllCellsSelected = function (_a) {
+ var editor = _a.editor;
+ var selection = editor.state.selection;
+ if (!isCellSelection(selection)) {
+ return false;
+ }
+ var cellCount = 0;
+ var table = findParentNodeClosestToPos(selection.ranges[0].$from, function (node) {
+ return node.type.name === 'table';
+ });
+ table === null || table === void 0 ? void 0 : table.node.descendants(function (node) {
+ if (node.type.name === 'table') {
+ return false;
+ }
+ if (['tableCell', 'tableHeader'].includes(node.type.name)) {
+ cellCount += 1;
+ }
+ });
+ var allCellsSelected = cellCount === selection.ranges.length;
+ if (!allCellsSelected) {
+ return false;
+ }
+ editor.commands.deleteTable();
+ return true;
+};
diff --git a/packages/editor/dist/extensions/table/utils/getTableNodeTypes.d.ts b/packages/editor/dist/extensions/table/utils/getTableNodeTypes.d.ts
new file mode 100644
index 000000000..c9af32844
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/getTableNodeTypes.d.ts
@@ -0,0 +1,4 @@
+import { NodeType, Schema } from 'prosemirror-model';
+export declare function getTableNodeTypes(schema: Schema): {
+ [key: string]: NodeType;
+};
diff --git a/packages/editor/dist/extensions/table/utils/getTableNodeTypes.js b/packages/editor/dist/extensions/table/utils/getTableNodeTypes.js
new file mode 100644
index 000000000..ac64b7e69
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/getTableNodeTypes.js
@@ -0,0 +1,14 @@
+export function getTableNodeTypes(schema) {
+ if (schema.cached.tableNodeTypes) {
+ return schema.cached.tableNodeTypes;
+ }
+ var roles = {};
+ Object.keys(schema.nodes).forEach(function (type) {
+ var nodeType = schema.nodes[type];
+ if (nodeType.spec.tableRole) {
+ roles[nodeType.spec.tableRole] = nodeType;
+ }
+ });
+ schema.cached.tableNodeTypes = roles;
+ return roles;
+}
diff --git a/packages/editor/dist/extensions/table/utils/isCellSelection.d.ts b/packages/editor/dist/extensions/table/utils/isCellSelection.d.ts
new file mode 100644
index 000000000..83cc26e57
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/isCellSelection.d.ts
@@ -0,0 +1,2 @@
+import { CellSelection } from 'prosemirror-tables';
+export declare function isCellSelection(value: unknown): value is CellSelection;
diff --git a/packages/editor/dist/extensions/table/utils/isCellSelection.js b/packages/editor/dist/extensions/table/utils/isCellSelection.js
new file mode 100644
index 000000000..a609952c8
--- /dev/null
+++ b/packages/editor/dist/extensions/table/utils/isCellSelection.js
@@ -0,0 +1,4 @@
+import { CellSelection } from 'prosemirror-tables';
+export function isCellSelection(value) {
+ return value instanceof CellSelection;
+}
diff --git a/packages/editor/dist/index.js b/packages/editor/dist/index.js
index 14bf83d46..29ec9b7de 100644
--- a/packages/editor/dist/index.js
+++ b/packages/editor/dist/index.js
@@ -39,7 +39,6 @@ import BulletList from "./extensions/bullet-list";
import OrderedList from "./extensions/ordered-list";
import Highlight from "@tiptap/extension-highlight";
import Color from "@tiptap/extension-color";
-import Table from "@tiptap/extension-table";
import TableRow from "@tiptap/extension-table-row";
import TableCell from "./extensions/table-cell";
import TableHeader from "@tiptap/extension-table-header";
@@ -56,6 +55,7 @@ import { Link } from "./extensions/link";
import { EventDispatcher, NodeViewSelectionNotifier, } from "./extensions/react";
import { OutlineList } from "./extensions/outline-list";
import { OutlineListItem } from "./extensions/outline-list-item";
+import { Table } from "./extensions/table";
EditorView.prototype.updateState = function updateState(state) {
if (!this.docView)
return; // This prevents the matchesNode error on hot reloads
@@ -95,6 +95,7 @@ var useTiptap = function (options, deps) {
Table.configure({
resizable: true,
allowTableNodeSelection: true,
+ cellMinWidth: 50,
}),
TableRow,
TableCell,
diff --git a/packages/editor/dist/toolbar/components/toolbargroup.d.ts b/packages/editor/dist/toolbar/components/toolbargroup.d.ts
index 40eb720b3..4e13e964c 100644
--- a/packages/editor/dist/toolbar/components/toolbargroup.d.ts
+++ b/packages/editor/dist/toolbar/components/toolbargroup.d.ts
@@ -1,9 +1,10 @@
/// <reference types="react" />
-import { ToolbarGroupDefinition } from "../types";
+import { ToolbarGroupDefinition, ToolButtonVariant } from "../types";
import { FlexProps } from "rebass";
import { Editor } from "@tiptap/core";
export declare type ToolbarGroupProps = FlexProps & {
tools: ToolbarGroupDefinition;
editor: Editor;
+ variant?: ToolButtonVariant;
};
export declare function ToolbarGroup(props: ToolbarGroupProps): JSX.Element;
diff --git a/packages/editor/dist/toolbar/components/toolbargroup.js b/packages/editor/dist/toolbar/components/toolbargroup.js
index c7c0eff6b..f39dce669 100644
--- a/packages/editor/dist/toolbar/components/toolbargroup.js
+++ b/packages/editor/dist/toolbar/components/toolbargroup.js
@@ -34,8 +34,6 @@ export function ToolbarGroup(props) {
else {
var Component = findTool(toolId);
var toolDefinition = getToolDefinition(toolId);
- if (toolId === "textColor")
- console.log("Rendering", toolId);
return (_jsx(Component, __assign({ editor: editor }, toolDefinition), toolDefinition.title));
}
}) })));
diff --git a/packages/editor/dist/toolbar/components/toolbutton.d.ts b/packages/editor/dist/toolbar/components/toolbutton.d.ts
index 883a3f460..daf2d4209 100644
--- a/packages/editor/dist/toolbar/components/toolbutton.d.ts
+++ b/packages/editor/dist/toolbar/components/toolbutton.d.ts
@@ -2,12 +2,13 @@ import { SchemeColors } from "@notesnook/theme/dist/theme/colorscheme";
import React from "react";
import { ButtonProps } from "rebass";
import { IconNames } from "../icons";
+import { ToolButtonVariant } from "../types";
export declare type ToolButtonProps = ButtonProps & {
icon: IconNames;
iconColor?: keyof SchemeColors;
iconSize?: number;
toggled: boolean;
buttonRef?: React.MutableRefObject<HTMLButtonElement | null | undefined>;
- variant?: "small" | "normal";
+ variant?: ToolButtonVariant;
};
export declare function ToolButton(props: ToolButtonProps): JSX.Element;
diff --git a/packages/editor/dist/toolbar/floatingmenus/image.d.ts b/packages/editor/dist/toolbar/floatingmenus/image.d.ts
new file mode 100644
index 000000000..3b89cb25e
--- /dev/null
+++ b/packages/editor/dist/toolbar/floatingmenus/image.d.ts
@@ -0,0 +1,3 @@
+/// <reference types="react" />
+import { FloatingMenuProps } from "./types";
+export declare function ImageToolbar(props: FloatingMenuProps): JSX.Element;
diff --git a/packages/editor/dist/toolbar/floatingmenus/image.js b/packages/editor/dist/toolbar/floatingmenus/image.js
new file mode 100644
index 000000000..0edf71be2
--- /dev/null
+++ b/packages/editor/dist/toolbar/floatingmenus/image.js
@@ -0,0 +1,15 @@
+import { jsx as _jsx } from "react/jsx-runtime";
+import { ToolbarGroup } from "../components/toolbar-group";
+export function ImageToolbar(props) {
+ var editor = props.editor;
+ return (_jsx(ToolbarGroup, { editor: editor, tools: [
+ "imageAlignLeft",
+ "imageAlignCenter",
+ "imageAlignRight",
+ "imageProperties",
+ ], sx: {
+ boxShadow: "menu",
+ borderRadius: "default",
+ bg: "background",
+ } }));
+}
diff --git a/packages/editor/dist/toolbar/floatingmenus/index.js b/packages/editor/dist/toolbar/floatingmenus/index.js
index ef6fb823c..329ddc6f0 100644
--- a/packages/editor/dist/toolbar/floatingmenus/index.js
+++ b/packages/editor/dist/toolbar/floatingmenus/index.js
@@ -9,10 +9,8 @@ var __assign = (this && this.__assign) || function () {
};
return __assign.apply(this, arguments);
};
-import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
-import { TableRowFloatingMenu, TableColumnFloatingMenu, TableFloatingMenu, } from "./table/table";
+import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { SearchReplaceFloatingMenu } from "./search-replace";
-import { DesktopOnly, MobileOnly } from "../../components/responsive";
export function EditorFloatingMenus(props) {
- return (_jsxs(_Fragment, { children: [_jsxs(DesktopOnly, { children: [_jsx(TableRowFloatingMenu, __assign({}, props)), _jsx(TableColumnFloatingMenu, __assign({}, props))] }), _jsx(MobileOnly, { children: _jsx(TableFloatingMenu, __assign({}, props)) }), _jsx(SearchReplaceFloatingMenu, __assign({}, props))] }));
+ return (_jsx(_Fragment, { children: _jsx(SearchReplaceFloatingMenu, __assign({}, props)) }));
}
diff --git a/packages/editor/dist/toolbar/floatingmenus/table/index.d.ts b/packages/editor/dist/toolbar/floatingmenus/table/index.d.ts
index 1d5440135..68a7f6136 100644
--- a/packages/editor/dist/toolbar/floatingmenus/table/index.d.ts
+++ b/packages/editor/dist/toolbar/floatingmenus/table/index.d.ts
@@ -1 +1 @@
-export { TableColumnFloatingMenu, TableFloatingMenu, TableRowFloatingMenu, } from "./table";
+export { TableColumnFloatingMenu, TableRowFloatingMenu } from "./table";
diff --git a/packages/editor/dist/toolbar/floatingmenus/table/index.js b/packages/editor/dist/toolbar/floatingmenus/table/index.js
index 1d5440135..68a7f6136 100644
--- a/packages/editor/dist/toolbar/floatingmenus/table/index.js
+++ b/packages/editor/dist/toolbar/floatingmenus/table/index.js
@@ -1 +1 @@
-export { TableColumnFloatingMenu, TableFloatingMenu, TableRowFloatingMenu, } from "./table";
+export { TableColumnFloatingMenu, TableRowFloatingMenu } from "./table";
diff --git a/packages/editor/dist/toolbar/floatingmenus/table/table.d.ts b/packages/editor/dist/toolbar/floatingmenus/table/table.d.ts
index 2ac8450ec..ed7a585a1 100644
--- a/packages/editor/dist/toolbar/floatingmenus/table/table.d.ts
+++ b/packages/editor/dist/toolbar/floatingmenus/table/table.d.ts
@@ -2,4 +2,3 @@
import { FloatingMenuProps } from "../types";
export declare function TableRowFloatingMenu(props: FloatingMenuProps): JSX.Element | null;
export declare function TableColumnFloatingMenu(props: FloatingMenuProps): JSX.Element | null;
-export declare function TableFloatingMenu(props: FloatingMenuProps): JSX.Element | null;
diff --git a/packages/editor/dist/toolbar/floatingmenus/table/table.js b/packages/editor/dist/toolbar/floatingmenus/table/table.js
index 583bdac14..e962536b5 100644
--- a/packages/editor/dist/toolbar/floatingmenus/table/table.js
+++ b/packages/editor/dist/toolbar/floatingmenus/table/table.js
@@ -28,27 +28,19 @@ var __read = (this && this.__read) || function (o, n) {
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import { Flex } from "rebass";
-import { useToolbarLocation } from "../../stores/toolbar-store";
import { PopupPresenter } from "../../../components/popup-presenter";
-import { getToolbarElement } from "../../utils/dom";
import { InsertColumnRight, InsertRowBelow, RowProperties, TableProperties, } from "../../tools/table";
+import { findSelectedDOMNode } from "../../utils/prosemirror";
import { getToolDefinition } from "../../tool-definitions";
export function TableRowFloatingMenu(props) {
var editor = props.editor;
- // const theme = editor.storage.theme as Theme;
var _a = __read(useState(null), 2), position = _a[0], setPosition = _a[1];
useEffect(function () {
- var _a;
- if (!editor.isActive("tableCell") &&
- !editor.isActive("tableRow") &&
- !editor.isActive("tableHeader")) {
+ if (!editor.isActive("table")) {
setPosition(null);
return;
}
- var $from = editor.state.selection.$from;
- var selectedNode = $from.node();
- var pos = selectedNode.isTextblock ? $from.before() : $from.pos;
- var currentRow = (_a = editor.view.nodeDOM(pos)) === null || _a === void 0 ? void 0 : _a.closest("tr");
+ var currentRow = findSelectedDOMNode(editor, ["tableRow"]);
if (!currentRow)
return;
setPosition(function (old) {
@@ -119,23 +111,3 @@ export function TableColumnFloatingMenu(props) {
},
} }, { children: [_jsx(TableProperties, { editor: editor, title: "tableProperties", icon: "more", variant: "small" }), _jsx(InsertColumnRight, __assign({}, getToolDefinition("insertColumnRight"), { editor: editor, variant: "small", icon: "plus" }))] })) })));
}
-export function TableFloatingMenu(props) {
- var editor = props.editor;
- var toolbarLocation = useToolbarLocation();
- if (!editor.isActive("table"))
- return null;
- return (_jsx(PopupPresenter, __assign({ isOpen: true, onClose: function () { }, blocking: false, position: {
- isTargetAbsolute: true,
- target: getToolbarElement(),
- location: toolbarLocation === "bottom" ? "top" : "below",
- }, focusOnRender: false }, { children: _jsx(Flex, { sx: {
- bg: "background",
- flexWrap: "nowrap",
- borderRadius: "default",
- // opacity: 0.3,
- // opacity: isMenuOpen || showCellProps ? 1 : 0.3,
- ":hover": {
- opacity: 1,
- },
- } }) })));
-}
diff --git a/packages/editor/dist/toolbar/icons.d.ts b/packages/editor/dist/toolbar/icons.d.ts
index e32b1aa1c..3308578e3 100644
--- a/packages/editor/dist/toolbar/icons.d.ts
+++ b/packages/editor/dist/toolbar/icons.d.ts
@@ -23,6 +23,7 @@ export declare const Icons: {
link: string;
url: string;
image: string;
+ imageSettings: string;
colorClear: string;
check: string;
checkbox: string;
diff --git a/packages/editor/dist/toolbar/icons.js b/packages/editor/dist/toolbar/icons.js
index ec4094a23..59284b2b2 100644
--- a/packages/editor/dist/toolbar/icons.js
+++ b/packages/editor/dist/toolbar/icons.js
@@ -1,4 +1,4 @@
-import { mdiAttachment, mdiBorderHorizontal, mdiCheck, mdiChevronDown, mdiCodeBraces, mdiCodeTags, mdiDotsVertical, mdiFormatAlignCenter, mdiFormatAlignJustify, mdiFormatAlignLeft, mdiFormatAlignRight, mdiFormatBold, mdiFormatClear, mdiFormatColorHighlight, mdiFormatColorText, mdiFormatItalic, mdiFormatListBulleted, mdiFormatListNumbered, mdiFormatQuoteClose, mdiFormatStrikethrough, mdiFormatSubscript, mdiFormatSuperscript, mdiFormatTextdirectionLToR, mdiFormatTextdirectionRToL, mdiFormatUnderline, mdiImage, mdiInvertColorsOff, mdiLinkPlus, mdiLoading, mdiTable, mdiTableBorder, mdiTableRowPlusAfter, mdiTableRowPlusBefore, mdiTableRowRemove, mdiTableColumnPlusAfter, mdiTableColumnPlusBefore, mdiTableColumnRemove, mdiUploadOutline, mdiPlus, mdiFormatColorFill, mdiClose, mdiSortDescending, mdiArrowExpandRight, mdiArrowExpandLeft, mdiArrowExpandDown, mdiArrowExpandUp, mdiTableMergeCells, mdiTableSplitCell, mdiDeleteOutline, mdiDownloadOutline, mdiFormatListCheckbox, mdiDrag, mdiCheckboxMarkedOutline, mdiChevronUp, mdiArrowUp, mdiArrowDown, mdiRegex, mdiFormatLetterCase, mdiFormatLetterMatches, mdiMoviePlusOutline, mdiLink, mdiChevronRight, mdiTableColumnWidth, mdiTableRowHeight, mdiMinus, mdiPaletteOutline, mdiCircle, mdiChevronLeft, mdiTableCog, mdiTableOff, mdiRectangle, } from "@mdi/js";
+import { mdiAttachment, mdiBorderHorizontal, mdiCheck, mdiChevronDown, mdiCodeBraces, mdiCodeTags, mdiDotsVertical, mdiFormatAlignCenter, mdiFormatAlignJustify, mdiFormatAlignLeft, mdiFormatAlignRight, mdiFormatBold, mdiFormatClear, mdiFormatColorHighlight, mdiFormatColorText, mdiFormatItalic, mdiFormatListBulleted, mdiFormatListNumbered, mdiFormatQuoteClose, mdiFormatStrikethrough, mdiFormatSubscript, mdiFormatSuperscript, mdiFormatTextdirectionLToR, mdiFormatTextdirectionRToL, mdiFormatUnderline, mdiImage, mdiInvertColorsOff, mdiLinkPlus, mdiLoading, mdiTable, mdiTableBorder, mdiTableRowPlusAfter, mdiTableRowPlusBefore, mdiTableRowRemove, mdiTableColumnPlusAfter, mdiTableColumnPlusBefore, mdiTableColumnRemove, mdiUploadOutline, mdiPlus, mdiFormatColorFill, mdiClose, mdiSortDescending, mdiArrowExpandRight, mdiArrowExpandLeft, mdiArrowExpandDown, mdiArrowExpandUp, mdiTableMergeCells, mdiTableSplitCell, mdiDeleteOutline, mdiDownloadOutline, mdiFormatListCheckbox, mdiDrag, mdiCheckboxMarkedOutline, mdiChevronUp, mdiArrowUp, mdiArrowDown, mdiRegex, mdiFormatLetterCase, mdiFormatLetterMatches, mdiMoviePlusOutline, mdiLink, mdiChevronRight, mdiTableColumnWidth, mdiTableRowHeight, mdiMinus, mdiPaletteOutline, mdiCircle, mdiChevronLeft, mdiTableCog, mdiTableOff, mdiRectangle, mdiImageEditOutline, } from "@mdi/js";
export var Icons = {
bold: mdiFormatBold,
italic: mdiFormatItalic,
@@ -24,6 +24,7 @@ export var Icons = {
link: mdiLinkPlus,
url: mdiLink,
image: mdiImage,
+ imageSettings: mdiImageEditOutline,
colorClear: mdiInvertColorsOff,
check: mdiCheck,
checkbox: mdiCheckboxMarkedOutline,
diff --git a/packages/editor/dist/toolbar/popups/imageproperties.js b/packages/editor/dist/toolbar/popups/imageproperties.js
index aeeebc4a8..d25af3809 100644
--- a/packages/editor/dist/toolbar/popups/imageproperties.js
+++ b/packages/editor/dist/toolbar/popups/imageproperties.js
@@ -13,7 +13,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Flex, Text } from "rebass";
import { useCallback } from "react";
import { Toggle } from "../../components/toggle";
-import { Input } from "@rebass/forms";
+import { InlineInput } from "../../components/inline-input";
export function ImageProperties(props) {
var height = props.height, width = props.width, float = props.float, editor = props.editor;
var onSizeChange = useCallback(function (newWidth, newHeight) {
@@ -33,14 +33,12 @@ export function ImageProperties(props) {
};
editor.chain().setImageSize(size).run();
}, [width, height]);
- return (_jsxs(Flex, __assign({ sx: { width: 200, flexDirection: "column", p: 1 } }, { children: [_jsxs(Flex, __assign({ sx: { justifyContent: "space-between", alignItems: "center" } }, { children: [_jsx(Text, __assign({ variant: "body" }, { children: "Floating?" })), _jsx(Toggle, { checked: float, onClick: function () {
+ return (_jsxs(Flex, __assign({ sx: { width: ["auto", 300], flexDirection: "column", p: 1 } }, { children: [_jsxs(Flex, __assign({ sx: { justifyContent: "space-between", alignItems: "center" } }, { children: [_jsx(Text, __assign({ variant: "body" }, { children: "Floating?" })), _jsx(Toggle, { checked: float, onClick: function () {
return editor
.chain()
.setImageAlignment({ float: !float, align: "left" })
.run();
- } })] })), _jsxs(Flex, __assign({ sx: { alignItems: "center", mt: 2 } }, { children: [_jsx(Input, { type: "number", placeholder: "Width", value: width, sx: {
- mr: 2,
- p: 1,
- fontSize: "body",
- }, onChange: function (e) { return onSizeChange(e.target.valueAsNumber); } }), _jsx(Input, { type: "number", placeholder: "Height", value: height, sx: { p: 1, fontSize: "body" }, onChange: function (e) { return onSizeChange(undefined, e.target.valueAsNumber); } })] }))] })));
+ } })] })), _jsxs(Flex, __assign({ sx: { alignItems: "center", mt: 2 } }, { children: [_jsx(InlineInput, { label: "width", type: "number", value: width, containerProps: {
+ sx: { mr: 1 },
+ }, onChange: function (e) { return onSizeChange(e.target.valueAsNumber); } }), _jsx(InlineInput, { label: "height", type: "number", value: height, onChange: function (e) { return onSizeChange(undefined, e.target.valueAsNumber); } })] }))] })));
}
diff --git a/packages/editor/dist/toolbar/toolbar.js b/packages/editor/dist/toolbar/toolbar.js
index 5143b98ba..0dfeab71b 100644
--- a/packages/editor/dist/toolbar/toolbar.js
+++ b/packages/editor/dist/toolbar/toolbar.js
@@ -26,7 +26,7 @@ export function Toolbar(props) {
setToolbarLocation(location);
}, [isMobile, location]);
var tools = [
- ["insertBlock", "tableSettings"],
+ ["insertBlock", "tableSettings", "imageSettings"],
[
"bold",
"italic",
diff --git a/packages/editor/dist/toolbar/tooldefinitions.js b/packages/editor/dist/toolbar/tooldefinitions.js
index bf4aefbfc..f77d66f67 100644
--- a/packages/editor/dist/toolbar/tooldefinitions.js
+++ b/packages/editor/dist/toolbar/tooldefinitions.js
@@ -196,6 +196,31 @@ var tools = {
title: "Cell border width",
conditional: true,
},
+ imageSettings: {
+ icon: "imageSettings",
+ title: "Image settings",
+ conditional: true,
+ },
+ imageAlignCenter: {
+ icon: "alignCenter",
+ title: "Align center",
+ conditional: true,
+ },
+ imageAlignLeft: {
+ icon: "alignLeft",
+ title: "Align left",
+ conditional: true,
+ },
+ imageAlignRight: {
+ icon: "alignRight",
+ title: "Align right",
+ conditional: true,
+ },
+ imageProperties: {
+ icon: "more",
+ title: "Image properties",
+ conditional: true,
+ },
};
export function getToolDefinition(id) {
return tools[id];
diff --git a/packages/editor/dist/toolbar/tools/image.d.ts b/packages/editor/dist/toolbar/tools/image.d.ts
new file mode 100644
index 000000000..d192850d6
--- /dev/null
+++ b/packages/editor/dist/toolbar/tools/image.d.ts
@@ -0,0 +1,7 @@
+/// <reference types="react" />
+import { ToolProps } from "../types";
+export declare function ImageSettings(props: ToolProps): JSX.Element | null;
+export declare function ImageAlignLeft(props: ToolProps): JSX.Element;
+export declare function ImageAlignRight(props: ToolProps): JSX.Element;
+export declare function ImageAlignCenter(props: ToolProps): JSX.Element;
+export declare function ImageProperties(props: ToolProps): JSX.Element;
diff --git a/packages/editor/dist/toolbar/tools/image.js b/packages/editor/dist/toolbar/tools/image.js
new file mode 100644
index 000000000..b9e2e6b89
--- /dev/null
+++ b/packages/editor/dist/toolbar/tools/image.js
@@ -0,0 +1,87 @@
+var __assign = (this && this.__assign) || function () {
+ __assign = Object.assign || function(t) {
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
+ s = arguments[i];
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
+ t[p] = s[p];
+ }
+ return t;
+ };
+ return __assign.apply(this, arguments);
+};
+var __read = (this && this.__read) || function (o, n) {
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
+ if (!m) return o;
+ var i = m.call(o), r, ar = [], e;
+ try {
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
+ }
+ catch (error) { e = { error: error }; }
+ finally {
+ try {
+ if (r && !r.done && (m = i["return"])) m.call(i);
+ }
+ finally { if (e) throw e.error; }
+ }
+ return ar;
+};
+import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
+import { ToolButton } from "../components/tool-button";
+import { useMemo, useRef, useState } from "react";
+import { Popup } from "../components/popup";
+import { ResponsivePresenter } from "../../components/responsive";
+import { MoreTools } from "../components/more-tools";
+import { useToolbarLocation } from "../stores/toolbar-store";
+import { ImageProperties as ImagePropertiesPopup } from "../popups/image-properties";
+import { findSelectedNode } from "../utils/prosemirror";
+export function ImageSettings(props) {
+ var editor = props.editor;
+ var isBottom = useToolbarLocation() === "bottom";
+ if (!editor.isActive("image") || !isBottom)
+ return null;
+ var image = useMemo(function () { return findSelectedNode(editor, "image"); }, []);
+ var float = ((image === null || image === void 0 ? void 0 : image.attrs) || {}).float;
+ return (_jsx(MoreTools, __assign({}, props, { popupId: "imageSettings", tools: float
+ ? ["imageAlignLeft", "imageAlignRight", "imageProperties"]
+ : [
+ "imageAlignLeft",
+ "imageAlignCenter",
+ "imageAlignRight",
+ "imageProperties",
+ ] })));
+}
+export function ImageAlignLeft(props) {
+ var editor = props.editor;
+ return (_jsx(ToolButton, __assign({}, props, { toggled: false, onClick: function () {
+ return editor.chain().focus().setImageAlignment({ align: "left" }).run();
+ } })));
+}
+export function ImageAlignRight(props) {
+ var editor = props.editor;
+ return (_jsx(ToolButton, __assign({}, props, { toggled: false, onClick: function () {
+ return editor.chain().focus().setImageAlignment({ align: "right" }).run();
+ } })));
+}
+export function ImageAlignCenter(props) {
+ var editor = props.editor;
+ return (_jsx(ToolButton, __assign({}, props, { toggled: false, onClick: function () {
+ return editor.chain().focus().setImageAlignment({ align: "center" }).run();
+ } })));
+}
+export function ImageProperties(props) {
+ var editor = props.editor;
+ var _a = __read(useState(false), 2), isOpen = _a[0], setIsOpen = _a[1];
+ var buttonRef = useRef();
+ var image = useMemo(function () { return findSelectedNode(editor, "image"); }, []);
+ var _b = ((image === null || image === void 0 ? void 0 : image.attrs) ||
+ {}), float = _b.float, align = _b.align, width = _b.width, height = _b.height;
+ return (_jsxs(_Fragment, { children: [_jsx(ToolButton, __assign({ buttonRef: buttonRef, toggled: isOpen }, props, { onClick: function () { return setIsOpen(function (s) { return !s; }); } })), _jsx(ResponsivePresenter, __assign({ isOpen: isOpen, desktop: "menu", mobile: "sheet", onClose: function () { return setIsOpen(false); }, blocking: true, focusOnRender: false, position: {
+ target: buttonRef.current || "mouse",
+ align: "start",
+ location: "below",
+ yOffset: 10,
+ isTargetAbsolute: true,
+ } }, { children: _jsx(Popup, __assign({ title: "Image properties", onClose: function () {
+ setIsOpen(false);
+ } }, { children: _jsx(ImagePropertiesPopup, { editor: editor, height: height, width: width, align: align, float: float }) })) }))] }));
+}
diff --git a/packages/editor/dist/toolbar/tools/index.d.ts b/packages/editor/dist/toolbar/tools/index.d.ts
index f51c43898..50c623077 100644
--- a/packages/editor/dist/toolbar/tools/index.d.ts
+++ b/packages/editor/dist/toolbar/tools/index.d.ts
@@ -9,6 +9,7 @@ import { NumberedList, BulletList } from "./lists";
import { LeftToRight, RightToLeft } from "./text-direction";
import { Highlight, TextColor } from "./colors";
import { TableSettings, ColumnProperties, RowProperties, CellProperties, CellBackgroundColor, CellBorderColor, CellTextColor, CellBorderWidth } from "./table";
+import { ImageSettings, ImageAlignCenter, ImageAlignLeft, ImageAlignRight, ImageProperties } from "./image";
export declare type ToolId = keyof typeof tools;
declare const tools: {
bold: typeof Bold;
@@ -34,6 +35,11 @@ declare const tools: {
rtl: typeof RightToLeft;
textColor: typeof TextColor;
highlight: typeof Highlight;
+ imageSettings: typeof ImageSettings;
+ imageAlignCenter: typeof ImageAlignCenter;
+ imageAlignLeft: typeof ImageAlignLeft;
+ imageAlignRight: typeof ImageAlignRight;
+ imageProperties: typeof ImageProperties;
tableSettings: typeof TableSettings;
columnProperties: typeof ColumnProperties;
rowProperties: typeof RowProperties;
diff --git a/packages/editor/dist/toolbar/tools/index.js b/packages/editor/dist/toolbar/tools/index.js
index 77a2f2152..44aa058c6 100644
--- a/packages/editor/dist/toolbar/tools/index.js
+++ b/packages/editor/dist/toolbar/tools/index.js
@@ -7,6 +7,7 @@ import { NumberedList, BulletList } from "./lists";
import { LeftToRight, RightToLeft } from "./text-direction";
import { Highlight, TextColor } from "./colors";
import { TableSettings, ColumnProperties, RowProperties, CellProperties, InsertColumnLeft, InsertColumnRight, MoveColumnLeft, MoveColumnRight, DeleteColumn, SplitCells, MergeCells, InsertRowAbove, InsertRowBelow, MoveRowUp, MoveRowDown, DeleteRow, DeleteTable, CellBackgroundColor, CellBorderColor, CellTextColor, CellBorderWidth, } from "./table";
+import { ImageSettings, ImageAlignCenter, ImageAlignLeft, ImageAlignRight, ImageProperties, } from "./image";
var tools = {
bold: Bold,
italic: Italic,
@@ -31,6 +32,11 @@ var tools = {
rtl: RightToLeft,
textColor: TextColor,
highlight: Highlight,
+ imageSettings: ImageSettings,
+ imageAlignCenter: ImageAlignCenter,
+ imageAlignLeft: ImageAlignLeft,
+ imageAlignRight: ImageAlignRight,
+ imageProperties: ImageProperties,
tableSettings: TableSettings,
columnProperties: ColumnProperties,
rowProperties: RowProperties,
diff --git a/packages/editor/dist/toolbar/tools/table.js b/packages/editor/dist/toolbar/tools/table.js
index 680788fe9..6cd9c2693 100644
--- a/packages/editor/dist/toolbar/tools/table.js
+++ b/packages/editor/dist/toolbar/tools/table.js
@@ -27,9 +27,10 @@ var __read = (this && this.__read) || function (o, n) {
};
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { ToolButton } from "../components/tool-button";
-import { useCallback, useMemo, useState } from "react";
+import { useCallback, useMemo, useRef, useState } from "react";
import { Flex, Text } from "rebass";
import { ResponsivePresenter } from "../../components/responsive";
+import { moveColumnLeft as moveColumnLeftAction, moveColumnRight as moveColumnRightAction, moveRowDown as moveRowDownAction, moveRowUp as moveRowUpAction, } from "../../extensions/table/actions";
import { MoreTools } from "../components/more-tools";
import { menuButtonToTool } from "./utils";
import { getToolDefinition } from "../tool-definitions";
@@ -58,6 +59,7 @@ export function TableSettings(props) {
}
export function RowProperties(props) {
var editor = props.editor;
+ var buttonRef = useRef();
var _a = __read(useState(false), 2), isMenuOpen = _a[0], setIsMenuOpen = _a[1];
var items = useMemo(function () { return [
insertRowAbove(editor),
@@ -66,10 +68,16 @@ export function RowProperties(props) {
moveRowDown(editor),
deleteRow(editor),
]; }, []);
- return (_jsxs(_Fragment, { children: [_jsx(ToolButton, __assign({}, props, { toggled: isMenuOpen, onClick: function () { return setIsMenuOpen(true); } })), _jsx(ResponsivePresenter, { title: "Row properties", mobile: "sheet", desktop: "menu", isOpen: isMenuOpen, onClose: function () { return setIsMenuOpen(false); }, position: {}, items: items })] }));
+ return (_jsxs(_Fragment, { children: [_jsx(ToolButton, __assign({}, props, { buttonRef: buttonRef, toggled: isMenuOpen, onClick: function () { return setIsMenuOpen(true); } })), _jsx(ResponsivePresenter, { title: "Row properties", mobile: "sheet", desktop: "menu", isOpen: isMenuOpen, onClose: function () { return setIsMenuOpen(false); }, position: {
+ target: buttonRef.current,
+ isTargetAbsolute: true,
+ location: "below",
+ yOffset: 5,
+ }, items: items })] }));
}
export function ColumnProperties(props) {
var editor = props.editor;
+ var buttonRef = useRef();
var _a = __read(useState(false), 2), isMenuOpen = _a[0], setIsMenuOpen = _a[1];
var items = useMemo(function () { return [
insertColumnLeft(editor),
@@ -78,10 +86,16 @@ export function ColumnProperties(props) {
moveColumnRight(editor),
deleteColumn(editor),
]; }, []);
- return (_jsxs(_Fragment, { children: [_jsx(ToolButton, __assign({}, props, { toggled: isMenuOpen, onClick: function () { return setIsMenuOpen(true); } })), _jsx(ResponsivePresenter, { title: "Column properties", mobile: "sheet", desktop: "menu", isOpen: isMenuOpen, onClose: function () { return setIsMenuOpen(false); }, position: {}, items: items })] }));
+ return (_jsxs(_Fragment, { children: [_jsx(ToolButton, __assign({}, props, { buttonRef: buttonRef, toggled: isMenuOpen, onClick: function () { return setIsMenuOpen(true); } })), _jsx(ResponsivePresenter, { title: "Column properties", mobile: "sheet", desktop: "menu", isOpen: isMenuOpen, onClose: function () { return setIsMenuOpen(false); }, position: {
+ target: buttonRef.current,
+ isTargetAbsolute: true,
+ location: "below",
+ yOffset: 5,
+ }, items: items })] }));
}
export function TableProperties(props) {
var editor = props.editor;
+ var buttonRef = useRef();
var _a = __read(useState(false), 2), isMenuOpen = _a[0], setIsMenuOpen = _a[1];
var items = useMemo(function () { return [
insertColumnLeft(editor),
@@ -96,7 +110,12 @@ export function TableProperties(props) {
{ type: "separator", key: "tableSeperator" },
deleteTable(editor),
]; }, []);
- return (_jsxs(_Fragment, { children: [_jsx(ToolButton, __assign({}, props, { toggled: isMenuOpen, onClick: function () { return setIsMenuOpen(true); } })), _jsx(ResponsivePresenter, { title: "Table properties", mobile: "sheet", desktop: "menu", isOpen: isMenuOpen, onClose: function () { return setIsMenuOpen(false); }, position: {}, items: items })] }));
+ return (_jsxs(_Fragment, { children: [_jsx(ToolButton, __assign({}, props, { buttonRef: buttonRef, toggled: isMenuOpen, onClick: function () { return setIsMenuOpen(true); } })), _jsx(ResponsivePresenter, { title: "Table properties", mobile: "sheet", desktop: "menu", isOpen: isMenuOpen, onClose: function () { return setIsMenuOpen(false); }, position: {
+ target: buttonRef.current,
+ isTargetAbsolute: true,
+ location: "below",
+ yOffset: 5,
+ }, items: items })] }));
}
export function CellProperties(props) {
return (_jsx(_Fragment, { children: _jsx(MoreTools, __assign({}, props, { popupId: "cellProperties", tools: [
@@ -147,15 +166,15 @@ export function CellBorderWidth(props) {
}
var insertColumnLeft = function (editor) { return (__assign(__assign({}, getToolDefinition("insertColumnLeft")), { key: "addColumnLeft", type: "button", onClick: function () { return editor.chain().focus().addColumnBefore().run(); } })); };
var insertColumnRight = function (editor) { return (__assign(__assign({}, getToolDefinition("insertColumnRight")), { key: "addColumnRight", type: "button", title: "Add column right", onClick: function () { return editor.chain().focus().addColumnAfter().run(); }, icon: "insertColumnRight" })); };
-var moveColumnLeft = function (editor) { return (__assign(__assign({}, getToolDefinition("moveColumnLeft")), { key: "moveColumnLeft", type: "button", onClick: function () { return moveColumnLeft(editor); } })); };
-var moveColumnRight = function (editor) { return (__assign(__assign({}, getToolDefinition("moveColumnRight")), { key: "moveColumnRight", type: "button", onClick: function () { return moveColumnRight(editor); } })); };
+var moveColumnLeft = function (editor) { return (__assign(__assign({}, getToolDefinition("moveColumnLeft")), { key: "moveColumnLeft", type: "button", onClick: function () { return moveColumnLeftAction(editor); } })); };
+var moveColumnRight = function (editor) { return (__assign(__assign({}, getToolDefinition("moveColumnRight")), { key: "moveColumnRight", type: "button", onClick: function () { return moveColumnRightAction(editor); } })); };
var deleteColumn = function (editor) { return (__assign(__assign({}, getToolDefinition("deleteColumn")), { key: "deleteColumn", type: "button", onClick: function () { return editor.chain().focus().deleteColumn().run(); } })); };
var splitCells = function (editor) { return (__assign(__assign({}, getToolDefinition("splitCells")), { key: "splitCells", type: "button", onClick: function () { return editor.chain().focus().splitCell().run(); } })); };
var mergeCells = function (editor) { return (__assign(__assign({}, getToolDefinition("mergeCells")), { key: "mergeCells", type: "button", onClick: function () { return editor.chain().focus().mergeCells().run(); } })); };
var insertRowAbove = function (editor) { return (__assign(__assign({}, getToolDefinition("insertRowAbove")), { key: "insertRowAbove", type: "button", onClick: function () { return editor.chain().focus().addRowBefore().run(); } })); };
var insertRowBelow = function (editor) { return (__assign(__assign({}, getToolDefinition("insertRowBelow")), { key: "insertRowBelow", type: "button", onClick: function () { return editor.chain().focus().addRowAfter().run(); } })); };
-var moveRowUp = function (editor) { return (__assign(__assign({}, getToolDefinition("moveRowUp")), { key: "moveRowUp", type: "button", onClick: function () { } })); };
-var moveRowDown = function (editor) { return (__assign(__assign({}, getToolDefinition("moveRowDown")), { key: "moveRowDown", type: "button", onClick: function () { } })); };
+var moveRowUp = function (editor) { return (__assign(__assign({}, getToolDefinition("moveRowUp")), { key: "moveRowUp", type: "button", onClick: function () { return moveRowUpAction(editor); } })); };
+var moveRowDown = function (editor) { return (__assign(__assign({}, getToolDefinition("moveRowDown")), { key: "moveRowDown", type: "button", onClick: function () { return moveRowDownAction(editor); } })); };
var deleteRow = function (editor) { return (__assign(__assign({}, getToolDefinition("deleteRow")), { key: "deleteRow", type: "button", onClick: function () { return editor.chain().focus().deleteRow().run(); } })); };
var deleteTable = function (editor) { return (__assign(__assign({}, getToolDefinition("deleteTable")), { key: "deleteTable", type: "button", onClick: function () { return editor.chain().focus().deleteTable().run(); } })); };
var cellProperties = function (editor) { return (__assign(__assign({}, getToolDefinition("cellProperties")), { key: "cellProperties", type: "button", onClick: function () {
diff --git a/packages/editor/dist/toolbar/types.d.ts b/packages/editor/dist/toolbar/types.d.ts
index 20bbc1c61..f1658e4bc 100644
--- a/packages/editor/dist/toolbar/types.d.ts
+++ b/packages/editor/dist/toolbar/types.d.ts
@@ -1,9 +1,10 @@
import { Editor } from "@tiptap/core";
import { IconNames } from "./icons";
import { ToolId } from "./tools";
+export declare type ToolButtonVariant = "small" | "normal";
export declare type ToolProps = ToolDefinition & {
editor: Editor;
- variant?: "small" | "normal";
+ variant?: ToolButtonVariant;
};
export declare type ToolDefinition = {
icon: IconNames;
diff --git a/packages/editor/dist/toolbar/utils/dom.d.ts b/packages/editor/dist/toolbar/utils/dom.d.ts
index 03ae30042..e3fdd7ffb 100644
--- a/packages/editor/dist/toolbar/utils/dom.d.ts
+++ b/packages/editor/dist/toolbar/utils/dom.d.ts
@@ -1,2 +1,4 @@
export declare function getToolbarElement(): HTMLElement;
export declare function getPopupContainer(): HTMLElement;
+export declare function getEditorContainer(): HTMLElement;
+export declare function getEditorDOM(): HTMLElement;
diff --git a/packages/editor/dist/toolbar/utils/dom.js b/packages/editor/dist/toolbar/utils/dom.js
index a8c8fb214..729a9f754 100644
--- a/packages/editor/dist/toolbar/utils/dom.js
+++ b/packages/editor/dist/toolbar/utils/dom.js
@@ -4,3 +4,11 @@ export function getToolbarElement() {
export function getPopupContainer() {
return (document.getElementById("popup-container") || undefined);
}
+export function getEditorContainer() {
+ return (document.querySelector(".editor") ||
+ getPopupContainer());
+}
+export function getEditorDOM() {
+ return (document.querySelector(".ProseMirror") ||
+ getEditorContainer()); // ProseMirror
+}
diff --git a/packages/editor/dist/toolbar/utils/prosemirror.d.ts b/packages/editor/dist/toolbar/utils/prosemirror.d.ts
new file mode 100644
index 000000000..bfd6b17fe
--- /dev/null
+++ b/packages/editor/dist/toolbar/utils/prosemirror.d.ts
@@ -0,0 +1,4 @@
+import { Editor } from "@tiptap/core";
+import { Node } from "prosemirror-model";
+export declare function findSelectedDOMNode(editor: Editor, types: string[]): HTMLElement | null;
+export declare function findSelectedNode(editor: Editor, type: string): Node | null;
diff --git a/packages/editor/dist/toolbar/utils/prosemirror.js b/packages/editor/dist/toolbar/utils/prosemirror.js
new file mode 100644
index 000000000..f7acbd3ea
--- /dev/null
+++ b/packages/editor/dist/toolbar/utils/prosemirror.js
@@ -0,0 +1,23 @@
+import { findParentNode, } from "@tiptap/core";
+export function findSelectedDOMNode(editor, types) {
+ var _a;
+ var $anchor = editor.state.selection.$anchor;
+ var selectedNode = editor.state.doc.nodeAt($anchor.pos);
+ var pos = types.includes((selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.type.name) || "")
+ ? $anchor.pos
+ : (_a = findParentNode(function (node) { return types.includes(node.type.name); })(editor.state.selection)) === null || _a === void 0 ? void 0 : _a.pos;
+ if (!pos)
+ return null;
+ return editor.view.nodeDOM(pos) || null;
+}
+export function findSelectedNode(editor, type) {
+ var _a;
+ var $anchor = editor.state.selection.$anchor;
+ var selectedNode = editor.state.doc.nodeAt($anchor.pos);
+ var pos = (selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.type.name) === type
+ ? $anchor.pos
+ : (_a = findParentNode(function (node) { return node.type.name === type; })(editor.state.selection)) === null || _a === void 0 ? void 0 : _a.pos;
+ if (!pos)
+ return null;
+ return editor.state.doc.nodeAt(pos);
+}
diff --git a/packages/editor/dist/utils/position.js b/packages/editor/dist/utils/position.js
index 519292baf..a5befc6a8 100644
--- a/packages/editor/dist/utils/position.js
+++ b/packages/editor/dist/utils/position.js
@@ -24,12 +24,11 @@ export function getPosition(element, options) {
else {
position.left = x;
}
- if (width) {
- if (location === "right")
- position.left += width;
- else if (location === "left")
- position.left -= elementWidth;
+ if (width && location === "right") {
+ position.left += width;
}
+ else if (location === "left")
+ position.left -= elementWidth;
if (actualY + elementHeight > windowHeight) {
position.top = windowHeight - elementHeight;
}
@@ -54,10 +53,10 @@ export function getPosition(element, options) {
}
if (yAnchor) {
var anchorY = getElementPosition(yAnchor, isTargetAbsolute);
- position.top = anchorY.actualY - elementHeight;
+ position.top = anchorY.y - elementHeight;
}
- position.top = position.top < 0 ? 0 : position.top;
- position.left = position.left < 0 ? 0 : position.left;
+ position.top = isTargetAbsolute && position.top < 0 ? 0 : position.top;
+ position.left = isTargetAbsolute && position.left < 0 ? 0 : position.left;
position.top += location === "below" ? yOffset : -yOffset;
position.left += xOffset;
return position;
diff --git a/packages/editor/src/components/popup-presenter/index.tsx b/packages/editor/src/components/popup-presenter/index.tsx
index 389e5b131..4debac680 100644
--- a/packages/editor/src/components/popup-presenter/index.tsx
+++ b/packages/editor/src/components/popup-presenter/index.tsx
@@ -183,7 +183,7 @@ export function PopupPresenter(props: PropsWithChildren<PopupPresenterProps>) {
display: "flex",
width: "fit-content",
height: "fit-content",
- position: "absolute",
+ position: "fixed",
backgroundColor: undefined,
padding: 0,
zIndex: 0,
diff --git a/packages/editor/src/extensions/attachment/component.tsx b/packages/editor/src/extensions/attachment/component.tsx
index ac558a9b8..dfd86f8e1 100644
--- a/packages/editor/src/extensions/attachment/component.tsx
+++ b/packages/editor/src/extensions/attachment/component.tsx
@@ -15,13 +15,7 @@ export function AttachmentComponent(
const { editor, node, selected } = props;
const { hash, filename, size, progress } = node.attrs;
const elementRef = useRef<HTMLSpanElement>();
- // const isActive = editor.isActive("attachment", { hash });
- // const [isToolbarVisible, setIsToolbarVisible] = useState<boolean>();
- // useEffect(() => {
- // setIsToolbarVisible(isActive);
- // }, [isActive]);
- console.log(progress);
return (
<>
<Box
@@ -34,7 +28,6 @@ export function AttachmentComponent(
overflow: "hidden",
position: "relative",
justifyContent: "center",
- zIndex: 1,
userSelect: "none",
alignItems: "center",
backgroundColor: "bgSecondary",
diff --git a/packages/editor/src/extensions/image/component.tsx b/packages/editor/src/extensions/image/component.tsx
index 2768f3ac2..e540943c4 100644
--- a/packages/editor/src/extensions/image/component.tsx
+++ b/packages/editor/src/extensions/image/component.tsx
@@ -15,6 +15,7 @@ import { ImageProperties } from "../../toolbar/popups/image-properties";
import { Popup } from "../../toolbar/components/popup";
import { Icon } from "../../toolbar/components/icon";
import { Icons } from "../../toolbar/icons";
+import { ImageToolbar } from "../../toolbar/floatingmenus/image";
export function ImageComponent(
props: SelectionBasedReactNodeViewProps<
@@ -70,13 +71,16 @@ export function ImageComponent(
>
{selected && (
<Flex sx={{ position: "relative", justifyContent: "end" }}>
- <ImageToolbar
- editor={editor}
- float={float}
- align={align}
- height={height || 0}
- width={width || 0}
- />
+ <Flex
+ sx={{
+ position: "absolute",
+ top: -40,
+ mb: 2,
+ alignItems: "end",
+ }}
+ >
+ <ImageToolbar editor={editor} />
+ </Flex>
</Flex>
)}
<Icon
@@ -90,7 +94,7 @@ export function ImageComponent(
top: 2,
left: 2,
zIndex: 999,
- opacity: 0,
+ opacity: selected ? 1 : 0,
}}
/>
<Image
@@ -114,127 +118,6 @@ export function ImageComponent(
);
}
-type ImageToolbarProps = ImageAlignmentOptions &
- Required<ImageSizeOptions> & {
- editor: Editor;
- };
-
-function ImageToolbar(props: ImageToolbarProps) {
- const { editor, float, height, width } = props;
- const [isOpen, setIsOpen] = useState(false);
- const ref = useRef<HTMLDivElement>();
-
- return (
- <Flex
- ref={ref}
- sx={{
- flexDirection: "column",
- position: "absolute",
- top: -40,
- mb: 2,
- zIndex: 9999,
- alignItems: "end",
- }}
- >
- <Flex
- sx={{
- bg: "background",
- boxShadow: "menu",
- flexWrap: "nowrap",
- borderRadius: "default",
- mb: 2,
- }}
- >
- <Flex
- className="toolbar-group"
- sx={{
- pr: 1,
- mr: 1,
- borderRight: "1px solid var(--border)",
- ":last-of-type": { mr: 0, pr: 0, borderRight: "none" },
- }}
- >
- <ToolButton
- toggled={false}
- title="Align left"
- id="alignLeft"
- icon="alignLeft"
- onClick={() =>
- editor.chain().focus().setImageAlignment({ align: "left" }).run()
- }
- />
- {float ? null : (
- <ToolButton
- toggled={false}
- title="Align center"
- id="alignCenter"
- icon="alignCenter"
- onClick={() =>
- editor
- .chain()
- .focus()
- .setImageAlignment({ align: "center" })
- .run()
- }
- />
- )}
- <ToolButton
- toggled={false}
- title="Align right"
- id="alignRight"
- icon="alignRight"
- onClick={() =>
- editor.chain().focus().setImageAlignment({ align: "right" }).run()
- }
- />
- </Flex>
- <Flex
- className="toolbar-group"
- sx={{
- pr: 1,
- mr: 1,
- borderRight: "1px solid var(--border)",
- ":last-of-type": { mr: 0, pr: 0, borderRight: "none" },
- }}
- >
- <ToolButton
- toggled={isOpen}
- title="Image properties"
- id="imageProperties"
- icon="more"
- onClick={() => setIsOpen((s) => !s)}
- />
- </Flex>
- </Flex>
-
- <ResponsivePresenter
- isOpen={isOpen}
- desktop="menu"
- mobile="sheet"
- onClose={() => setIsOpen(false)}
- blocking
- focusOnRender={false}
- position={{
- target: ref.current || "mouse",
- align: "start",
- location: "below",
- yOffset: 10,
- isTargetAbsolute: true,
- }}
- >
- <Popup
- title="Image properties"
- onClose={() => {
- setIsOpen(false);
- }}
- >
- <ImageProperties {...props} />
- </Popup>
- </ResponsivePresenter>
- </Flex>
- );
-}
-
async function dataUriToBlobURL(dataurl: string) {
if (!dataurl.startsWith("data:image")) return dataurl;
diff --git a/packages/editor/src/toolbar/floating-menus/table/actions.ts b/packages/editor/src/extensions/table/actions.ts
similarity index 100%
rename from packages/editor/src/toolbar/floating-menus/table/actions.ts
rename to packages/editor/src/extensions/table/actions.ts
diff --git a/packages/editor/src/extensions/table/component.tsx b/packages/editor/src/extensions/table/component.tsx
new file mode 100644
index 000000000..420a8a69f
--- /dev/null
+++ b/packages/editor/src/extensions/table/component.tsx
@@ -0,0 +1,222 @@
+import { Flex, Text } from "rebass";
+import {
+ createNodeView,
+ ReactNodeView,
+ ReactNodeViewProps,
+ SelectionBasedNodeView,
+ SelectionBasedReactNodeViewProps,
+} from "../react";
+import { Icon } from "../../toolbar/components/icon";
+import { Icons } from "../../toolbar/icons";
+import { Node as ProsemirrorNode } from "prosemirror-model";
+import { Transaction } from "prosemirror-state";
+import {
+ Editor,
+ findChildren,
+ findParentNode,
+ getNodeType,
+ NodeWithPos,
+} from "@tiptap/core";
+import { useCallback, useEffect, useRef, useState } from "react";
+import { updateColumnsOnResize } from "prosemirror-tables";
+import { Decoration, DecorationSource, NodeView } from "prosemirror-view";
+import { TableOptions } from "./table";
+import { ToolbarGroup } from "../../toolbar/components/toolbar-group";
+import {
+ InsertColumnRight,
+ InsertRowBelow,
+ RowProperties,
+ TableProperties,
+} from "../../toolbar/tools/table";
+import { getToolDefinition } from "../../toolbar/tool-definitions";
+import { getPosition, PositionOptions } from "../../utils/position";
+import { findSelectedDOMNode } from "../../toolbar/utils/prosemirror";
+
+export function TableComponent(props: SelectionBasedReactNodeViewProps) {
+ const { editor, node, forwardRef } = props;
+ const colgroupRef = useRef<HTMLTableColElement>(null);
+ const tableRef = useRef<HTMLTableElement>();
+ const selected = editor.isActive("table");
+
+ useEffect(() => {
+ if (!colgroupRef.current || !tableRef.current) return;
+
+ updateColumnsOnResize(node, colgroupRef.current, tableRef.current, 50);
+ }, [node]);
+
+ return (
+ <>
+ {selected && (
+ <>
+ <TableRowToolbar editor={editor} table={tableRef.current} />
+ <TableColumnToolbar editor={editor} table={tableRef.current} />
+ </>
+ )}
+ <div className="tableWrapper">
+ <table
+ ref={(ref) => {
+ forwardRef?.(ref);
+ tableRef.current = ref || undefined;
+ }}
+ >
+ <colgroup ref={colgroupRef} />
+ {/* <tbody /> */}
+ </table>
+ </div>
+ </>
+ );
+}
+
+export function TableNodeView(editor: Editor) {
+ class TableNode
+ extends SelectionBasedNodeView<SelectionBasedReactNodeViewProps<{}>>
+ implements NodeView
+ {
+ constructor(node: ProsemirrorNode, cellMinWidth: number) {
+ super(
+ node,
+ editor,
+ () => 0, // todo
+ editor.storage.portalProviderAPI,
+ editor.storage.eventDispatcher,
+ {
+ component: TableComponent,
+ shouldUpdate: (prev, next) => {
+ return prev.type === next.type;
+ },
+ contentDOMFactory: () => {
+ const dom = document.createElement("tbody");
+ return { dom };
+ },
+ wrapperFactory: () => {
+ const dom = document.createElement("div");
+ dom.style.position = "relative";
+ return dom;
+ },
+ }
+ );
+ super.init();
+ }
+ }
+ return TableNode as unknown as NodeView;
+}
+
+type TableToolbarProps = {
+ editor: Editor;
+ table?: HTMLTableElement;
+};
+
+function TableRowToolbar(props: TableToolbarProps) {
+ const { editor } = props;
+ const rowToolsRef = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ if (!rowToolsRef.current) {
+ return;
+ }
+
+ const currentRow = findSelectedDOMNode(editor, ["tableRow"]);
+ if (!currentRow) return;
+
+ const pos = getPosition(rowToolsRef.current, {
+ location: "left",
+ target: currentRow,
+ align: "start",
+ xOffset: -5,
+ yOffset: -3,
+ });
+ rowToolsRef.current.style.top = `${pos.top}px`;
+ rowToolsRef.current.style.left = `${pos.left}px`;
+ }, [editor.state.selection, rowToolsRef.current]);
+
+ return (
+ <Flex
+ ref={rowToolsRef}
+ sx={{
+ zIndex: 999,
+ top: 0,
+ left: 0,
+ position: "absolute",
+ bg: "background",
+ flexWrap: "nowrap",
+ borderRadius: "default",
+ opacity: 0.3,
+ ":hover": {
+ opacity: 1,
+ },
+ }}
+ >
+ <RowProperties
+ {...getToolDefinition("rowProperties")}
+ icon="more"
+ variant="small"
+ editor={editor}
+ />
+ <InsertRowBelow
+ {...getToolDefinition("insertRowBelow")}
+ editor={editor}
+ variant="small"
+ />
+ </Flex>
+ );
+}
+
+function TableColumnToolbar(props: TableToolbarProps) {
+ const { editor, table } = props;
+ const columnToolsRef = useRef<HTMLDivElement>(null);
+ useEffect(() => {
+ if (!columnToolsRef.current || !table) {
+ return;
+ }
+
+ const currentCell = findSelectedDOMNode(editor, [
+ "tableCell",
+ "tableHeader",
+ ]);
+ if (!currentCell) return;
+
+ // tableRef.current
+ const pos = getPosition(columnToolsRef.current, {
+ location: "top",
+ align: "center",
+ target: currentCell as HTMLElement,
+ yAnchor: table,
+ yOffset: 2,
+ });
+
+ columnToolsRef.current.style.left = `${pos.left}px`;
+ columnToolsRef.current.style.top = `${pos.top}px`;
+ }, [editor.state.selection, columnToolsRef.current, table]);
+
+ return (
+ <Flex
+ ref={columnToolsRef}
+ sx={{
+ zIndex: 999,
+ top: 0,
+ left: 0,
+ position: "absolute",
+ bg: "background",
+ flexWrap: "nowrap",
+ borderRadius: "default",
+ opacity: 0.3,
+ ":hover": {
+ opacity: 1,
+ },
+ }}
+ >
+ <TableProperties
+ editor={editor}
+ title="tableProperties"
+ icon="more"
+ variant={"small"}
+ />
+ <InsertColumnRight
+ {...getToolDefinition("insertColumnRight")}
+ editor={editor}
+ variant={"small"}
+ icon="plus"
+ />
+ </Flex>
+ );
+}
diff --git a/packages/editor/src/extensions/table/index.ts b/packages/editor/src/extensions/table/index.ts
new file mode 100644
index 000000000..0e948df9e
--- /dev/null
+++ b/packages/editor/src/extensions/table/index.ts
@@ -0,0 +1 @@
+export * from "./table";
diff --git a/packages/editor/src/extensions/table/table.ts b/packages/editor/src/extensions/table/table.ts
new file mode 100644
index 000000000..7dca45f10
--- /dev/null
+++ b/packages/editor/src/extensions/table/table.ts
@@ -0,0 +1,313 @@
+import {
+ callOrReturn,
+ getExtensionField,
+ mergeAttributes,
+ Node,
+ ParentConfig,
+} from "@tiptap/core";
+import { TextSelection } from "prosemirror-state";
+import {
+ addColumnAfter,
+ addColumnBefore,
+ addRowAfter,
+ addRowBefore,
+ CellSelection,
+ columnResizing,
+ deleteColumn,
+ deleteRow,
+ deleteTable,
+ fixTables,
+ goToNextCell,
+ mergeCells,
+ setCellAttr,
+ splitCell,
+ tableEditing,
+ toggleHeader,
+ toggleHeaderCell,
+} from "prosemirror-tables";
+import { NodeView } from "prosemirror-view";
+
+import { TableNodeView } from "./component";
+import { createTable } from "./utils/createTable";
+import { deleteTableWhenAllCellsSelected } from "./utils/deleteTableWhenAllCellsSelected";
+
+export interface TableOptions {
+ HTMLAttributes: Record<string, any>;
+ resizable: boolean;
+ handleWidth: number;
+ cellMinWidth: number;
+ View: NodeView;
+ lastColumnResizable: boolean;
+ allowTableNodeSelection: boolean;
+}
+
+declare module "@tiptap/core" {
+ interface Commands<ReturnType> {
+ table: {
+ insertTable: (options?: {
+ rows?: number;
+ cols?: number;
+ withHeaderRow?: boolean;
+ }) => ReturnType;
+ addColumnBefore: () => ReturnType;
+ addColumnAfter: () => ReturnType;
+ deleteColumn: () => ReturnType;
+ addRowBefore: () => ReturnType;
+ addRowAfter: () => ReturnType;
+ deleteRow: () => ReturnType;
+ deleteTable: () => ReturnType;
+ mergeCells: () => ReturnType;
+ splitCell: () => ReturnType;
+ toggleHeaderColumn: () => ReturnType;
+ toggleHeaderRow: () => ReturnType;
+ toggleHeaderCell: () => ReturnType;
+ mergeOrSplit: () => ReturnType;
+ setCellAttribute: (name: string, value: any) => ReturnType;
+ goToNextCell: () => ReturnType;
+ goToPreviousCell: () => ReturnType;
+ fixTables: () => ReturnType;
+ setCellSelection: (position: {
+ anchorCell: number;
+ headCell?: number;
+ }) => ReturnType;
+ };
+ }
+
+ interface NodeConfig<Options, Storage> {
+ /**
+ * Table Role
+ */
+ tableRole?:
+ | string
+ | ((this: {
+ name: string;
+ options: Options;
+ storage: Storage;
+ parent: ParentConfig<NodeConfig<Options>>["tableRole"];
+ }) => string);
+ }
+}
+
+export const Table = Node.create<TableOptions>({
+ name: "table",
+
+ // @ts-ignore
+ addOptions() {
+ return {
+ HTMLAttributes: {},
+ resizable: false,
+ handleWidth: 5,
+ cellMinWidth: 25,
+ // TODO: fix
+ View: null,
+ lastColumnResizable: true,
+ allowTableNodeSelection: false,
+ };
+ },
+
+ content: "tableRow+",
+
+ tableRole: "table",
+
+ isolating: true,
+
+ group: "block",
+
+ parseHTML() {
+ return [{ tag: "table" }];
+ },
+
+ renderHTML({ HTMLAttributes }) {
+ return [
+ "table",
+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
+ ["tbody", 0],
+ ];
+ },
+
+ addCommands() {
+ return {
+ insertTable:
+ ({ rows = 3, cols = 3, withHeaderRow = true } = {}) =>
+ ({ tr, dispatch, editor }) => {
+ const node = createTable(editor.schema, rows, cols, withHeaderRow);
+
+ if (dispatch) {
+ const offset = tr.selection.anchor + 1;
+
+ tr.replaceSelectionWith(node)
+ .scrollIntoView()
+ .setSelection(TextSelection.near(tr.doc.resolve(offset)));
+ }
+
+ return true;
+ },
+ addColumnBefore:
+ () =>
+ ({ state, dispatch }) => {
+ return addColumnBefore(state, dispatch);
+ },
+ addColumnAfter:
+ () =>
+ ({ state, dispatch }) => {
+ return addColumnAfter(state, dispatch);
+ },
+ deleteColumn:
+ () =>
+ ({ state, dispatch }) => {
+ return deleteColumn(state, dispatch);
+ },
+ addRowBefore:
+ () =>
+ ({ state, dispatch }) => {
+ return addRowBefore(state, dispatch);
+ },
+ addRowAfter:
+ () =>
+ ({ state, dispatch }) => {
+ return addRowAfter(state, dispatch);
+ },
+ deleteRow:
+ () =>
+ ({ state, dispatch }) => {
+ return deleteRow(state, dispatch);
+ },
+ deleteTable:
+ () =>
+ ({ state, dispatch }) => {
+ return deleteTable(state, dispatch);
+ },
+ mergeCells:
+ () =>
+ ({ state, dispatch }) => {
+ return mergeCells(state, dispatch);
+ },
+ splitCell:
+ () =>
+ ({ state, dispatch }) => {
+ return splitCell(state, dispatch);
+ },
+ toggleHeaderColumn:
+ () =>
+ ({ state, dispatch }) => {
+ return toggleHeader("column")(state, dispatch);
+ },
+ toggleHeaderRow:
+ () =>
+ ({ state, dispatch }) => {
+ return toggleHeader("row")(state, dispatch);
+ },
+ toggleHeaderCell:
+ () =>
+ ({ state, dispatch }) => {
+ return toggleHeaderCell(state, dispatch);
+ },
+ mergeOrSplit:
+ () =>
+ ({ state, dispatch }) => {
+ if (mergeCells(state, dispatch)) {
+ return true;
+ }
+
+ return splitCell(state, dispatch);
+ },
+ setCellAttribute:
+ (name, value) =>
+ ({ state, dispatch }) => {
+ return setCellAttr(name, value)(state, dispatch);
+ },
+ goToNextCell:
+ () =>
+ ({ state, dispatch }) => {
+ return goToNextCell(1)(state, dispatch);
+ },
+ goToPreviousCell:
+ () =>
+ ({ state, dispatch }) => {
+ return goToNextCell(-1)(state, dispatch);
+ },
+ fixTables:
+ () =>
+ ({ state, dispatch }) => {
+ if (dispatch) {
+ fixTables(state);
+ }
+
+ return true;
+ },
+ setCellSelection:
+ (position) =>
+ ({ tr, dispatch }) => {
+ if (dispatch) {
+ const selection = CellSelection.create(
+ tr.doc,
+ position.anchorCell,
+ position.headCell
+ );
+
+ // @ts-ignore
+ tr.setSelection(selection);
+ }
+
+ return true;
+ },
+ };
+ },
+
+ addKeyboardShortcuts() {
+ return {
+ Tab: () => {
+ if (this.editor.commands.goToNextCell()) {
+ return true;
+ }
+
+ if (!this.editor.can().addRowAfter()) {
+ return false;
+ }
+
+ return this.editor.chain().addRowAfter().goToNextCell().run();
+ },
+ "Shift-Tab": () => this.editor.commands.goToPreviousCell(),
+ Backspace: deleteTableWhenAllCellsSelected,
+ "Mod-Backspace": deleteTableWhenAllCellsSelected,
+ Delete: deleteTableWhenAllCellsSelected,
+ "Mod-Delete": deleteTableWhenAllCellsSelected,
+ };
+ },
+
+ addProseMirrorPlugins() {
+ const isResizable = this.options.resizable && this.editor.isEditable;
+
+ return [
+ ...(isResizable
+ ? [
+ columnResizing({
+ handleWidth: this.options.handleWidth,
+ cellMinWidth: this.options.cellMinWidth,
+ View: TableNodeView(this.editor),
+ // TODO: PR for @types/prosemirror-tables
+ // @ts-ignore (incorrect type)
+ lastColumnResizable: this.options.lastColumnResizable,
+ }),
+ ]
+ : []),
+ tableEditing({
+ allowTableNodeSelection: this.options.allowTableNodeSelection,
+ }),
+ ];
+ },
+
+ extendNodeSchema(extension) {
+ const context = {
+ name: extension.name,
+ options: extension.options,
+ storage: extension.storage,
+ };
+
+ return {
+ tableRole: callOrReturn(
+ getExtensionField(extension, "tableRole", context)
+ ),
+ };
+ },
+});
diff --git a/packages/editor/src/extensions/table/utils/createCell.ts b/packages/editor/src/extensions/table/utils/createCell.ts
new file mode 100644
index 000000000..0d4c29c2f
--- /dev/null
+++ b/packages/editor/src/extensions/table/utils/createCell.ts
@@ -0,0 +1,17 @@
+import {
+ Fragment,
+ Node as ProsemirrorNode,
+ NodeType,
+ Schema,
+} from "prosemirror-model";
+
+export function createCell(
+ cellType: NodeType,
+ cellContent?: Fragment | ProsemirrorNode | Array<ProsemirrorNode>
+): ProsemirrorNode | null | undefined {
+ if (cellContent) {
+ return cellType.createChecked(null, cellContent);
+ }
+
+ return cellType.createAndFill();
+}
diff --git a/packages/editor/src/extensions/table/utils/createTable.ts b/packages/editor/src/extensions/table/utils/createTable.ts
new file mode 100644
index 000000000..d970c3166
--- /dev/null
+++ b/packages/editor/src/extensions/table/utils/createTable.ts
@@ -0,0 +1,45 @@
+import { Fragment, Node as ProsemirrorNode, Schema } from "prosemirror-model";
+
+import { createCell } from "./createCell";
+import { getTableNodeTypes } from "./getTableNodeTypes";
+
+export function createTable(
+ schema: Schema,
+ rowsCount: number,
+ colsCount: number,
+ withHeaderRow: boolean,
+ cellContent?: Fragment | ProsemirrorNode | Array<ProsemirrorNode>
+): ProsemirrorNode {
+ const types = getTableNodeTypes(schema);
+ const headerCells = [];
+ const cells = [];
+
+ for (let index = 0; index < colsCount; index += 1) {
+ const cell = createCell(types.cell, cellContent);
+
+ if (cell) {
+ cells.push(cell);
+ }
+
+ if (withHeaderRow) {
+ const headerCell = createCell(types.header_cell, cellContent);
+
+ if (headerCell) {
+ headerCells.push(headerCell);
+ }
+ }
+ }
+
+ const rows = [];
+
+ for (let index = 0; index < rowsCount; index += 1) {
+ rows.push(
+ types.row.createChecked(
+ null,
+ withHeaderRow && index === 0 ? headerCells : cells
+ )
+ );
+ }
+
+ return types.table.createChecked(null, rows);
+}
diff --git a/packages/editor/src/extensions/table/utils/deleteTableWhenAllCellsSelected.ts b/packages/editor/src/extensions/table/utils/deleteTableWhenAllCellsSelected.ts
new file mode 100644
index 000000000..50ebaeee8
--- /dev/null
+++ b/packages/editor/src/extensions/table/utils/deleteTableWhenAllCellsSelected.ts
@@ -0,0 +1,36 @@
+import { findParentNodeClosestToPos, KeyboardShortcutCommand } from '@tiptap/core'
+
+import { isCellSelection } from './isCellSelection'
+
+export const deleteTableWhenAllCellsSelected: KeyboardShortcutCommand = ({ editor }) => {
+ const { selection } = editor.state
+
+ if (!isCellSelection(selection)) {
+ return false
+ }
+
+ let cellCount = 0
+ const table = findParentNodeClosestToPos(selection.ranges[0].$from, node => {
+ return node.type.name === 'table'
+ })
+
+ table?.node.descendants(node => {
+ if (node.type.name === 'table') {
+ return false
+ }
+
+ if (['tableCell', 'tableHeader'].includes(node.type.name)) {
+ cellCount += 1
+ }
+ })
+
+ const allCellsSelected = cellCount === selection.ranges.length
+
+ if (!allCellsSelected) {
+ return false
+ }
+
+ editor.commands.deleteTable()
+
+ return true
+}
diff --git a/packages/editor/src/extensions/table/utils/getTableNodeTypes.ts b/packages/editor/src/extensions/table/utils/getTableNodeTypes.ts
new file mode 100644
index 000000000..489e201cf
--- /dev/null
+++ b/packages/editor/src/extensions/table/utils/getTableNodeTypes.ts
@@ -0,0 +1,21 @@
+import { NodeType, Schema } from 'prosemirror-model'
+
+export function getTableNodeTypes(schema: Schema): { [key: string]: NodeType } {
+ if (schema.cached.tableNodeTypes) {
+ return schema.cached.tableNodeTypes
+ }
+
+ const roles: { [key: string]: NodeType } = {}
+
+ Object.keys(schema.nodes).forEach(type => {
+ const nodeType = schema.nodes[type]
+
+ if (nodeType.spec.tableRole) {
+ roles[nodeType.spec.tableRole] = nodeType
+ }
+ })
+
+ schema.cached.tableNodeTypes = roles
+
+ return roles
+}
diff --git a/packages/editor/src/extensions/table/utils/isCellSelection.ts b/packages/editor/src/extensions/table/utils/isCellSelection.ts
new file mode 100644
index 000000000..4fc826e6c
--- /dev/null
+++ b/packages/editor/src/extensions/table/utils/isCellSelection.ts
@@ -0,0 +1,5 @@
+import { CellSelection } from 'prosemirror-tables'
+
+export function isCellSelection(value: unknown): value is CellSelection {
+ return value instanceof CellSelection
+}
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index 681a55070..073b745e3 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -17,7 +17,6 @@ import BulletList from "./extensions/bullet-list";
import OrderedList from "./extensions/ordered-list";
import Highlight from "@tiptap/extension-highlight";
import Color from "@tiptap/extension-color";
-import Table from "@tiptap/extension-table";
import TableRow from "@tiptap/extension-table-row";
import TableCell from "./extensions/table-cell";
import TableHeader from "@tiptap/extension-table-header";
@@ -39,6 +38,7 @@ import {
} from "./extensions/react";
import { OutlineList } from "./extensions/outline-list";
import { OutlineListItem } from "./extensions/outline-list-item";
+import { Table } from "./extensions/table";
EditorView.prototype.updateState = function updateState(state) {
if (!(this as any).docView) return; // This prevents the matchesNode error on hot reloads
@@ -94,6 +94,7 @@ const useTiptap = (
Table.configure({
resizable: true,
allowTableNodeSelection: true,
+ cellMinWidth: 50,
}),
TableRow,
TableCell,
diff --git a/packages/editor/src/styles.css b/packages/editor/src/styles.css
index fdc35f548..b55571ceb 100644
--- a/packages/editor/src/styles.css
+++ b/packages/editor/src/styles.css
@@ -162,7 +162,7 @@ img.ProseMirror-separator {
}
.tableWrapper {
- padding: 1rem 0;
+ /* padding: 1rem 0; */
overflow-x: auto;
}
diff --git a/packages/editor/src/toolbar/components/tool-button.tsx b/packages/editor/src/toolbar/components/tool-button.tsx
index 23ca430e7..f5d805be0 100644
--- a/packages/editor/src/toolbar/components/tool-button.tsx
+++ b/packages/editor/src/toolbar/components/tool-button.tsx
@@ -4,6 +4,7 @@ import { useTheme } from "emotion-theming";
import React from "react";
import { Button, ButtonProps } from "rebass";
import { IconNames, Icons } from "../icons";
+import { ToolButtonVariant } from "../types";
import { Icon } from "./icon";
export type ToolButtonProps = ButtonProps & {
@@ -12,7 +13,7 @@ export type ToolButtonProps = ButtonProps & {
iconSize?: number;
toggled: boolean;
buttonRef?: React.MutableRefObject<HTMLButtonElement | null | undefined>;
- variant?: "small" | "normal";
+ variant?: ToolButtonVariant;
};
export function ToolButton(props: ToolButtonProps) {
const {
diff --git a/packages/editor/src/toolbar/components/toolbar-group.tsx b/packages/editor/src/toolbar/components/toolbar-group.tsx
index bf5a220e8..73051475c 100644
--- a/packages/editor/src/toolbar/components/toolbar-group.tsx
+++ b/packages/editor/src/toolbar/components/toolbar-group.tsx
@@ -1,4 +1,4 @@
-import { ToolbarGroupDefinition } from "../types";
+import { ToolbarGroupDefinition, ToolButtonVariant } from "../types";
import { findTool } from "../tools";
import { Flex, FlexProps } from "rebass";
import { Editor } from "@tiptap/core";
@@ -8,6 +8,7 @@ import { getToolDefinition } from "../tool-definitions";
export type ToolbarGroupProps = FlexProps & {
tools: ToolbarGroupDefinition;
editor: Editor;
+ variant?: ToolButtonVariant;
};
export function ToolbarGroup(props: ToolbarGroupProps) {
const { tools, editor, ...flexProps } = props;
@@ -28,7 +29,6 @@ export function ToolbarGroup(props: ToolbarGroupProps) {
} else {
const Component = findTool(toolId);
const toolDefinition = getToolDefinition(toolId);
- if (toolId === "textColor") console.log("Rendering", toolId);
return (
<Component
key={toolDefinition.title}
diff --git a/packages/editor/src/toolbar/floating-menus/image.tsx b/packages/editor/src/toolbar/floating-menus/image.tsx
new file mode 100644
index 000000000..c9f00db6a
--- /dev/null
+++ b/packages/editor/src/toolbar/floating-menus/image.tsx
@@ -0,0 +1,23 @@
+import { ToolbarGroup } from "../components/toolbar-group";
+import { FloatingMenuProps } from "./types";
+
+export function ImageToolbar(props: FloatingMenuProps) {
+ const { editor } = props;
+
+ return (
+ <ToolbarGroup
+ editor={editor}
+ tools={[
+ "imageAlignLeft",
+ "imageAlignCenter",
+ "imageAlignRight",
+ "imageProperties",
+ ]}
+ sx={{
+ boxShadow: "menu",
+ borderRadius: "default",
+ bg: "background",
+ }}
+ />
+ );
+}
diff --git a/packages/editor/src/toolbar/floating-menus/index.tsx b/packages/editor/src/toolbar/floating-menus/index.tsx
index 4fa414b80..3a850b17d 100644
--- a/packages/editor/src/toolbar/floating-menus/index.tsx
+++ b/packages/editor/src/toolbar/floating-menus/index.tsx
@@ -1,22 +1,16 @@
-import {
- TableRowFloatingMenu,
- TableColumnFloatingMenu,
- TableFloatingMenu,
-} from "./table/table";
+import { TableRowFloatingMenu, TableColumnFloatingMenu } from "./table/table";
import { SearchReplaceFloatingMenu } from "./search-replace";
import { FloatingMenuProps } from "./types";
import { DesktopOnly, MobileOnly } from "../../components/responsive";
+import { ImageToolbar } from "./image";
export function EditorFloatingMenus(props: FloatingMenuProps) {
return (
<>
- <DesktopOnly>
+ {/* <DesktopOnly>
<TableRowFloatingMenu {...props} />
<TableColumnFloatingMenu {...props} />
- </DesktopOnly>
- <MobileOnly>
- <TableFloatingMenu {...props} />
- </MobileOnly>
+ </DesktopOnly> */}
<SearchReplaceFloatingMenu {...props} />
</>
);
diff --git a/packages/editor/src/toolbar/floating-menus/table/index.ts b/packages/editor/src/toolbar/floating-menus/table/index.ts
deleted file mode 100644
index 50a03f9a0..000000000
--- a/packages/editor/src/toolbar/floating-menus/table/index.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-export {
- TableColumnFloatingMenu,
- TableFloatingMenu,
- TableRowFloatingMenu,
-} from "./table";
diff --git a/packages/editor/src/toolbar/floating-menus/table/table.tsx b/packages/editor/src/toolbar/floating-menus/table/table.tsx
deleted file mode 100644
index 41a145bf0..000000000
--- a/packages/editor/src/toolbar/floating-menus/table/table.tsx
+++ /dev/null
@@ -1,223 +0,0 @@
-import { useEffect, useState } from "react";
-import { Flex } from "rebass";
-// import { ColorPicker, DEFAULT_COLORS } from "../tools/colors";
-import { FloatingMenuProps } from "../types";
-import { useToolbarLocation } from "../../stores/toolbar-store";
-import { PositionOptions } from "../../../utils/position";
-import { PopupPresenter } from "../../../components/popup-presenter";
-import { getToolbarElement } from "../../utils/dom";
-import {
- ColumnProperties,
- InsertColumnRight,
- InsertRowBelow,
- RowProperties,
- TableProperties,
-} from "../../tools/table";
-import { getToolDefinition } from "../../tool-definitions";
-
-export function TableRowFloatingMenu(props: FloatingMenuProps) {
- const { editor } = props;
- // const theme = editor.storage.theme as Theme;
- const [position, setPosition] = useState<PositionOptions | null>(null);
-
- useEffect(() => {
- if (
- !editor.isActive("tableCell") &&
- !editor.isActive("tableRow") &&
- !editor.isActive("tableHeader")
- ) {
- setPosition(null);
- return;
- }
-
- let { $from } = editor.state.selection;
-
- const selectedNode = $from.node();
- const pos = selectedNode.isTextblock ? $from.before() : $from.pos;
-
- const currentRow = (editor.view.nodeDOM(pos) as HTMLElement)?.closest("tr");
- if (!currentRow) return;
- setPosition((old) => {
- if (old?.target === currentRow) return old;
-
- return {
- isTargetAbsolute: true,
- location: "left",
- xOffset: -5,
- target: currentRow,
- // parent: editor.view.dom as HTMLElement,
- };
- });
- }, [editor.state.selection]);
-
- if (!position) return null;
-
- return (
- <PopupPresenter
- isOpen
- blocking={false}
- focusOnRender={false}
- onClose={() => {}}
- position={position}
- >
- <Flex
- sx={{
- bg: "background",
- flexWrap: "nowrap",
- borderRadius: "default",
- // opacity: isMenuOpen ? 1 : 0.3,
- ":hover": {
- opacity: 1,
- },
- }}
- >
- <RowProperties
- {...getToolDefinition("rowProperties")}
- icon="more"
- variant="small"
- editor={editor}
- />
- <InsertRowBelow
- {...getToolDefinition("insertRowBelow")}
- editor={editor}
- variant="small"
- />
- </Flex>
- </PopupPresenter>
- );
-}
-
-export function TableColumnFloatingMenu(props: FloatingMenuProps) {
- const { editor } = props;
- const [position, setPosition] = useState<PositionOptions | null>(null);
-
- useEffect(() => {
- if (
- !editor.isActive("tableCell") &&
- !editor.isActive("tableRow") &&
- !editor.isActive("tableHeader")
- ) {
- setPosition(null);
- return;
- }
- let { $from } = editor.state.selection;
-
- const selectedNode = $from.node();
- const pos = selectedNode.isTextblock ? $from.before() : $from.pos;
-
- const currentCell = (editor.view.nodeDOM(pos) as HTMLElement)?.closest(
- "td,th"
- );
- const currentTable = currentCell?.closest("table");
-
- if (!currentCell || !currentTable) return;
-
- setPosition((old) => {
- if (old?.target === currentCell) return old;
-
- return {
- isTargetAbsolute: true,
- location: "top",
- align: "center",
- yAnchor: currentTable,
- yOffset: 2,
- target: currentCell as HTMLElement,
- };
- });
- }, [editor.state.selection]);
-
- if (!position) return null;
-
- return (
- <PopupPresenter
- isOpen
- onClose={() => {}}
- blocking={false}
- position={position}
- focusOnRender={false}
- >
- <Flex
- sx={{
- bg: "background",
- flexWrap: "nowrap",
- borderRadius: "default",
- // opacity: 0.3,
- // opacity: isMenuOpen || showCellProps ? 1 : 0.3,
- ":hover": {
- opacity: 1,
- },
- }}
- >
- <TableProperties
- editor={editor}
- title="tableProperties"
- icon="more"
- variant={"small"}
- />
- <InsertColumnRight
- {...getToolDefinition("insertColumnRight")}
- editor={editor}
- variant={"small"}
- icon="plus"
- />
- </Flex>
- </PopupPresenter>
- );
-}
-
-export function TableFloatingMenu(props: FloatingMenuProps) {
- const { editor } = props;
- const toolbarLocation = useToolbarLocation();
- if (!editor.isActive("table")) return null;
- return (
- <PopupPresenter
- isOpen
- onClose={() => {}}
- blocking={false}
- position={{
- isTargetAbsolute: true,
- target: getToolbarElement(),
- location: toolbarLocation === "bottom" ? "top" : "below",
- }}
- focusOnRender={false}
- >
- <Flex
- sx={{
- bg: "background",
- flexWrap: "nowrap",
- borderRadius: "default",
- // opacity: 0.3,
- // opacity: isMenuOpen || showCellProps ? 1 : 0.3,
- ":hover": {
- opacity: 1,
- },
- }}
- >
- {/* <RowProperties
- title="Row properties"
- editor={editor}
- variant="normal"
- icon="rowProperties"
- />
- <InsertRowBelow
- title="Insert row below"
- icon="insertRowBelow"
- editor={editor}
- variant="normal"
- />
- <ColumnProperties
- title="Column properties"
- editor={editor}
- icon="columnProperties"
- variant={"normal"}
- />
- <InsertColumnRight
- editor={editor}
- title="Insert column right"
- variant={"normal"}
- icon="insertColumnRight"
- /> */}
- </Flex>
- </PopupPresenter>
- );
-}
diff --git a/packages/editor/src/toolbar/icons.ts b/packages/editor/src/toolbar/icons.ts
index 75ccc5ffc..4143424c5 100644
--- a/packages/editor/src/toolbar/icons.ts
+++ b/packages/editor/src/toolbar/icons.ts
@@ -92,6 +92,8 @@ import {
mdiTableOff,
mdiRectangleOutline,
mdiRectangle,
+ mdiImageEdit,
+ mdiImageEditOutline,
} from "@mdi/js";
export const Icons = {
@@ -119,6 +121,7 @@ export const Icons = {
link: mdiLinkPlus,
url: mdiLink,
image: mdiImage,
+ imageSettings: mdiImageEditOutline,
colorClear: mdiInvertColorsOff,
check: mdiCheck,
checkbox: mdiCheckboxMarkedOutline,
diff --git a/packages/editor/src/toolbar/popups/image-properties.tsx b/packages/editor/src/toolbar/popups/image-properties.tsx
index b2e0e3488..46a36bee7 100644
--- a/packages/editor/src/toolbar/popups/image-properties.tsx
+++ b/packages/editor/src/toolbar/popups/image-properties.tsx
@@ -8,6 +8,7 @@ import {
ImageSizeOptions,
} from "../../extensions/image";
import { Editor } from "@tiptap/core";
+import { InlineInput } from "../../components/inline-input";
export type ImagePropertiesProps = ImageSizeOptions &
ImageAlignmentOptions & { editor: Editor };
@@ -37,7 +38,7 @@ export function ImageProperties(props: ImagePropertiesProps) {
);
return (
- <Flex sx={{ width: 200, flexDirection: "column", p: 1 }}>
+ <Flex sx={{ width: ["auto", 300], flexDirection: "column", p: 1 }}>
<Flex sx={{ justifyContent: "space-between", alignItems: "center" }}>
<Text variant={"body"}>Floating?</Text>
<Toggle
@@ -51,22 +52,19 @@ export function ImageProperties(props: ImagePropertiesProps) {
/>
</Flex>
<Flex sx={{ alignItems: "center", mt: 2 }}>
- <Input
+ <InlineInput
+ label="width"
type="number"
- placeholder="Width"
value={width}
- sx={{
- mr: 2,
- p: 1,
- fontSize: "body",
+ containerProps={{
+ sx: { mr: 1 },
}}
onChange={(e) => onSizeChange(e.target.valueAsNumber)}
/>
- <Input
+ <InlineInput
+ label="height"
type="number"
- placeholder="Height"
value={height}
- sx={{ p: 1, fontSize: "body" }}
onChange={(e) => onSizeChange(undefined, e.target.valueAsNumber)}
/>
</Flex>
diff --git a/packages/editor/src/toolbar/tool-definitions.ts b/packages/editor/src/toolbar/tool-definitions.ts
index 83dc739ea..43c9984d2 100644
--- a/packages/editor/src/toolbar/tool-definitions.ts
+++ b/packages/editor/src/toolbar/tool-definitions.ts
@@ -200,6 +200,32 @@ const tools: Record<ToolId, ToolDefinition> = {
title: "Cell border width",
conditional: true,
},
+
+ imageSettings: {
+ icon: "imageSettings",
+ title: "Image settings",
+ conditional: true,
+ },
+ imageAlignCenter: {
+ icon: "alignCenter",
+ title: "Align center",
+ conditional: true,
+ },
+ imageAlignLeft: {
+ icon: "alignLeft",
+ title: "Align left",
+ conditional: true,
+ },
+ imageAlignRight: {
+ icon: "alignRight",
+ title: "Align right",
+ conditional: true,
+ },
+ imageProperties: {
+ icon: "more",
+ title: "Image properties",
+ conditional: true,
+ },
};
export function getToolDefinition(id: ToolId) {
diff --git a/packages/editor/src/toolbar/toolbar.tsx b/packages/editor/src/toolbar/toolbar.tsx
index 2a88f4af6..4e850b239 100644
--- a/packages/editor/src/toolbar/toolbar.tsx
+++ b/packages/editor/src/toolbar/toolbar.tsx
@@ -51,7 +51,7 @@ export function Toolbar(props: ToolbarProps) {
}, [isMobile, location]);
const tools: ToolbarDefinition = [
- ["insertBlock", "tableSettings"],
+ ["insertBlock", "tableSettings", "imageSettings"],
[
"bold",
"italic",
diff --git a/packages/editor/src/toolbar/tools/image.tsx b/packages/editor/src/toolbar/tools/image.tsx
new file mode 100644
index 000000000..65035ca8b
--- /dev/null
+++ b/packages/editor/src/toolbar/tools/image.tsx
@@ -0,0 +1,148 @@
+import { ToolProps } from "../types";
+import { Editor } from "@tiptap/core";
+import { ToolButton } from "../components/tool-button";
+import { useCallback, useMemo, useRef, useState } from "react";
+import { Flex, Text } from "rebass";
+import { Input } from "@rebass/forms";
+import { Popup } from "../components/popup";
+import { ResponsivePresenter } from "../../components/responsive";
+import { MenuButton, MenuItem } from "../../components/menu/types";
+import {
+ moveColumnLeft as moveColumnLeftAction,
+ moveColumnRight as moveColumnRightAction,
+ moveRowDown as moveRowDownAction,
+ moveRowUp as moveRowUpAction,
+} from "../floatingmenus/table/actions";
+import { MoreTools } from "../components/more-tools";
+import { menuButtonToTool } from "./utils";
+import { getToolDefinition } from "../tool-definitions";
+import { CellProperties as CellPropertiesPopup } from "../popups/cell-properties";
+import { ColorTool } from "./colors";
+import { Counter } from "../components/counter";
+import { useToolbarLocation } from "../stores/toolbar-store";
+import { showPopup } from "../../components/popup-presenter";
+import { ImageProperties as ImagePropertiesPopup } from "../popups/image-properties";
+import {
+ ImageAlignmentOptions,
+ ImageAttributes,
+ ImageSizeOptions,
+} from "../../extensions/image";
+import { findSelectedNode } from "../utils/prosemirror";
+
+export function ImageSettings(props: ToolProps) {
+ const { editor } = props;
+ const isBottom = useToolbarLocation() === "bottom";
+ if (!editor.isActive("image") || !isBottom) return null;
+
+ const image = useMemo(() => findSelectedNode(editor, "image"), []);
+ const { float } = (image?.attrs || {}) as ImageAlignmentOptions &
+ ImageSizeOptions;
+
+ return (
+ <MoreTools
+ {...props}
+ popupId="imageSettings"
+ tools={
+ float
+ ? ["imageAlignLeft", "imageAlignRight", "imageProperties"]
+ : [
+ "imageAlignLeft",
+ "imageAlignCenter",
+ "imageAlignRight",
+ "imageProperties",
+ ]
+ }
+ />
+ );
+}
+
+export function ImageAlignLeft(props: ToolProps) {
+ const { editor } = props;
+ return (
+ <ToolButton
+ {...props}
+ toggled={false}
+ onClick={() =>
+ editor.chain().focus().setImageAlignment({ align: "left" }).run()
+ }
+ />
+ );
+}
+
+export function ImageAlignRight(props: ToolProps) {
+ const { editor } = props;
+ return (
+ <ToolButton
+ {...props}
+ toggled={false}
+ onClick={() =>
+ editor.chain().focus().setImageAlignment({ align: "right" }).run()
+ }
+ />
+ );
+}
+
+export function ImageAlignCenter(props: ToolProps) {
+ const { editor } = props;
+ return (
+ <ToolButton
+ {...props}
+ toggled={false}
+ onClick={() =>
+ editor.chain().focus().setImageAlignment({ align: "center" }).run()
+ }
+ />
+ );
+}
+
+export function ImageProperties(props: ToolProps) {
+ const { editor } = props;
+ const [isOpen, setIsOpen] = useState(false);
+ const buttonRef = useRef<HTMLButtonElement>();
+
+ const image = useMemo(() => findSelectedNode(editor, "image"), []);
+ const { float, align, width, height } = (image?.attrs ||
+ {}) as ImageAlignmentOptions & ImageSizeOptions;
+
+ return (
+ <>
+ <ToolButton
+ buttonRef={buttonRef}
+ toggled={isOpen}
+ {...props}
+ onClick={() => setIsOpen((s) => !s)}
+ />
+
+ <ResponsivePresenter
+ isOpen={isOpen}
+ desktop="menu"
+ mobile="sheet"
+ onClose={() => setIsOpen(false)}
+ blocking
+ focusOnRender={false}
+ position={{
+ target: buttonRef.current || "mouse",
+ align: "start",
+ location: "below",
+ yOffset: 10,
+ isTargetAbsolute: true,
+ }}
+ >
+ <Popup
+ title="Image properties"
+ onClose={() => {
+ setIsOpen(false);
+ }}
+ >
+ <ImagePropertiesPopup
+ editor={editor}
+ height={height}
+ width={width}
+ align={align}
+ float={float}
+ />
+ </Popup>
+ </ResponsivePresenter>
+ </>
+ );
+}
diff --git a/packages/editor/src/toolbar/tools/index.ts b/packages/editor/src/toolbar/tools/index.ts
index 2a19185df..41b599dec 100644
--- a/packages/editor/src/toolbar/tools/index.ts
+++ b/packages/editor/src/toolbar/tools/index.ts
@@ -41,6 +41,13 @@ import {
CellTextColor,
CellBorderWidth,
} from "./table";
+import {
+ ImageSettings,
+ ImageAlignCenter,
+ ImageAlignLeft,
+ ImageAlignRight,
+ ImageProperties,
+} from "./image";
export type ToolId = keyof typeof tools;
const tools = {
@@ -68,6 +75,12 @@ const tools = {
textColor: TextColor,
highlight: Highlight,
+ imageSettings: ImageSettings,
+ imageAlignCenter: ImageAlignCenter,
+ imageAlignLeft: ImageAlignLeft,
+ imageAlignRight: ImageAlignRight,
+ imageProperties: ImageProperties,
+
tableSettings: TableSettings,
columnProperties: ColumnProperties,
rowProperties: RowProperties,
diff --git a/packages/editor/src/toolbar/tools/table.tsx b/packages/editor/src/toolbar/tools/table.tsx
index 8e25c6264..0922d04cb 100644
--- a/packages/editor/src/toolbar/tools/table.tsx
+++ b/packages/editor/src/toolbar/tools/table.tsx
@@ -3,8 +3,6 @@ import { Editor } from "@tiptap/core";
import { ToolButton } from "../components/tool-button";
import { useCallback, useMemo, useRef, useState } from "react";
import { Flex, Text } from "rebass";
-import { Input } from "@rebass/forms";
-import { Popup } from "../components/popup";
import { ResponsivePresenter } from "../../components/responsive";
import { MenuButton, MenuItem } from "../../components/menu/types";
import {
@@ -12,7 +10,7 @@ import {
moveColumnRight as moveColumnRightAction,
moveRowDown as moveRowDownAction,
moveRowUp as moveRowUpAction,
-} from "../floatingmenus/table/actions";
+} from "../../extensions/table/actions";
import { MoreTools } from "../components/more-tools";
import { menuButtonToTool } from "./utils";
import { getToolDefinition } from "../tool-definitions";
@@ -48,6 +46,7 @@ export function TableSettings(props: ToolProps) {
export function RowProperties(props: ToolProps) {
const { editor } = props;
+ const buttonRef = useRef<HTMLButtonElement>();
const [isMenuOpen, setIsMenuOpen] = useState(false);
const items = useMemo(
() => [
@@ -64,6 +63,7 @@ export function RowProperties(props: ToolProps) {
<>
<ToolButton
{...props}
+ buttonRef={buttonRef}
toggled={isMenuOpen}
onClick={() => setIsMenuOpen(true)}
/>
@@ -73,7 +73,12 @@ export function RowProperties(props: ToolProps) {
desktop="menu"
isOpen={isMenuOpen}
onClose={() => setIsMenuOpen(false)}
- position={{}}
+ position={{
+ target: buttonRef.current,
+ isTargetAbsolute: true,
+ location: "below",
+ yOffset: 5,
+ }}
items={items}
/>
</>
@@ -82,6 +87,7 @@ export function RowProperties(props: ToolProps) {
export function ColumnProperties(props: ToolProps) {
const { editor } = props;
+ const buttonRef = useRef<HTMLButtonElement>();
const [isMenuOpen, setIsMenuOpen] = useState(false);
const items = useMemo(
@@ -99,6 +105,7 @@ export function ColumnProperties(props: ToolProps) {
<>
<ToolButton
{...props}
+ buttonRef={buttonRef}
toggled={isMenuOpen}
onClick={() => setIsMenuOpen(true)}
/>
@@ -108,7 +115,12 @@ export function ColumnProperties(props: ToolProps) {
desktop="menu"
isOpen={isMenuOpen}
onClose={() => setIsMenuOpen(false)}
- position={{}}
+ position={{
+ target: buttonRef.current,
+ isTargetAbsolute: true,
+ location: "below",
+ yOffset: 5,
+ }}
items={items}
/>
</>
@@ -117,6 +129,7 @@ export function ColumnProperties(props: ToolProps) {
export function TableProperties(props: ToolProps) {
const { editor } = props;
+ const buttonRef = useRef<HTMLButtonElement>();
const [isMenuOpen, setIsMenuOpen] = useState(false);
const items = useMemo<MenuItem[]>(
@@ -140,6 +153,7 @@ export function TableProperties(props: ToolProps) {
<>
<ToolButton
{...props}
+ buttonRef={buttonRef}
toggled={isMenuOpen}
onClick={() => setIsMenuOpen(true)}
/>
@@ -149,7 +163,12 @@ export function TableProperties(props: ToolProps) {
desktop="menu"
isOpen={isMenuOpen}
onClose={() => setIsMenuOpen(false)}
- position={{}}
+ position={{
+ target: buttonRef.current,
+ isTargetAbsolute: true,
+ location: "below",
+ yOffset: 5,
+ }}
items={items}
/>
</>
@@ -276,14 +295,14 @@ const moveColumnLeft = (editor: Editor): MenuButton => ({
...getToolDefinition("moveColumnLeft"),
key: "moveColumnLeft",
type: "button",
- onClick: () => moveColumnLeft(editor),
+ onClick: () => moveColumnLeftAction(editor),
});
const moveColumnRight = (editor: Editor): MenuButton => ({
...getToolDefinition("moveColumnRight"),
key: "moveColumnRight",
type: "button",
- onClick: () => moveColumnRight(editor),
+ onClick: () => moveColumnRightAction(editor),
});
const deleteColumn = (editor: Editor): MenuButton => ({
@@ -325,13 +344,13 @@ const moveRowUp = (editor: Editor): MenuButton => ({
...getToolDefinition("moveRowUp"),
key: "moveRowUp",
type: "button",
- onClick: () => {},
+ onClick: () => moveRowUpAction(editor),
});
const moveRowDown = (editor: Editor): MenuButton => ({
...getToolDefinition("moveRowDown"),
key: "moveRowDown",
type: "button",
- onClick: () => {},
+ onClick: () => moveRowDownAction(editor),
});
const deleteRow = (editor: Editor): MenuButton => ({
diff --git a/packages/editor/src/toolbar/types.ts b/packages/editor/src/toolbar/types.ts
index 2325228ab..ce9ec9613 100644
--- a/packages/editor/src/toolbar/types.ts
+++ b/packages/editor/src/toolbar/types.ts
@@ -2,9 +2,10 @@ import { Editor } from "@tiptap/core";
import { IconNames } from "./icons";
import { ToolId } from "./tools";
+export type ToolButtonVariant = "small" | "normal";
export type ToolProps = ToolDefinition & {
editor: Editor;
- variant?: "small" | "normal";
+ variant?: ToolButtonVariant;
};
export type ToolDefinition = {
diff --git a/packages/editor/src/toolbar/utils/dom.ts b/packages/editor/src/toolbar/utils/dom.ts
index ad87837e4..d3285e623 100644
--- a/packages/editor/src/toolbar/utils/dom.ts
+++ b/packages/editor/src/toolbar/utils/dom.ts
@@ -9,3 +9,13 @@ export function getPopupContainer() {
(document.getElementById("popup-container") as HTMLElement) || undefined
);
}
+
+export function getEditorContainer() {
+ return (document.querySelector(".editor") ||
+ getPopupContainer()) as HTMLElement;
+}
+
+export function getEditorDOM() {
+ return (document.querySelector(".ProseMirror") ||
+ getEditorContainer()) as HTMLElement; // ProseMirror
+}
diff --git a/packages/editor/src/toolbar/utils/prosemirror.ts b/packages/editor/src/toolbar/utils/prosemirror.ts
new file mode 100644
index 000000000..4b2173ab1
--- /dev/null
+++ b/packages/editor/src/toolbar/utils/prosemirror.ts
@@ -0,0 +1,39 @@
+import {
+ Editor,
+ findParentNode,
+ findParentNodeClosestToPos,
+ isNodeSelection,
+} from "@tiptap/core";
+import { Node } from "prosemirror-model";
+
+export function findSelectedDOMNode(
+ editor: Editor,
+ types: string[]
+): HTMLElement | null {
+ const { $anchor } = editor.state.selection;
+
+ const selectedNode = editor.state.doc.nodeAt($anchor.pos);
+ const pos = types.includes(selectedNode?.type.name || "")
+ ? $anchor.pos
+ : findParentNode((node) => types.includes(node.type.name))(
+ editor.state.selection
+ )?.pos;
+ if (!pos) return null;
+
+ return (editor.view.nodeDOM(pos) as HTMLElement) || null;
+}
+
+export function findSelectedNode(editor: Editor, type: string): Node | null {
+ const { $anchor } = editor.state.selection;
+
+ const selectedNode = editor.state.doc.nodeAt($anchor.pos);
+ const pos =
+ selectedNode?.type.name === type
+ ? $anchor.pos
+ : findParentNode((node) => node.type.name === type)(
+ editor.state.selection
+ )?.pos;
+ if (!pos) return null;
+
+ return editor.state.doc.nodeAt(pos);
+}
diff --git a/packages/editor/src/utils/position.ts b/packages/editor/src/utils/position.ts
index 392534554..a20a0d931 100644
--- a/packages/editor/src/utils/position.ts
+++ b/packages/editor/src/utils/position.ts
@@ -62,10 +62,9 @@ export function getPosition(
position.left = x;
}
- if (width) {
- if (location === "right") position.left += width;
- else if (location === "left") position.left -= elementWidth;
- }
+ if (width && location === "right") {
+ position.left += width;
+ } else if (location === "left") position.left -= elementWidth;
if (actualY + elementHeight > windowHeight) {
position.top = windowHeight - elementHeight;
@@ -91,11 +90,11 @@ export function getPosition(
if (yAnchor) {
const anchorY = getElementPosition(yAnchor, isTargetAbsolute);
- position.top = anchorY.actualY - elementHeight;
+ position.top = anchorY.y - elementHeight;
}
- position.top = position.top < 0 ? 0 : position.top;
- position.left = position.left < 0 ? 0 : position.left;
+ position.top = isTargetAbsolute && position.top < 0 ? 0 : position.top;
+ position.left = isTargetAbsolute && position.left < 0 ? 0 : position.left;
position.top += location === "below" ? yOffset : -yOffset;
position.left += xOffset;