-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support project documentation without CK Editor upgrade #3478
Open
ciyer
wants to merge
3
commits into
main
Choose a base branch
from
ciyer/project-docu-minimal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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
3 changes: 3 additions & 0 deletions
3
client/src/features/ProjectPageV2/ProjectPageContent/Documentation/Documentation.module.scss
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,3 @@ | ||
.modalBody { | ||
max-height: 75vh; | ||
} |
279 changes: 279 additions & 0 deletions
279
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,279 @@ | ||
/*! | ||
* 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, useEffect, useState } from "react"; | ||
|
||
import { FileEarmarkText, Pencil, XLg } from "react-bootstrap-icons"; | ||
import { | ||
Button, | ||
Card, | ||
CardBody, | ||
CardHeader, | ||
Form, | ||
ModalBody, | ||
ModalHeader, | ||
ModalFooter, | ||
} from "reactstrap"; | ||
import { useForm } from "react-hook-form"; | ||
|
||
import { RtkOrNotebooksError } from "../../../../components/errors/RtkErrorAlert"; | ||
import TextAreaInput from "../../../../components/form-field/TextAreaInput.tsx"; | ||
import { Loader } from "../../../../components/Loader"; | ||
import LazyRenkuMarkdown from "../../../../components/markdown/LazyRenkuMarkdown"; | ||
import ScrollableModal from "../../../../components/modal/ScrollableModal"; | ||
|
||
import styles from "./Documentation.module.scss"; | ||
import PermissionsGuard from "../../../permissionsV2/PermissionsGuard"; | ||
import { Project } from "../../../projectsV2/api/projectV2.api"; | ||
import { usePatchProjectsByProjectIdMutation } from "../../../projectsV2/api/projectV2.enhanced-api"; | ||
|
||
import useProjectPermissions from "../../utils/useProjectPermissions.hook"; | ||
|
||
// Taken from src/features/projectsV2/api/projectV2.openapi.json | ||
const DESCRIPTION_MAX_LENGTH = 5000; | ||
|
||
interface DocumentationForm { | ||
documentation: string; | ||
} | ||
|
||
interface DocumentationProps { | ||
project: Project; | ||
} | ||
|
||
export default function Documentation({ project }: DocumentationProps) { | ||
const permissions = useProjectPermissions({ projectId: project.id }); | ||
const [isModalOpen, setModalOpen] = useState(false); | ||
const toggleOpen = useCallback(() => { | ||
setModalOpen((open) => !open); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Card data-cy="project-documentation-card"> | ||
<CardHeader> | ||
<div | ||
className={cx( | ||
"align-items-center", | ||
"d-flex", | ||
"justify-content-between" | ||
)} | ||
> | ||
<h4 className="m-0"> | ||
<FileEarmarkText className={cx("me-1", "bi")} /> | ||
Documentation | ||
</h4> | ||
<div className="my-auto"> | ||
<PermissionsGuard | ||
disabled={null} | ||
enabled={ | ||
<Button | ||
data-cy="project-documentation-edit" | ||
color="outline-primary" | ||
onClick={toggleOpen} | ||
size="sm" | ||
> | ||
<Pencil className="bi" /> | ||
ciyer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<span className="visually-hidden">Edit</span> | ||
</Button> | ||
} | ||
requestedPermission="write" | ||
userPermissions={permissions} | ||
/> | ||
</div> | ||
</div> | ||
</CardHeader> | ||
<CardBody> | ||
<div data-cy="project-documentation-text"> | ||
{project.documentation ? ( | ||
<LazyRenkuMarkdown markdownText={project.documentation} /> | ||
) : ( | ||
<p className={cx("m-0", "text-muted", "fst-italic")}> | ||
Describe your project, so others can understand what it does and | ||
how to use it. | ||
</p> | ||
)} | ||
</div> | ||
</CardBody> | ||
</Card> | ||
<DocumentationModal | ||
isOpen={isModalOpen} | ||
project={project} | ||
toggle={toggleOpen} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
interface DocumentationModalProps extends DocumentationProps { | ||
isOpen: boolean; | ||
toggle: () => void; | ||
} | ||
|
||
function DocumentationModal({ | ||
isOpen, | ||
project, | ||
toggle, | ||
}: DocumentationModalProps) { | ||
const [updateProject, result] = usePatchProjectsByProjectIdMutation(); | ||
const { isLoading } = result; | ||
|
||
const { | ||
control, | ||
formState: { errors, isDirty }, | ||
handleSubmit, | ||
getValues, | ||
register, | ||
reset, | ||
watch, | ||
} = useForm<DocumentationForm>({ | ||
defaultValues: { | ||
documentation: project.documentation || "", | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
reset({ | ||
documentation: project.documentation || "", | ||
}); | ||
}, [project.documentation, reset]); | ||
|
||
const onSubmit = useCallback( | ||
(data: DocumentationForm) => { | ||
updateProject({ | ||
"If-Match": project.etag ? project.etag : "", | ||
projectId: project.id, | ||
projectPatch: { documentation: data.documentation }, | ||
}); | ||
}, | ||
[project.etag, project.id, updateProject] | ||
); | ||
|
||
useEffect(() => { | ||
if (!isOpen) { | ||
reset({ documentation: project.documentation || "" }); | ||
result.reset(); | ||
} | ||
}, [isOpen, project.documentation, reset, result]); | ||
|
||
useEffect(() => { | ||
if (result.isSuccess) { | ||
toggle(); | ||
} | ||
}, [result.isSuccess, toggle]); | ||
|
||
const documentationField = register("documentation", { | ||
maxLength: { | ||
message: `Documentation is limited to ${DESCRIPTION_MAX_LENGTH} characters.`, | ||
value: DESCRIPTION_MAX_LENGTH, | ||
}, | ||
}); | ||
return ( | ||
<ScrollableModal | ||
backdrop="static" | ||
centered | ||
data-cy="project-documentation-modal" | ||
isOpen={isOpen} | ||
size="lg" | ||
toggle={toggle} | ||
> | ||
<ModalHeader toggle={toggle} data-cy="project-documentation-modal-header"> | ||
<div> | ||
<FileEarmarkText className={cx("me-1", "bi")} /> | ||
Documentation | ||
</div> | ||
</ModalHeader> | ||
<Form noValidate onSubmit={handleSubmit(onSubmit)}> | ||
<ModalBody | ||
data-cy="project-documentation-modal-body" | ||
className={styles.modalBody} | ||
> | ||
<div className="mb-1"> | ||
<TextAreaInput<DocumentationForm> | ||
control={control} | ||
getValue={() => getValues("documentation")} | ||
name="documentation" | ||
register={documentationField} | ||
/> | ||
</div> | ||
</ModalBody> | ||
<ModalFooter | ||
className="border-top" | ||
data-cy="project-documentation-modal-footer" | ||
> | ||
{errors.documentation && ( | ||
<div className="text-danger"> | ||
{errors.documentation.message ? ( | ||
<>{errors.documentation.message}</> | ||
) : ( | ||
<>Documentation text is invalid</> | ||
)} | ||
</div> | ||
)} | ||
{result.error && <RtkOrNotebooksError error={result.error} />} | ||
<DocumentationWordCount watch={watch} /> | ||
<Button | ||
color="outline-primary" | ||
className="me-2" | ||
onClick={() => { | ||
toggle(); | ||
}} | ||
> | ||
<XLg className={cx("bi", "me-1")} /> | ||
Close | ||
</Button> | ||
<Button | ||
color="primary" | ||
disabled={isLoading || !isDirty} | ||
type="submit" | ||
> | ||
{isLoading ? ( | ||
<Loader className="me-1" inline size={16} /> | ||
) : ( | ||
<Pencil className={cx("bi", "me-1")} /> | ||
)} | ||
Save | ||
</Button> | ||
</ModalFooter> | ||
</Form> | ||
</ScrollableModal> | ||
); | ||
} | ||
|
||
function DocumentationWordCount({ | ||
watch, | ||
}: { | ||
watch: ReturnType<typeof useForm<DocumentationForm>>["watch"]; | ||
}) { | ||
const documentation = watch("documentation"); | ||
const charCount = documentation.length; | ||
const isCloseToLimit = charCount >= DESCRIPTION_MAX_LENGTH - 10; | ||
return ( | ||
<div> | ||
<span | ||
className={cx( | ||
isCloseToLimit && "text-danger", | ||
isCloseToLimit && "fw-bold" | ||
)} | ||
> | ||
{charCount} | ||
</span>{" "} | ||
of {DESCRIPTION_MAX_LENGTH} characters | ||
</div> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the new entries here? I'm not sure I see comments/code with these words.