Skip to content

Commit

Permalink
wip: Selection & content insert
Browse files Browse the repository at this point in the history
  • Loading branch information
areknawo committed May 17, 2024
1 parent 54947ec commit 64857f6
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 309 deletions.
3 changes: 3 additions & 0 deletions apps/web/public/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,9 @@
const parts = path.split(".");
const getVal = () => {
return parts.slice(1).reduce((currentVal, part, index) => {
if (typeof currentVal !== "function") {
console.log(parts, part, currentVal, env.data);
}
const value = currentVal();
if (typeof value !== "object" || Array.isArray(value) || value === null) {
throw new Error(`Cannot use ${path} in this context`);
Expand Down
4 changes: 4 additions & 0 deletions apps/web/scripts/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ type SerializedContext<C extends ExtensionBaseContext> = Omit<
const parts = path.split(".");
const getVal = (): Val => {
return parts.slice(1).reduce((currentVal, part, index) => {
if (typeof currentVal !== "function") {
console.log(parts, part, currentVal, env!.data);
}

const value = currentVal();

if (typeof value !== "object" || Array.isArray(value) || value === null) {
Expand Down
108 changes: 0 additions & 108 deletions apps/web/src/lib/editor/extensions/custom-node-menu/component.tsx

This file was deleted.

This file was deleted.

123 changes: 0 additions & 123 deletions apps/web/src/lib/editor/extensions/custom-node-menu/plugin.tsx

This file was deleted.

70 changes: 70 additions & 0 deletions apps/web/src/lib/editor/extensions/element/custom-node-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { SolidEditor, SolidRenderer } from "@vrite/tiptap-solid";
import { ExtensionElementViewContext, ExtensionElement } from "@vrite/sdk/extensions";
import { NodeView as PMNodeView } from "@tiptap/pm/view";
import clsx from "clsx";
import { render } from "solid-js/web";
import { mdiCursorText, mdiFormatParagraph, mdiPlus } from "@mdi/js";
import { TextSelection } from "@tiptap/pm/state";
import { useNotifications } from "#context";
import { ExtensionDetails, ExtensionViewRenderer } from "#lib/extensions";
import { IconButton } from "#components/primitives";

const customSubTrees = new Map<string, Promise<ExtensionElement | null>>();
const customNodeView = ({
Expand Down Expand Up @@ -76,10 +80,76 @@ const customNodeView = ({
wrapper.setAttribute("data-initialized", "true");
wrapper.append(component.element);

const pos = typeof props.getPos === "function" ? props.getPos() : null;
const { node } = props;

if (typeof pos === "number" && pos <= editor.view.state.doc.nodeSize) {
const res = editor.commands.command(({ tr, dispatch }) => {
if (!dispatch) return false;

if (typeof pos !== "number" || pos > editor.view.state.doc.nodeSize) {
return false;
}

if (node && node.type.name === "element") {
tr.setMeta("addToHistory", false)
.setMeta("customView", true)
.setNodeMarkup(pos, node.type, {
...node.attrs,
_: { uid }
});
}

return true;
});
}

if (top) {
wrapper.setAttribute("data-custom-node-view", "true");
}

if (!node.content.size && contentWrapperParent) {
const button = document.createElement("div");

button.setAttribute("contentEditable", "false");
button.setAttribute("class", "min-h-[35px] flex items-center");
render(
() => (
<IconButton
path={mdiPlus}
label="Add content"
class="m-0 pr-1"
text="soft"
variant="text"
size="small"
onPointerDown={(event) => {
editor
.chain()
.command(({ tr }) => {
if (typeof props.getPos !== "function") return false;

const lastPos = props.getPos();

tr.replaceWith(
lastPos + 1,
lastPos + 1,
editor.schema.node("paragraph")
).setSelection(TextSelection.create(tr.doc, lastPos + 1));

return true;
})
.focus()
.run();
event.preventDefault();
event.stopPropagation();
}}
/>
),
button
);
contentWrapperParent.append(button);
}

return {
selectNode() {
wrapper.classList.add("ring", "ring-primary", "ring-2");
Expand Down
Loading

0 comments on commit 64857f6

Please sign in to comment.