Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve the issue of text editing not being effective before the table #5365

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 34 additions & 49 deletions ui/packages/editor/src/extensions/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import {
type NodeView,
type EditorState,
type DOMOutputSpec,
Plugin,
DecorationSet,
Decoration,
} from "@/tiptap/pm";
import TableCell from "./table-cell";
import TableRow from "./table-row";
Expand Down Expand Up @@ -129,14 +126,22 @@ class TableView implements NodeView {
this.containerDOM.addEventListener("wheel", (e) => {
return this.handleHorizontalWheel(this.containerDOM, e);
});

let mouseX = 0;
let mouseY = 0;
document.addEventListener("mousemove", function (event) {
mouseX = event.clientX;
mouseY = event.clientY;
});

this.containerDOM.addEventListener("scroll", () => {
if (!editor) {
return false;
}
const { state, view } = editor;
const { tr } = state;
view.dispatch(tr);
return false;
const { view } = editor;
const coords = { left: mouseX, top: mouseY };
const pos = view.posAtCoords(coords);
editor.commands.setTextSelection(pos?.pos || 0);
});

this.scrollDom = document.createElement("div");
Expand All @@ -147,6 +152,11 @@ class TableView implements NodeView {
this.colgroup = this.table.appendChild(document.createElement("colgroup"));
updateColumns(node, this.colgroup, this.table, cellMinWidth);
this.contentDOM = this.table.appendChild(document.createElement("tbody"));
// delay execution during initialization, otherwise
// the correct scrollWidth cannot be obtained.
setTimeout(() => {
this.updateTableShadow();
});
}

update(node: ProseMirrorNode) {
Expand All @@ -156,15 +166,32 @@ class TableView implements NodeView {

this.node = node;
updateColumns(node, this.colgroup, this.table, this.cellMinWidth);
this.updateTableShadow();
return true;
}

updateTableShadow() {
const { scrollWidth, clientWidth, scrollLeft } = this
.containerDOM as HTMLElement;
if (scrollWidth > clientWidth && scrollLeft < scrollWidth - clientWidth) {
this.dom.classList.add("table-right-shadow");
} else {
this.dom.classList.remove("table-right-shadow");
}
if (scrollLeft > 0) {
this.dom.classList.add("table-left-shadow");
} else {
this.dom.classList.remove("table-left-shadow");
}
}

ignoreMutation(
mutation: MutationRecord | { type: "selection"; target: Element }
) {
return (
mutation.type === "attributes" &&
(mutation.target === this.table ||
mutation.target === this.dom ||
this.colgroup.contains(mutation.target))
);
}
Expand Down Expand Up @@ -487,48 +514,6 @@ const Table = TiptapTable.extend<ExtensionOptions & TableOptions>({
onTransaction() {
editor = this.editor;
},

addProseMirrorPlugins() {
const plugins = this.parent?.() ?? [];
return [
...plugins,
new Plugin({
props: {
decorations: (state) => {
const { doc, tr } = state;
const decorations: Decoration[] = [];
doc.descendants((node, pos) => {
if (node.type.name === Table.name) {
const { view } = this.editor;
const nodeDom = view.nodeDOM(pos) || view.domAtPos(pos)?.node;
if (!nodeDom) {
return true;
}
const { scrollWidth, clientWidth, scrollLeft } =
nodeDom.firstChild as HTMLElement;
let classNames = "";
if (
scrollWidth > clientWidth &&
scrollLeft < scrollWidth - clientWidth
) {
classNames += "table-right-shadow ";
}
if (scrollLeft > 0) {
classNames += "table-left-shadow ";
}
decorations.push(
Decoration.node(pos, pos + node.nodeSize, {
class: classNames,
})
);
}
});
return DecorationSet.create(tr.doc, decorations);
},
},
}),
];
},
}).configure({ resizable: true });

export default Table;
Loading