Skip to content

[WEB-65] [EDITOR] Make blog title editable within editor #374

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

Merged
merged 8 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import styled from "styled-components";

export const StyledTextBox = styled.input`
background: transparent;

border: none;
border-color: transparent;


padding: 0.5em;

font-size: inherit;

display: flex;
justify-content: center;
align-items: center;
user-select: none;

&:hover {
cursor: text;
color: black;
transform: scale(1.04);
}

cursor: pointer;
`;
15 changes: 15 additions & 0 deletions frontend/src/cse-ui-kit/EditableTitle_textbox/EditableTitle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react";
import { StyledTextBox } from "./EditableTitle-Styled";

type Props = {
onChange?: React.ChangeEventHandler<HTMLInputElement>;
onBlur?: React.FocusEventHandler<HTMLInputElement>
value: string
};

export default function EditableTitle({ onChange, onBlur, value, ...styleProps }: Props) {
return (
<StyledTextBox onChange={onChange} onBlur={onBlur} value={value} {...styleProps}/>
);
}

3 changes: 3 additions & 0 deletions frontend/src/cse-ui-kit/EditableTitle_textbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import EditableTitle from './EditableTitle';

export default EditableTitle;
53 changes: 49 additions & 4 deletions frontend/src/packages/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import CreateContentBlock from "src/cse-ui-kit/CreateContentBlock_button";
import CreateHeadingBlock from "src/cse-ui-kit/CreateHeadingBlock_button";
import SyncDocument from "src/cse-ui-kit/SyncDocument_button";
import PublishDocument from "src/cse-ui-kit/PublishDocument_button";
import EditableTitle from "src/cse-ui-kit/EditableTitle_textbox";

import EditorHeader from "src/deprecated/components/Editor/EditorHeader";
import { useParams, useLocation, useNavigate } from "react-router-dom";

Expand All @@ -20,6 +22,12 @@ import CreateCodeBlock from "src/cse-ui-kit/CreateCodeBlock_button ";
import IconButton from "@mui/material/IconButton";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";

import {
RenamePayloadType,
renameFileEntityAction,
} from "src/packages/dashboard/state/folders/actions";
import { useDispatch } from "react-redux";

const Container = styled.div`
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -52,7 +60,34 @@ const EditorPage: FC = () => {
const [focusedId, setFocusedId] = useState<number>(0);

const state = useLocation().state as LocationState;
const filename = state != null ? state.filename : "";

const [filename, setFilename] = useState<string>(state != null ? state.filename : "");

const [savedFilename, setSavedFilename] = useState<string>(`${filename}`);

const navigate = useNavigate();
const dispatch = useDispatch();
const updateFilename = () => {
// No empty names allowed!
if (filename == "") {
setFilename(savedFilename);

} else if (filename !== savedFilename && id !== undefined) {
const newPayload: RenamePayloadType = { id, newName: filename };
dispatch(renameFileEntityAction(newPayload));

// Re-navigate to current page with new file name so that
// filename changes are persistent on reloads
navigate("/editor/" + id, {
replace: true,
state: {
filename
}
}), [navigate];

setSavedFilename(`${filename}`);
}
}

const updateValues: UpdateCallback = (idx, updatedBlock) => {
const requiresUpdate = JSON.stringify(blocks[idx]) !== JSON.stringify(updateValues);
Expand Down Expand Up @@ -100,16 +135,26 @@ const EditorPage: FC = () => {
opManager.current?.pushToServer(newCreationOperation(newElement, blocks.length));
}

const navigate = useNavigate();

return (
<div style={{ height: "100%" }}>
<EditorHeader>
<LeftContainer>
<IconButton aria-label="back" onClick={() => navigate(-1)} sx={{ 'paddingRight': '20px' }}>
<IconButton
aria-label="back"
onClick={() => navigate(-1)}
sx={{ 'paddingRight': '20px' }}>
<ArrowBackIcon fontSize="inherit"/>
</IconButton>
{filename}

<EditableTitle
value={filename}
onChange={(event) => {
setFilename(event.target.value)
}}
onBlur={updateFilename}
/>

</LeftContainer>
<ButtonContainer>
<SyncDocument onClick={() => syncDocument()} />
Expand Down