-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add documentation to projects
- Loading branch information
Showing
16 changed files
with
386 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/*! | ||
* Copyright 2023 - Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { lazy, Suspense } from "react"; | ||
import { Loader } from "../Loader"; | ||
|
||
const CkEditor = lazy(() => import("./CkEditor")); | ||
|
||
export function LazyCkEditorRenderer(props: { name: string; data: string }) { | ||
return ( | ||
<Suspense fallback={<Loader />}> | ||
<CkEditor | ||
id={props.name} | ||
name={props.name} | ||
data={props.data} | ||
disabled={true} | ||
invalid={false} | ||
outputType={"html"} | ||
setInputs={() => {}} | ||
/> | ||
</Suspense> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.ck.ck-editor__main > .ck-editor__editable:not(.ck-focused) { | ||
border: 0px; | ||
} | ||
.ck.ck-editor__top | ||
.ck-sticky-panel:not(.ck-focused) | ||
.ck-sticky-panel__content:not(.ck-focused) { | ||
border: 0px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
client/src/features/ProjectPageV2/ProjectPageContent/Documentation/Documentation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/*! | ||
* Copyright 2024 - Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import cx from "classnames"; | ||
import { useCallback, useState } from "react"; | ||
|
||
import { FileEarmarkText } from "react-bootstrap-icons"; | ||
import { Card, CardBody, CardHeader, ListGroup } from "reactstrap"; | ||
|
||
import { EditSaveButton } from "../../../../components/buttons/Button"; | ||
|
||
import { Project } from "../../../projectsV2/api/projectV2.api"; | ||
import { usePatchProjectsByProjectIdMutation } from "../../../projectsV2/api/projectV2.enhanced-api"; | ||
import TextAreaInput from "../../../../components/form-field/TextAreaInput.tsx"; | ||
import { useForm } from "react-hook-form"; | ||
import { LazyCkEditorRenderer } from "../../../../components/form-field/LazyCkEditorRenderer.tsx"; | ||
|
||
interface DocumentationForm { | ||
description: string; | ||
} | ||
|
||
export default function Documentation({ project }: { project: Project }) { | ||
const [updateProject] = usePatchProjectsByProjectIdMutation(); | ||
const [description, setDescription] = useState(project.documentation || ""); | ||
|
||
const { control, handleSubmit, setValue, getValues, register } = | ||
useForm<DocumentationForm>(); | ||
const onSubmit = useCallback( | ||
(data: DocumentationForm) => { | ||
setDescription(data.description); | ||
setShowEditor(false); | ||
updateProject({ | ||
"If-Match": project.etag ? project.etag : "", | ||
projectId: project.id, | ||
projectPatch: { documentation: data.description }, | ||
}); | ||
}, | ||
[project.etag, project.id, updateProject] | ||
); | ||
|
||
const [showEditor, setShowEditor] = useState(false); | ||
const toggle = () => { | ||
setShowEditor(!showEditor); | ||
setValue("description", description); | ||
}; | ||
|
||
const markdownCharacterLimit = 5000; | ||
const aboutCharacterLimit = | ||
Math.floor(((2 / 3) * markdownCharacterLimit) / 10) * 10; | ||
const [characterLimit, setCharacterLimit] = useState(aboutCharacterLimit); | ||
const [character, setCharacter] = useState(0); | ||
const [disabledSaveButton, setDisabledSaveButton] = useState(false); | ||
|
||
const wordCount = (stats: { | ||
exact: boolean; | ||
characters: number; | ||
words: number; | ||
}) => { | ||
stats.exact | ||
? setCharacterLimit(markdownCharacterLimit) | ||
: setCharacterLimit(aboutCharacterLimit); | ||
setCharacter(stats.characters); | ||
}; | ||
|
||
const descriptionField = register("description"); | ||
{ | ||
const descriptionFieldTmp = descriptionField.onChange; | ||
descriptionField.onChange = (value) => { | ||
setDisabledSaveButton(false); | ||
return descriptionFieldTmp(value); | ||
}; | ||
} | ||
|
||
return ( | ||
<Card data-cy="project-documentation-card"> | ||
<form className="form-rk-pink" onSubmit={handleSubmit(onSubmit)}> | ||
<CardHeader> | ||
<div | ||
className={cx( | ||
"align-items-center", | ||
"d-flex", | ||
"flex-wrap", | ||
"justify-content-between" | ||
)} | ||
> | ||
<h4 className={cx("m-0")}> | ||
<FileEarmarkText className={cx("me-1", "bi")} /> | ||
Documentation | ||
</h4> | ||
<span> | ||
{showEditor ? ( | ||
<span style={{ verticalAlign: "middle" }}> | ||
{character} of | ||
{characterLimit == aboutCharacterLimit ? " about " : " "} | ||
{characterLimit} characters | ||
</span> | ||
) : ( | ||
<></> | ||
)} | ||
<EditSaveButton | ||
data-cy="project-documentation-edit" | ||
toggle={toggle} | ||
disabled={disabledSaveButton} | ||
// tooltip="Save or discard your changes." | ||
checksBeforeSave={() => { | ||
if ( | ||
getValues("description").length <= markdownCharacterLimit | ||
) { | ||
return true; | ||
} | ||
setDisabledSaveButton(true); | ||
return false; | ||
}} | ||
checksBeforeSaveTooltipMessage={() => | ||
`Documentation is too long.\n The document can not be longer\nthan ${markdownCharacterLimit} characters.` | ||
} | ||
/> | ||
</span> | ||
</div> | ||
</CardHeader> | ||
<CardBody> | ||
{showEditor ? ( | ||
<ListGroup flush> | ||
<TextAreaInput<DocumentationForm> | ||
control={control} | ||
getValue={() => getValues("description")} | ||
name="description" | ||
label="Description" | ||
register={descriptionField} | ||
wordCount={wordCount} | ||
/> | ||
</ListGroup> | ||
) : ( | ||
<ListGroup flush> | ||
<div className="pb-2"></div> | ||
<LazyCkEditorRenderer name="description" data={description} /> | ||
</ListGroup> | ||
)} | ||
</CardBody> | ||
</form> | ||
</Card> | ||
); | ||
} |
Oops, something went wrong.