Skip to content

Commit

Permalink
feat(contributions): ajouter des infographies
Browse files Browse the repository at this point in the history
# Conflicts:
#	targets/frontend/src/components/forms/EditionField/MenuSpecial.tsx

# Conflicts:
#	targets/frontend/src/components/forms/EditionField/Editor.tsx
#	targets/frontend/src/components/forms/EditionField/extensions/Infographic.ts
  • Loading branch information
m-maillot committed Feb 6, 2025
1 parent efd31ba commit 3efe729
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 55 deletions.
3 changes: 0 additions & 3 deletions .kontinuous/env/dev/templates/www.configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ data:
MATOMO_URL: "https://matomo.fabrique.social.gouv.fr/"
NODE_ENV: "production"
URL_EXPORT: "http://export"
BUCKET_PUBLIC_ENDPOINT: "https://cdtn-dev-public.s3.gra.io.cloud.ovh.net"
BUCKET_DEFAULT_FOLDER: "default"
BUCKET_DRAFT_FOLDER: "draft"
3 changes: 3 additions & 0 deletions .kontinuous/env/dev/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ jobs:
with:
buildArgs:
NEXT_PUBLIC_BASE_PATH: https://www-{{.Values.global.host}}
BUCKET_PUBLIC_ENDPOINT: "https://cdtn-dev-public.s3.gra.io.cloud.ovh.net"
BUCKET_DEFAULT_FOLDER: "default"
BUCKET_DRAFT_FOLDER: "draft"
post-restore:
~needs: [ pg, hasura ]
use: psql
Expand Down
3 changes: 0 additions & 3 deletions .kontinuous/env/preprod/templates/www.configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ data:
MATOMO_URL: "https://matomo.fabrique.social.gouv.fr/"
NODE_ENV: "production"
URL_EXPORT: "http://export"
BUCKET_PUBLIC_ENDPOINT: "https://cdtn-dev-public.s3.gra.io.cloud.ovh.net"
BUCKET_DEFAULT_FOLDER: "default"
BUCKET_DRAFT_FOLDER: "draft"
3 changes: 3 additions & 0 deletions .kontinuous/env/preprod/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ jobs:
with:
buildArgs:
NEXT_PUBLIC_BASE_PATH: https://cdtn-admin-preprod.ovh.fabrique.social.gouv.fr
NEXT_PUBLIC_BUCKET_PUBLIC_ENDPOINT: "https://cdtn-dev-public.s3.gra.io.cloud.ovh.net"
NEXT_PUBLIC_BUCKET_DEFAULT_FOLDER: "default"
NEXT_PUBLIC_BUCKET_DRAFT_FOLDER: "draft"
post-restore:
~needs: [ pg, hasura ]
use: psql
Expand Down
3 changes: 0 additions & 3 deletions .kontinuous/env/prod/templates/www.configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ data:
NODE_ENV: "production"
PRODUCTION: "true"
URL_EXPORT: "http://export"
BUCKET_PUBLIC_ENDPOINT: "https://cdtn-prod-public.s3.gra.io.cloud.ovh.net"
BUCKET_DEFAULT_FOLDER: "default"
BUCKET_DRAFT_FOLDER: "draft"
3 changes: 3 additions & 0 deletions .kontinuous/env/prod/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ jobs:
with:
buildArgs:
NEXT_PUBLIC_BASE_PATH: https://cdtn-admin.fabrique.social.gouv.fr
NEXT_PUBLIC_BUCKET_PUBLIC_ENDPOINT: "https://cdtn-prod-public.s3.gra.io.cloud.ovh.net"
NEXT_PUBLIC_BUCKET_DEFAULT_FOLDER: "default"
NEXT_PUBLIC_BUCKET_DRAFT_FOLDER: "draft"

