Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

WIP variables

Tim Wilsonc4d01083c5c0
diff --git a/apps/web/app/(ee)/app.dub.co/(program-email)/[slug]/program/emails/[emailId]/page-client.tsx b/apps/web/app/(ee)/app.dub.co/(program-email)/[slug]/program/emails/[emailId]/page-client.tsx
index 21c27a19df..d461051be9 100644
--- a/apps/web/app/(ee)/app.dub.co/(program-email)/[slug]/program/emails/[emailId]/page-client.tsx
+++ b/apps/web/app/(ee)/app.dub.co/(program-email)/[slug]/program/emails/[emailId]/page-client.tsx
@@ -107,6 +107,7 @@ function ProgramEmailForm() {
editorClassName="-m-2 min-h-[200px] p-2"
initialValue={field.value}
onChange={field.onChange}
+ variables={["PartnerName", "PartnerEmail"]}
uploadImage={async (file) => {
try {
const result = await executeImageUpload({
diff --git a/packages/ui/package.json b/packages/ui/package.json
index 365daf3ba4..ae371b1663 100644
--- a/packages/ui/package.json
+++ b/packages/ui/package.json
@@ -54,6 +54,7 @@
"typescript": "^5.1.6"
},
"dependencies": {
+ "@floating-ui/dom": "^1.6.12",
"@floating-ui/react": "^0.26.20",
"@internationalized/date": "^3.5.3",
"@radix-ui/react-accordion": "^1.2.2",
@@ -73,6 +74,7 @@
"@tanstack/react-table": "^8.17.3",
"@tiptap/extension-file-handler": "^3.0.9",
"@tiptap/extension-image": "^3.0.9",
+ "@tiptap/extension-mention": "^3.0.9",
"@tiptap/extensions": "^3.0.9",
"@tiptap/pm": "^3.0.9",
"@tiptap/react": "^3.0.9",
diff --git a/packages/ui/src/rich-text-area/index.tsx b/packages/ui/src/rich-text-area/index.tsx
index 963217291b..b53e505476 100644
--- a/packages/ui/src/rich-text-area/index.tsx
+++ b/packages/ui/src/rich-text-area/index.tsx
@@ -1,12 +1,14 @@
import { cn } from "@dub/utils";
import FileHandler from "@tiptap/extension-file-handler";
import Image from "@tiptap/extension-image";
+import Mention from "@tiptap/extension-mention";
import { Placeholder } from "@tiptap/extensions";
import { Editor, EditorContent, EditorContext, useEditor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
import { useMemo, useState } from "react";
import { LoadingSpinner } from "../icons";
import { RichTextToolbar } from "./rich-text-toolbar";
+import { suggestions } from "./variables";
export function RichTextArea({
initialValue,
@@ -15,6 +17,7 @@ export function RichTextArea({
className,
editorClassName,
uploadImage,
+ variables,
}: {
initialValue?: string;
onChange?: (value: string) => void;
@@ -22,6 +25,7 @@ export function RichTextArea({
className?: string;
editorClassName?: string;
uploadImage?: (file: File) => Promise<string | null>;
+ variables?: string[];
}) {
const [isUploading, setIsUploading] = useState(false);
@@ -94,6 +98,17 @@ export function RichTextArea({
}),
]
: []),
+ ...(variables
+ ? [
+ Mention.configure({
+ HTMLAttributes: {
+ class:
+ "px-1 py-0.5 bg-blue-100 text-blue-700 rounded font-semibold",
+ },
+ suggestion: suggestions(variables),
+ }),
+ ]
+ : []),
],
editorProps: {
attributes: {
diff --git a/packages/ui/src/rich-text-area/variables.tsx b/packages/ui/src/rich-text-area/variables.tsx
new file mode 100644
index 0000000000..58445642e4
--- /dev/null
+++ b/packages/ui/src/rich-text-area/variables.tsx
@@ -0,0 +1,112 @@
+import { cn } from "@dub/utils";
+import { computePosition, flip, shift } from "@floating-ui/dom";
+import { Editor, posToDOMRect, ReactRenderer } from "@tiptap/react";
+import { Command } from "cmdk";
+
+const updatePosition = (editor: Editor, element: HTMLElement) => {
+ const virtualElement = {
+ getBoundingClientRect: () =>
+ posToDOMRect(
+ editor.view,
+ editor.state.selection.from,
+ editor.state.selection.to,
+ ),
+ };
+
+ computePosition(virtualElement, element, {
+ placement: "bottom-start",
+ strategy: "absolute",
+ middleware: [shift(), flip()],
+ }).then(({ x, y, strategy }) => {
+ element.style.width = "max-content";
+ element.style.position = strategy;
+ element.style.left = `${x}px`;
+ element.style.top = `${y}px`;
+ });
+};
+
+export const suggestions = (variables: string[]) => ({
+ items: ({ query }: { query: string }) =>
+ variables
+ .filter((item) => item.toLowerCase().startsWith(query.toLowerCase()))
+ .slice(0, 5),
+
+ render: () => {
+ let component: any;
+
+ return {
+ onStart: (props: {
+ editor: Editor;
+ clientRect?: (() => DOMRect | null) | null;
+ command?: (props: any) => void;
+ }) => {
+ component = new ReactRenderer(Menu, {
+ props,
+ editor: props.editor,
+ });
+
+ if (!props.clientRect) return;
+
+ component.element.style.position = "absolute";
+
+ document.body.appendChild(component.element);
+
+ updatePosition(props.editor, component.element);
+ },
+
+ onUpdate(props: any) {
+ component.updateProps(props);
+
+ if (!props.clientRect) return;
+
+ updatePosition(props.editor, component.element);
+ },
+
+ onKeyDown(props: any) {
+ if (props.event.key === "Escape") {
+ component.destroy();
+
+ return true;
+ }
+
+ return component.ref?.onKeyDown(props);
+ },
+
+ onExit() {
+ component.element.remove();
+ component.destroy();
+ },
+ };
+ },
+});
+
+function Menu({
+ items,
+ command,
+}: {
+ items: string[];
+ command: (props: any) => void;
+}) {
+ return (
+ <Command tabIndex={0} loop className="focus:outline-none">
+ <Command.List className="border-border-subtle flex flex-col rounded-lg border bg-white p-1 shadow-sm">
+ {items.length ? (
+ items.map((item, index) => (
+ <Command.Item
+ key={index}
+ onSelect={() => command({ id: item })}
+ className={cn(
+ "flex cursor-pointer select-none items-center gap-2 whitespace-nowrap rounded-md px-2 py-1 font-mono text-sm text-neutral-950",
+ "data-[selected=true]:bg-neutral-100",
+ )}
+ >
+ {item}
+ </Command.Item>
+ ))
+ ) : (
+ <div className="item">No result</div>
+ )}
+ </Command.List>
+ </Command>
+ );
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 492a7df30e..afb1b369c3 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -662,6 +662,9 @@ importers:
packages/ui:
dependencies:
+ '@floating-ui/dom':
+ specifier: ^1.6.12
+ version: 1.6.12
'@floating-ui/react':
specifier: ^0.26.20
@@ -719,6 +722,9 @@ importers:
'@tiptap/extension-image':
specifier: ^3.0.9
version: 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-mention':
+ specifier: ^3.0.9
+ version: 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
'@tiptap/extensions':
specifier: ^3.0.9
version: 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
@@ -4544,6 +4550,13 @@ packages:
'@tiptap/core': ^3.0.9
'@tiptap/pm': ^3.0.9
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-DTQNAQkHZ+7Enlt3KvjqN6eECINlqPpET4Drzwj8Mmz9kMILc87cz3G2cwEKRrS9A1Xn3H3VpWvElWE2Wq9JHw==}
+ peerDependencies:
+ '@tiptap/core': ^3.0.9
+ '@tiptap/pm': ^3.0.9
+ '@tiptap/suggestion': ^3.0.9
+
'@tiptap/[email protected]':
resolution: {integrity: sha512-ACubdGc/y/rKPEgHTO7hDSg547wRRA+Es7c/rQgjrkpI///LBJQfixyUvNg2UNNPttNsavF/CUwhshCeo9MeBA==}
peerDependencies:
@@ -4594,6 +4607,12 @@ packages:
'@tiptap/[email protected]':
resolution: {integrity: sha512-CYg6tV5fYOvkP1gyATkJJj+nFYmwjDKLipQc/r0D/tHKypxefENrm4G7mf4B78zsB/izfk5mW3iujvyeod6EcQ==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-irthqfUybezo3IwR6AXvyyTOtkzwfvvst58VXZtTnR1nN6NEcrs3TQoY3bGKGbN83bdiquKh6aU2nLnZfAhoXg==}
+ peerDependencies:
+ '@tiptap/core': ^3.0.9
+ '@tiptap/pm': ^3.0.9
+
'@tokenizer/[email protected]':
resolution: {integrity: sha512-MADQgmZT1eKjp06jpI2yozxaU9uVs4GzzgSL+uEq7bVcJ9V1ZXQkeGNql1fsSI0gMy1vhvNTNbUqrx+pZfJVmg==}
engines: {node: '>=18'}
@@ -15431,6 +15450,12 @@ snapshots:
'@tiptap/core': 3.0.9(@tiptap/[email protected])
'@tiptap/pm': 3.0.9
+ dependencies:
+ '@tiptap/core': 3.0.9(@tiptap/[email protected])
+ '@tiptap/pm': 3.0.9
+ '@tiptap/suggestion': 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+
dependencies:
'@tiptap/extension-list': 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
@@ -15523,6 +15548,11 @@ snapshots:
'@tiptap/extensions': 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/pm': 3.0.9
+ dependencies:
+ '@tiptap/core': 3.0.9(@tiptap/[email protected])
+ '@tiptap/pm': 3.0.9
+
'@tokenizer/[email protected]':
dependencies:
debug: 4.4.0