Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Hiros document desires (#63035)

Nick Fitzpatrick20afc0846838
diff --git a/e2e/support/helpers/e2e-ui-elements-helpers.js b/e2e/support/helpers/e2e-ui-elements-helpers.js
index d65bcd3be48..037ce70807d 100644
--- a/e2e/support/helpers/e2e-ui-elements-helpers.js
+++ b/e2e/support/helpers/e2e-ui-elements-helpers.js
@@ -56,7 +56,7 @@ export function entityPickerModalLevel(level) {
/**
*
* @param {number} level
- * @param {string} name
+ * @param {string | RegExp} name
*/
export function entityPickerModalItem(level, name) {
return entityPickerModalLevel(level).findByText(name).parents("a");
diff --git a/e2e/test/scenarios/documents/documents.cy.spec.ts b/e2e/test/scenarios/documents/documents.cy.spec.ts
index 89cdd6440ea..f64b9bfae38 100644
--- a/e2e/test/scenarios/documents/documents.cy.spec.ts
+++ b/e2e/test/scenarios/documents/documents.cy.spec.ts
@@ -112,9 +112,36 @@ H.describeWithSnowplowEE("documents", () => {
H.openCollectionItemMenu("Test Document");
- H.popover().findByText("Move to trash").click();
+ H.popover().findByText("Duplicate").click();
+ cy.findByRole("heading", { name: 'Duplicate "Test Document"' }).should(
+ "exist",
+ );
+ cy.findByTestId("collection-picker-button").click();
+ H.entityPickerModalTab("Collections").click();
+ H.entityPickerModalItem(0, /Personal Collection/).click();
+ H.entityPickerModal().findByRole("button", { name: "Select" }).click();
+ H.modal().findByRole("button", { name: "Copy" }).click();
H.openNavigationSidebar();
+ H.navigationSidebar().findByText("Your personal collection").click();
+
+ cy.findByTestId("collection-table")
+ .findByText("Test Document - Duplicate")
+ .click();
+
+ cy.findByRole("textbox", { name: "Document Title" }).should(
+ "have.value",
+ "Test Document - Duplicate",
+ );
+
+ H.documentContent().should("contain.text", "This is a paragraph");
+
+ H.openNavigationSidebar();
+ H.navigationSidebar().findByText("Our analytics").click();
+
+ H.openCollectionItemMenu("Test Document");
+
+ H.popover().findByText("Move to trash").click();
// Force the click since this is hidden behind a toast notification
H.navigationSidebar().findByText("Trash").click({ force: true });
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentCopyForm/DocumentCopyForm.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentCopyForm/DocumentCopyForm.tsx
new file mode 100644
index 00000000000..f631df06103
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentCopyForm/DocumentCopyForm.tsx
@@ -0,0 +1,79 @@
+import { t } from "ttag";
+import * as Yup from "yup";
+
+import FormCollectionPicker from "metabase/collections/containers/FormCollectionPicker";
+import { FormFooter } from "metabase/common/components/FormFooter";
+import {
+ Form,
+ FormProvider,
+ FormSubmitButton,
+ FormTextInput,
+} from "metabase/forms";
+import { Button, Stack } from "metabase/ui";
+import type { CollectionId } from "metabase-types/api";
+
+type CopyDocumentProperties = {
+ collection_id: CollectionId | null;
+ name: string;
+};
+
+type DocumentCopyFormProps = {
+ initialValues: Partial<CopyDocumentProperties>;
+ onCancel: () => void;
+ onSubmit: (vals: CopyDocumentProperties) => Promise<Document>;
+ onSaved: (doc: Document) => void;
+};
+
+const VALIDATION_SCHEMA = Yup.object({
+ name: Yup.string().required().default(""),
+ collection_id: Yup.number().nullable().default(null),
+});
+
+export const DocumentCopyForm = ({
+ initialValues,
+ onCancel,
+ onSubmit,
+ onSaved,
+}: DocumentCopyFormProps) => {
+ const handleSubmit = async (vals: CopyDocumentProperties) => {
+ const { name, collection_id } = vals;
+
+ const newDoc = await onSubmit({ name, collection_id });
+
+ onSaved?.(newDoc);
+ };
+
+ return (
+ <FormProvider
+ initialValues={{ ...VALIDATION_SCHEMA.getDefault(), ...initialValues }}
+ validationSchema={VALIDATION_SCHEMA}
+ onSubmit={handleSubmit}
+ enableReinitialize
+ >
+ {() => (
+ <Form>
+ <Stack gap="md" mb="md">
+ <FormTextInput
+ name="name"
+ label={t`Name`}
+ placeholder={t`What is the name of your dashboard?`}
+ autoFocus
+ />
+ <div>
+ <FormCollectionPicker
+ name="collection_id"
+ title={t`Folder this document should be copied to`}
+ />
+ </div>
+ </Stack>
+ <FormFooter>
+ {!!onCancel && (
+ <Button type="button" onClick={onCancel}>{t`Cancel`}</Button>
+ )}
+ <FormSubmitButton label={t`Copy`} variant="primary" />
+ </FormFooter>
+ </Form>
+ )}
+ </FormProvider>
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/index.ts b/enterprise/frontend/src/metabase-enterprise/documents/index.ts
index 7c6b72a5e49..b3625dcbf02 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/index.ts
+++ b/enterprise/frontend/src/metabase-enterprise/documents/index.ts
@@ -6,6 +6,7 @@ import {
import Documents from "metabase-enterprise/entities/document";
import { hasPremiumFeature } from "metabase-enterprise/settings";
+import { DocumentCopyForm } from "./components/DocumentCopyForm/DocumentCopyForm";
import { documentsReducer } from "./documents.slice";
import { getRoutes } from "./routes";
import {
@@ -20,6 +21,7 @@ if (hasPremiumFeature("documents")) {
PLUGIN_DOCUMENTS.getCurrentDocument = getCurrentDocument;
PLUGIN_DOCUMENTS.getSidebarOpen = getSidebarOpen;
PLUGIN_DOCUMENTS.getCommentSidebarOpen = getCommentSidebarOpen;
+ PLUGIN_DOCUMENTS.DocumentCopyForm = DocumentCopyForm;
PLUGIN_REDUCERS.documents = documentsReducer;
diff --git a/enterprise/frontend/src/metabase-enterprise/entities/document.ts b/enterprise/frontend/src/metabase-enterprise/entities/document.ts
index 30d727ae8bb..7d3c0d108ea 100644
--- a/enterprise/frontend/src/metabase-enterprise/entities/document.ts
+++ b/enterprise/frontend/src/metabase-enterprise/entities/document.ts
@@ -15,6 +15,7 @@ import { DocumentSchema } from "metabase/schema";
import { documentApi, useGetDocumentQuery } from "metabase-enterprise/api";
import type {
Collection,
+ CollectionId,
CreateDocumentRequest,
DeleteDocumentRequest,
Document,
@@ -103,6 +104,23 @@ const Documents = createEntity({
typeof pinned === "number" ? pinned : pinned ? 1 : null,
},
),
+ copy:
+ (
+ { id }: Document,
+ overrides: { name: string; collection_id: CollectionId },
+ ) =>
+ async (dispatch: Dispatch) => {
+ const data = (await dispatch(
+ documentApi.endpoints.getDocument.initiate({ id }),
+ )) as { data: Document };
+
+ await dispatch(
+ documentApi.endpoints.createDocument.initiate({
+ document: data.data.document,
+ ...overrides,
+ }),
+ );
+ },
},
objectSelectors: {
diff --git a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/CardEmbed/CardEmbedNode.tsx b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/CardEmbed/CardEmbedNode.tsx
index c54ec9ab37c..0e5ea85aef0 100644
--- a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/CardEmbed/CardEmbedNode.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/CardEmbed/CardEmbedNode.tsx
@@ -136,7 +136,14 @@ export const CardEmbed: Node<{
});
export const CardEmbedComponent = memo(
- ({ node, updateAttributes, selected, editor, getPos }: NodeViewProps) => {
+ ({
+ node,
+ updateAttributes,
+ selected,
+ editor,
+ getPos,
+ deleteNode,
+ }: NodeViewProps) => {
const childTargetId = useSelector(getChildTargetId);
const hoveredChildTargetId = useSelector(getHoveredChildTargetId);
const document = useSelector(getCurrentDocument);
@@ -271,6 +278,11 @@ export const CardEmbedComponent = memo(
[updateAttributes, document],
);
+ const handleRemoveNode = useCallback(() => {
+ deleteNode();
+ editor.chain().focus();
+ }, [deleteNode, editor]);
+
// Handle drill-through navigation
const handleChangeCardAndRun = useCallback(
({
@@ -491,6 +503,13 @@ export const CardEmbedComponent = memo(
>
{t`Replace`}
</Menu.Item>
+ <Menu.Item
+ onClick={handleRemoveNode}
+ disabled={!canWrite}
+ leftSection={<Icon name="trash" size={14} />}
+ >
+ {t`Remove Chart`}
+ </Menu.Item>
</Menu.Dropdown>
</Menu>
)}
diff --git a/frontend/src/metabase/entities/containers/EntityCopyModal.tsx b/frontend/src/metabase/entities/containers/EntityCopyModal.tsx
index 968bc81e389..b9ed05627d6 100644
--- a/frontend/src/metabase/entities/containers/EntityCopyModal.tsx
+++ b/frontend/src/metabase/entities/containers/EntityCopyModal.tsx
@@ -7,6 +7,7 @@ import {
CopyDashboardFormConnected,
type CopyDashboardFormProperties,
} from "metabase/dashboard/containers/CopyDashboardForm";
+import { PLUGIN_DOCUMENTS } from "metabase/plugins";
import { CopyQuestionForm } from "metabase/questions/components/CopyQuestionForm";
interface EntityCopyModalProps {
@@ -72,6 +73,16 @@ const EntityCopyModal = ({
{...props}
/>
)}
+ {entityType === "documents" && (
+ <PLUGIN_DOCUMENTS.DocumentCopyForm
+ onSubmit={copy}
+ onCancel={onClose}
+ onSaved={onSaved}
+ initialValues={initialValues}
+ model={entityObject?.type}
+ {...props}
+ />
+ )}
</ModalContent>
);
};
diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.ts
index 0115cb49aee..3223b9bcdfe 100644
--- a/frontend/src/metabase/plugins/index.ts
+++ b/frontend/src/metabase/plugins/index.ts
@@ -818,6 +818,7 @@ export const PLUGIN_DOCUMENTS = {
getCurrentDocument: (_state: any) => null as Document | null,
getSidebarOpen: (_state: any) => false,
getCommentSidebarOpen: (_state: any) => false,
+ DocumentCopyForm: (_props: any) => null as React.ReactElement | null,
};
export const PLUGIN_ENTITIES = {