www:
autoscale:
Expand Down
4 changes: 2 additions & 2 deletions targets/export-elasticsearch/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ rootContainer
.toConstantValue(process.env.BUCKET_SECRET_KEY ?? "");
rootContainer
.bind<string>(S3Parameters.BUCKET_DRAFT_FOLDER)
.toConstantValue(process.env.BUCKET_DRAFT_FOLDER ?? `draft`);
.toConstantValue(process.env.NEXT_PUBLIC_BUCKET_DRAFT_FOLDER ?? `draft`);
rootContainer
.bind<string>(S3Parameters.BUCKET_PUBLISHED_FOLDER)
.toConstantValue(process.env.BUCKET_PUBLISHED_FOLDER ?? `published`);
Expand All @@ -53,7 +53,7 @@ rootContainer
.toConstantValue(process.env.BUCKET_PREVIEW_FOLDER ?? `preview`);
rootContainer
.bind<string>(S3Parameters.BUCKET_DEFAULT_FOLDER)
.toConstantValue(process.env.BUCKET_DEFAULT_FOLDER ?? `default`);
.toConstantValue(process.env.NEXT_PUBLIC_BUCKET_DEFAULT_FOLDER ?? `default`);
/* REPOSITORIES */
rootContainer.bind<S3Repository>(getName(S3Repository)).to(S3Repository);
rootContainer
Expand Down
6 changes: 6 additions & 0 deletions targets/frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ FROM deps as dist

ARG NEXT_PUBLIC_BASE_PATH
ENV NEXT_PUBLIC_BASE_PATH=$NEXT_PUBLIC_BASE_PATH
ARG NEXT_PUBLIC_BUCKET_PUBLIC_ENDPOINT
ENV NEXT_PUBLIC_BUCKET_PUBLIC_ENDPOINT=$NEXT_PUBLIC_BUCKET_PUBLIC_ENDPOINT
ARG NEXT_PUBLIC_BUCKET_DEFAULT_FOLDER
ENV NEXT_PUBLIC_BUCKET_DEFAULT_FOLDER=$NEXT_PUBLIC_BUCKET_DEFAULT_FOLDER
ARG NEXT_PUBLIC_BUCKET_DRAFT_FOLDER
ENV NEXT_PUBLIC_BUCKET_DRAFT_FOLDER=$NEXT_PUBLIC_BUCKET_DRAFT_FOLDER

COPY --from=build-types /app/shared/types /app/shared/types
COPY --from=build-utils /app/shared/utils /app/shared/utils
Expand Down
48 changes: 27 additions & 21 deletions targets/frontend/src/components/forms/EditionField/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type EditorProps = {
onUpdate: (content: string) => void;
disabled?: boolean;
isError?: boolean;
infographicBaseUrl: string;
};

const emptyHtml = "<p></p>";
Expand All @@ -47,18 +48,18 @@ const Creation: ModeCreation = { mode: 0 };

type ModeEdition = {
mode: 1;
infoUrl: string;
pdfUrl: string;
infoName: string;
pdfName: string;
pdfSize: string;
};
const Edition = (
infoUrl: string,
pdfUrl: string,
infoName: string,
pdfName: string,
pdfSize: string
): ModeEdition => ({
mode: 1,
infoUrl,
pdfUrl,
infoName,
pdfName,
pdfSize,
});

