Skip to content
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

Adiciona contador de caracteres em tempo real no campo de postagem #1813

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions pages/interface/components/Content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const CONTENT_TITLE_PLACEHOLDER_EXAMPLES = [
'e.g. Como renomear uma branch local no Git?',
];

const MAX_BODY_LENGTH = 20000;

export default function Content({ content, isPageRootOwner, mode = 'view', viewFrame = false }) {
const [componentMode, setComponentMode] = useState(mode);
const [contentObject, setContentObject] = useState(content);
Expand Down Expand Up @@ -529,6 +531,7 @@ function EditMode({ contentObject, setContentObject, setComponentMode, localStor
onKeyDown={onKeyDown}
compact={!!contentObject?.parent_id}
clobberPrefix={`${contentObject?.owner_username ?? user?.username}-content-`}
maxLength={MAX_BODY_LENGTH}
/>

{errorObject?.key === 'body' && (
Expand Down
20 changes: 18 additions & 2 deletions pages/interface/components/Markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import byteMDLocale from 'bytemd/locales/pt_BR.json';
import 'katex/dist/katex.css';
import { useEffect, useMemo, useRef, useState } from 'react';

import { Box, EditorColors, EditorStyles, useTheme } from '@/TabNewsUI';
import { Box, EditorColors, EditorStyles, Text, useTheme } from '@/TabNewsUI';

import { anchorHeadersPlugin } from './plugins/anchor-headers';
import { copyCodeToClipboardPlugin } from './plugins/copy-code-to-clipboard';
Expand Down Expand Up @@ -82,8 +82,23 @@ export default function Viewer({ value: _value, areLinksTrusted, clobberPrefix,
);
}

export function CharacterCounter({ text, maxLength }) {
const characterCount = useMemo(() => text.length, [text]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precisa do useMemo aqui?

return (
<Text
sx={{
display: 'flex',
justifyContent: 'flex-end',
fontSize: '14px',
color: characterCount > maxLength ? 'danger.fg' : undefined,
}}>
{characterCount}/{maxLength}
</Text>
);
}

// Editor is not part of Primer, so error messages and styling need to be created manually
export function Editor({ isValid, onKeyDown, compact, areLinksTrusted, clobberPrefix, ...props }) {
export function Editor({ isValid, onKeyDown, compact, areLinksTrusted, clobberPrefix, maxLength, value, ...props }) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agora precisa voltar a passar value para ByteMdEditor, pois antes ele recebia via ...props.

Mas, ao invés disso, que tal adicionar o contador diretamente lá no Content? Assim você não precisa se preocupar com o funcionamento do Editor, pois não precisaria mudar nada nesse arquivo aqui e nem na maneira de usar o Editor lá no Content.

Seria só modificar ali no trecho que começa com {errorObject?.key === 'body' && (, e isso também facilitará a criação de um layout compatível com a visualização de mensagens de erro.

clobberPrefix = clobberPrefix?.toLowerCase();
const bytemdPluginList = usePlugins({ areLinksTrusted, clobberPrefix });
const editorMode = 'split'; // 'tab'
Expand Down Expand Up @@ -114,6 +129,7 @@ export function Editor({ isValid, onKeyDown, compact, areLinksTrusted, clobberPr
/>
<EditorStyles compact={compact} mode={editorMode} />
<EditorColors />
<CharacterCounter text={value} maxLength={maxLength} />
</Box>
);
}
Expand Down