Skip to content

Commit

Permalink
feat: add character counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Arman Hashemi committed Jul 19, 2022
1 parent 850707d commit a6ca366
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"preview": "vite preview"
},
"dependencies": {
"@tiptap/extension-character-count": "^2.0.0-beta.31",
"@tiptap/extension-code-block-lowlight": "^2.0.0-beta.73",
"@tiptap/extension-document": "^2.0.0-beta.17",
"@tiptap/extension-heading": "^2.0.0-beta.29",
Expand Down
16 changes: 8 additions & 8 deletions src/components/editor/RichTextEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Placeholder from "@tiptap/extension-placeholder";
import CharacterCount from "@tiptap/extension-character-count";

import { useEditor, EditorContent } from "@tiptap/react";
import { useEditorStore } from "../../global-stores/useEditorStore";
Expand All @@ -13,14 +14,9 @@ export const RichTextEditor = () => {

const editor = useEditor({
content: editorContent,
editorProps: {
attributes: {
class:
"mx-auto px-1 sm:px-2 pb-4 prose prose-sm sm:prose-base md:prose-md prose-stone prose-img:mx-auto prose-img:mb-2 prose-a:text-rose-500 prose-img:rounded prose-figcaption:text-center",
},
},
extensions: [
RichText,
CharacterCount,
Placeholder.configure({
placeholder: "یه چیزی بنویس...",
}),
Expand All @@ -36,8 +32,12 @@ export const RichTextEditor = () => {
<div>
<TitleEditor />
<Toolbar editor={editor} />
<div className="mt-6" />
<EditorContent editor={editor} />
<div className="mt-6 mx-auto px-1 sm:px-2 pb-4 prose prose-sm sm:prose-base md:prose-md prose-stone prose-img:mx-auto prose-img:mb-2 prose-a:text-rose-500 prose-img:rounded prose-figcaption:text-center">
<EditorContent editor={editor} />
<div className="text-gray-400">
{editor.storage.characterCount.words()} کلمه
</div>
</div>
</div>
)
);
Expand Down
22 changes: 16 additions & 6 deletions src/components/editor/TitleEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import Placeholder from "@tiptap/extension-placeholder";
import Heading from "@tiptap/extension-heading";
import Text from "@tiptap/extension-text";
import CharacterCount from "@tiptap/extension-character-count";

import { Document } from "@tiptap/extension-document";
import { useEditor, EditorContent } from "@tiptap/react";

import { TextDirection } from "./extensions/TextDirection";
import { useEditorStore } from "../../global-stores/useEditorStore";

const limit = 100;

export const TitleEditor = () => {
const setTitle = useEditorStore((s) => s.setTitleHtml);
const title = useEditorStore((s) => s.titleHtml);
Expand All @@ -18,11 +21,6 @@ export const TitleEditor = () => {

const editor = useEditor({
content: title,
editorProps: {
attributes: {
class: "prose prose-sm sm:prose-base mx-auto py-8",
},
},
extensions: [
TitleDocument,
Text,
Expand All @@ -33,12 +31,24 @@ export const TitleEditor = () => {
Placeholder.configure({
placeholder: "عنوان ...",
}),
CharacterCount.configure({
limit,
}),
],
onUpdate({ editor }) {
const html = editor.getHTML();
setTitle(html);
},
});

return <EditorContent className="text-x" editor={editor} />;
return (
editor && (
<div className="prose prose-sm sm:prose-base mx-auto py-8">
<EditorContent editor={editor} />
<div className="text-gray-400">
{editor.storage.characterCount.characters()}/{limit} کاراکتر
</div>
</div>
)
);
};
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@
resolved "https://registry.yarnpkg.com/@tiptap/extension-bullet-list/-/extension-bullet-list-2.0.0-beta.29.tgz#640883e4fffc1a86c7cbd78792688e7edee5ee41"
integrity sha512-R8VB2l1ZB6VeGWx/t/04nBS5Wg3qjIDEZCpPihj2fccJOw99Lu0Ub2UJg/SfdGmeNNpBh4ZYYFv1g/XjyzlXKg==

"@tiptap/extension-character-count@^2.0.0-beta.31":
version "2.0.0-beta.31"
resolved "https://registry.yarnpkg.com/@tiptap/extension-character-count/-/extension-character-count-2.0.0-beta.31.tgz#fac9ba809ddc38cf67c8a05a42d94e062a1967d2"
integrity sha512-NNA9MN1IjZe+yYQLuYVAg9RNG/3RonYrHiM5mL6vsegd+PF4uMqyZLgsM0/9dMhxh9K/pDPaCRxhuDoZC8V1wA==
dependencies:
prosemirror-model "1.18.1"
prosemirror-state "1.4.1"

"@tiptap/extension-code-block-lowlight@^2.0.0-beta.73":
version "2.0.0-beta.73"
resolved "https://registry.yarnpkg.com/@tiptap/extension-code-block-lowlight/-/extension-code-block-lowlight-2.0.0-beta.73.tgz#3f4711fadb1a1f51efbf58389216e6c8bca83d84"
Expand Down

0 comments on commit a6ca366

Please sign in to comment.