Expand All @@ -74,6 +75,7 @@ export const Editor = ({
content,
onUpdate,
disabled,
infographicBaseUrl,
isError = false,
}: EditorProps) => {
const [currentContent, setCurrentContent] = useState(content);
Expand Down Expand Up @@ -117,7 +119,9 @@ export const Editor = ({
}),
Alert,
Title,
Infographic,
Infographic.configure({
baseUrl: infographicBaseUrl,
}),
],
onUpdate: ({ editor }) => {
const html = editor.getHTML();
Expand Down Expand Up @@ -194,10 +198,10 @@ export const Editor = ({
onEdit={() => {
const node = editor?.state.selection.$from.node();
if (node?.type.name === "infographic") {
const src = node.attrs.src;
const dataPdf = node.attrs.urlPdf;
const dataInfo = node.attrs.infoName;
const dataPdf = node.attrs.pdfName;
const dataPdfSize = node.attrs.pdfSize;
setInfographicModal(Edition(src, dataPdf, dataPdfSize));
setInfographicModal(Edition(dataInfo, dataPdf, dataPdfSize));
}
}}
onDelete={() => {
Expand Down Expand Up @@ -229,17 +233,17 @@ export const Editor = ({
onSubmit: (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const { infoUrl, pdfUrl, pdfSize } = Object.fromEntries(
const { infoName, pdfName, pdfSize } = Object.fromEntries(
(formData as any).entries()
);
if (infographicModal.mode === Creation.mode) {
editor
?.chain()
.focus()
.setInfographic(infoUrl, pdfUrl, pdfSize)
.setInfographic(infoName, pdfName, pdfSize)
.run();
} else {
editor?.commands.updateInfographicSrc(infoUrl, pdfUrl, pdfSize);
editor?.commands.updateInfographicSrc(infoName, pdfName, pdfSize);
}
setInfographicModal(Hide);
},
Expand All @@ -255,11 +259,13 @@ export const Editor = ({
autoFocus
required
margin="dense"
id="infoUrl"
name="infoUrl"
label="URL vers l'infographie"
id="infoName"
name="infoName"
label="Nom de l'infographie"
defaultValue={
infographicModal.mode === 1 ? infographicModal.infoUrl : undefined
infographicModal.mode === 1
? infographicModal.infoName
: undefined
}
type="text"
fullWidth
Expand All @@ -268,11 +274,11 @@ export const Editor = ({
<TextField
required
margin="dense"
id="pdfUrl"
name="pdfUrl"
label="URL vers le PDF"
id="pdfName"
name="pdfName"
label="Nom du PDF"
defaultValue={
infographicModal.mode === 1 ? infographicModal.pdfUrl : undefined
infographicModal.mode === 1 ? infographicModal.pdfName : undefined
}
type="text"
fullWidth
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { Node } from "@tiptap/core";

export interface InfographicOptions {}
export interface InfographicOptions {
baseUrl: string;
}

declare module "@tiptap/core" {
interface Commands<ReturnType> {
infographic: {
setInfographic: (
src: string,
urlPdf: string,
infoName: string,
pdfName: string,
sizePdf: string
) => ReturnType;
updateInfographicSrc: (
newSrc: string,
newUrlPdf: string,
newSizePdf: string
newInfoName: string,
newPdfName: string,
newPdfSize: string
) => ReturnType;
removeInfographic: () => ReturnType;
};
Expand All @@ -27,23 +29,24 @@ export const Infographic = Node.create<InfographicOptions>({
addOptions() {
return {
HTMLAttributes: {},
baseUrl: "",
};
},

addAttributes() {
return {
src: {
infoName: {
parseHTML: (element) =>
element.querySelector("img")?.getAttribute("src"),
element.querySelector("img")?.getAttribute("data-infographic"),
renderHTML: (attributes) => {
return { src: attributes.src };
return { "data-infographic": attributes.infoName };
},
},
urlPdf: {
pdfName: {
parseHTML: (element) =>
element.querySelector("div.infographic")?.getAttribute("data-pdf"),
renderHTML: (attributes) => {
return { "data-pdf": attributes.urlPdf };
return { "data-pdf": attributes.pdfName };
},
},
pdfSize: {
Expand All @@ -69,8 +72,8 @@ export const Infographic = Node.create<InfographicOptions>({
getAttrs: (element) => {
const el = element as HTMLElement;
return {
src: el.querySelector("img")?.getAttribute("src") || "",
urlPdf: el.getAttribute("data-pdf") || "",
infoName: el.getAttribute("data-infographic") || "",
pdfName: el.getAttribute("data-pdf") || "",
pdfSize: el.getAttribute("data-pdf-size") || "",
};
},
Expand All @@ -85,11 +88,12 @@ export const Infographic = Node.create<InfographicOptions>({
class: "infographic",
"data-pdf": HTMLAttributes["data-pdf"],
"data-pdf-size": HTMLAttributes["data-pdf-size"],
"data-infographic": HTMLAttributes["data-infographic"],
},
[
"img",
{
src: HTMLAttributes.src,
src: `${this.options.baseUrl}/${HTMLAttributes["data-infographic"]}`,
height: "auto",
width: "500",
},
Expand All @@ -101,11 +105,11 @@ export const Infographic = Node.create<InfographicOptions>({
addCommands() {
return {
setInfographic:
(src: string, urlPdf: string, pdfSize: string) =>
(infoName: string, pdfName: string, pdfSize: string) =>
({ commands }) => {
return commands.insertContent({
type: this.name,
attrs: { src, urlPdf, pdfSize },
attrs: { infoName, pdfName, pdfSize },
content: [
{
type: "details",
Expand Down Expand Up @@ -140,7 +144,7 @@ export const Infographic = Node.create<InfographicOptions>({
},

updateInfographicSrc:
(newSrc: string, newUrlPdf: string, newSizePdf: string) =>
(newInfoName: string, newPdfName: string, newPdfSize: string) =>
({ state, chain }) => {
const { selection } = state;
const node = selection.$anchor.node();
Expand All @@ -150,9 +154,9 @@ export const Infographic = Node.create<InfographicOptions>({
}
return chain()
.updateAttributes("infographic", {
src: newSrc,
urlPdf: newUrlPdf,
pdfSize: newSizePdf,
infoName: newInfoName,
pdfName: newPdfName,
pdfSize: newPdfSize,
})
.run();
},
Expand Down
2 changes: 2 additions & 0 deletions targets/frontend/src/components/forms/EditionField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Controller } from "react-hook-form";
import { CommonFormProps } from "../type";
import { Editor } from "./Editor";
import { styled } from "@mui/system";
import { buildFilePathUrl } from "../../utils";

type FormEditionProps = CommonFormProps;

Expand All @@ -21,6 +22,7 @@ export const FormEditionField = (props: FormEditionProps) => {
onUpdate={onChange}
content={value}
disabled={props.disabled}
infographicBaseUrl={buildFilePathUrl()}
isError={!!error}
/>
{error && (
Expand Down
6 changes: 6 additions & 0 deletions targets/frontend/src/components/utils/fileBaseUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const buildFilePathUrl = () =>
`${
process.env.NEXT_PUBLIC_BUCKET_PUBLIC_ENDPOINT ?? "http://localhost:9000"
}/${process.env.NEXT_PUBLIC_BUCKET_DRAFT_FOLDER ?? "draft"}/${
process.env.NEXT_PUBLIC_BUCKET_DEFAULT_FOLDER ?? "default"
}`;
1 change: 1 addition & 0 deletions targets/frontend/src/components/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./Pagination";
export * from "./BreadcrumbLink";
export * from "./fileBaseUrl";
8 changes: 5 additions & 3 deletions targets/frontend/src/lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const mime = require("mime-types");
const region = process.env.BUCKET_REGION ?? "us-east-1";
const endpoint = process.env.BUCKET_ENDPOINT ?? "http://localhost:9000";
const publicEndpoint =
process.env.BUCKET_PUBLIC_ENDPOINT ?? "http://localhost:9000";
process.env.NEXT_PUBLIC_BUCKET_PUBLIC_ENDPOINT ?? "http://localhost:9000";
const accessKeyId = process.env.BUCKET_ACCESS_KEY ?? "MINIO_ACCESS_KEY";
const secretAccessKey = process.env.BUCKET_SECRET_KEY ?? "MINIO_SECRET_KEY";
const bucketName = process.env.BUCKET_NAME ?? "cdtn";
const bucketDraftFolder = process.env.BUCKET_DRAFT_FOLDER ?? "draft";
const bucketDefaultFolder = process.env.BUCKET_DEFAULT_FOLDER ?? "default";
const bucketDraftFolder =
process.env.NEXT_PUBLIC_BUCKET_DRAFT_FOLDER ?? "draft";
const bucketDefaultFolder =
process.env.NEXT_PUBLIC_BUCKET_DEFAULT_FOLDER ?? "default";

const client = new S3Client({
region,
Expand Down

0 comments on commit 3efe729

Please sign in to comment.