Skip to content

Commit

Permalink
refactoring and better styles
Browse files Browse the repository at this point in the history
  • Loading branch information
sereneinserenade committed Mar 26, 2022
1 parent 8b8b32e commit 5b4a0e9
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"lint-staged": "^9.5.0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",
"typescript": "~4.1.5"
"typescript": "^4.1.6"
},
"gitHooks": {
"pre-commit": "lint-staged"
Expand Down
93 changes: 93 additions & 0 deletions src/components/OuterComment.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<template>
<article
class="comment external-comment-area my-2 transition-all rounded-md overflow-hidden"
v-for="(comment, i) in allComments"
:key="i + 'external_comment'"
:class="[`${comment.jsonComments.uuid === activeCommentsInstance.uuid ? 'ml-4' : 'ml-8'}`]"
>
<article class="text-lg font-bold p-3 border-b-2 border-solid">"{{ comment.text }}" 💬</article>

<article
v-for="(jsonComment, j) in comment.jsonComments.comments"
:key="`${j}_${Math.random()}`"
class="external-comment border-b-2 border-gray-200 p-3"
@click="focusContent({ from: comment.from, to: comment.to })"
>
<div class="comment-details">
<strong>{{ jsonComment.userName }}</strong>

<span class="ml-1 date-time text-xs">{{ formatDate(jsonComment.time) }}</span>
</div>

<span class="content">{{ jsonComment.content }}</span>
</article>

<!-- v-if="comment.jsonComments.uuid === activeCommentsInstance.uuid" -->
<section
class="flex flex-col w-full gap-1 transition-all"
:class="[`${comment.jsonComments.uuid === activeCommentsInstance.uuid ? 'border-blue-900' : 'max-h-0 border-blue-300'}`]"
>
<textarea
v-model="commentText"
@keypress.enter.stop.prevent="setComment"
cols="30"
rows="3"
placeholder="Add comment..."
class="p-3 m-3 shadow-inner border-2 border-blue-500 focus:outline-none rounded-md"
/>

<section class="flex flex-row gap-1 m-3">
<button
class="bg-transparent hover:bg-red-500 text-red-700 font-semibold hover:text-white py-2 px-4 border-2 border-red-500 hover:border-transparent rounded-2xl shadow-lg w-1/3"
@click="() => (commentText = '')"
>Clear</button>

<button
class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border-2 border-blue-500 hover:border-transparent rounded-2xl shadow-lg w-2/3"
@click="setComment"
>
Add
<kbd class>(Ent)</kbd>
</button>
</section>
</section>
</article>
</template>

<script setup lang="ts">
import { defineProps, ref, defineEmits } from 'vue'
interface CommentInstance {
uuid?: string
comments?: any[]
}
const emit = defineEmits(['setComment'])
interface Props {
allComments: any[]
activeCommentsInstance: CommentInstance
focusContent: ({ from, to }: { from: number, to: number }) => void
formatDate: (d: any) => string | null
}
const props = defineProps<Props>()
const commentText = ref<string>('')
const setComment = () => {
emit('setComment', commentText.value)
commentText.value = ''
}
</script>

<style lang="scss">
.external-comment-area {
width: 400px;
box-shadow: 0 0 10px rgba($color: black, $alpha: 0.1);
.external-comment:last-of-type {
border: none;
}
}
</style>
150 changes: 41 additions & 109 deletions src/components/Tiptap.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="m-4 flex flex-row tiptap">
<div class="ml-4 flex flex-col tiptap-container">
<section>
<section class="flex gap-4">
<button
@click="toggleCommentMode"
type="button"
Expand All @@ -25,81 +25,41 @@
class="bubble-menu"
:shouldShow="() => (isCommentModeOn && isTextSelected && !activeCommentsInstance.uuid)"
>
<section class="comment-adder-section bg-white shadow-lg">
<textarea
v-model="commentText"
@keypress.enter.stop.prevent="setComment"
cols="30"
rows="4"
placeholder="Add comment..."
class="border-none outline-none"
/>
<section class="comment-adder-section flex bg-white shadow-lg p-3 rounded-md gap-4">
<section aria-label="textarea-section">
<textarea
v-model="commentText"
@keypress.enter.stop.prevent="() => setComment()"
cols="30"
rows="4"
placeholder="Add new comment..."
class="border-blue-500 outline-white shadow-inner"
/>
</section>

<section class="flex flex-row w-full gap-1">
<button
class="bg-transparent hover:bg-red-500 text-red-700 font-semibold hover:text-white py-2 px-4 border border-red-500 hover:border-transparent rounded shadow-lg w-1/3"
@click="() => (commentText = '')"
class="bg-transparent hover:bg-red-500 text-red-700 font-semibold hover:text-white py-2 px-4 border border-red-500 hover:border-transparent rounded-2xl shadow-sm w-1/3"
@click="() => commentText = ''"
>Clear</button>

<button
class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded shadow-lg w-2/3"
@click="setComment"
class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded-2xl shadow-sm w-2/3"
@click="() => setComment()"
>Add</button>
</section>
</section>
</BubbleMenu>
</div>

<section class="flex flex-col">
<article
class="comment external-comment shadow-lg my-2 bg-gray-100 transition-all rounded-md overflow-hidden"
v-for="(comment, i) in allComments"
:key="i + 'external_comment'"
:class="[`${comment.jsonComments.uuid === activeCommentsInstance.uuid ? 'ml-4' : 'ml-8'}`]"
>
<article
v-for="(jsonComment, j) in comment.jsonComments.comments"
:key="`${j}_${Math.random()}`"
class="external-comment border-b-2 border-gray-200 p-3"
>
<div class="comment-details">
<strong>{{ jsonComment.userName }}</strong>

<span class="ml-1 date-time text-xs">{{ formatDate(jsonComment.time) }}</span>
</div>

<span class="content">{{ jsonComment.content }}</span>
</article>

<section
v-if="comment.jsonComments.uuid === activeCommentsInstance.uuid"
class="flex flex-col w-full gap-1"
>
<textarea
v-model="commentText"
@keypress.enter.stop.prevent="setComment"
cols="30"
rows="3"
placeholder="Add comment..."
class="p-3 border-none outline-none"
/>

<section class="flex flex-row w-full gap-1">
<button
class="bg-transparent hover:bg-red-500 text-red-700 font-semibold hover:text-white py-2 px-4 border border-red-500 hover:border-transparent rounded-lg shadow-lg w-1/3"
@click="() => (commentText = '')"
>Clear</button>

<button
class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded-lg shadow-lg w-2/3"
@click="setComment"
>
Add (
<kbd class>Ent</kbd>)
</button>
</section>
</section>
</article>
<OuterCommentVue
:active-comments-instance="activeCommentsInstance"
:all-comments="allComments"
:format-date="formatDate"
:focus-content="focusContent"
@set-comment="setComment"
/>
</section>
</div>
</template>
Expand All @@ -114,6 +74,7 @@ import format from 'date-fns/format'
import { v4 as uuidv4 } from 'uuid'
import { Comment } from './extension/comment'
import OuterCommentVue from './OuterComment.vue'
/* imports over */
const dateTimeFormat = 'dd.MM.yyyy HH:mm'
Expand Down Expand Up @@ -144,11 +105,7 @@ const allComments = ref<any[]>([])
const findCommentsAndStoreValues = (editor: Editor) => {
const tempComments: any[] = []
const { doc } = editor.state
doc.descendants((node, pos) => {
debugger
editor.state.doc.descendants((node, pos) => {
const { marks } = node
marks.forEach((mark) => {
Expand All @@ -161,18 +118,15 @@ const findCommentsAndStoreValues = (editor: Editor) => {
tempComments.push({
node,
jsonComments,
pos,
from: pos,
to: pos + (node.text?.length || 0),
text: node.text,
});
}
}
})
})
if (!tempComments.length) {
allComments.value = []
return
}
allComments.value = tempComments
}
Expand All @@ -197,7 +151,9 @@ const setCurrentComment = (editor: Editor) => {
}
const tiptapEditor = useEditor({
content: '<p>I\'m trying to make comment extension, so you<span data-comment="{&quot;uuid&quot;:&quot;cc3d6027-4500-484e-a26a-146371c210ff&quot;,&quot;comments&quot;:[{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256036089,&quot;content&quot;:&quot;Talking with myself&quot;},{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256052643,&quot;content&quot;:&quot;Actually no, I am making a video/demo for you guys&quot;},{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256065012,&quot;content&quot;:&quot;And there you go&quot;}]}"> can add comm</span>ent here ☮️ and see how it goes. Add a comment <span data-comment="{&quot;uuid&quot;:&quot;a077d444-2c4d-4ccf-958b-8f3fcc33dd27&quot;,&quot;comments&quot;:[{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256014964,&quot;content&quot;:&quot;A new world of comments&quot;},{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256023904,&quot;content&quot;:&quot;Ah, so the last of us 2 is out&quot;},{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256027778,&quot;content&quot;:&quot;Yes, it is&quot;},{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256059546,&quot;content&quot;:&quot;Ah some more&quot;}]}">HERE.</span></p>',
content: `
<p>I'm trying to make comment extension, so you<span data-comment="{&quot;uuid&quot;:&quot;cc3d6027-4500-484e-a26a-146371c210ff&quot;,&quot;comments&quot;:[{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256036089,&quot;content&quot;:&quot;Talking with myself&quot;},{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256052643,&quot;content&quot;:&quot;Actually no, I am making a video/demo for you guys&quot;},{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1639256065012,&quot;content&quot;:&quot;And there you go&quot;},{&quot;userName&quot;:&quot;sereneinserenade&quot;,&quot;time&quot;:1646579995557,&quot;content&quot;:&quot;adding here&quot;}]}"> can add comm</span>ent here ☮️ and see how it goes. Add a comment here.</p>
`,
extensions: [StarterKit, Comment],
Expand All @@ -224,8 +180,10 @@ const tiptapEditor = useEditor({
},
})
const setComment = () => {
if (!commentText.value.trim().length) return
const setComment = (val?: string) => {
const localVal = val || commentText.value
if (!localVal.trim().length) return
const activeCommentInstance: CommentInstance = JSON.parse(JSON.stringify(activeCommentsInstance.value))
Expand All @@ -235,7 +193,7 @@ const setComment = () => {
commentsArray.push({
userName: currentUserName.value,
time: Date.now(),
content: commentText.value,
content: localVal,
})
const commentWithUuid = JSON.stringify({
Expand All @@ -251,7 +209,7 @@ const setComment = () => {
comments: [{
userName: currentUserName.value,
time: Date.now(),
content: commentText.value,
content: localVal,
}],
})
Expand All @@ -267,6 +225,10 @@ const toggleCommentMode = () => {
if (isCommentModeOn.value) tiptapEditor.value.setEditable(false)
else tiptapEditor.value.setEditable(true)
}
const focusContent = ({ from, to }: { from: number, to: number }) => {
tiptapEditor.value.chain().setTextSelection({ from, to }).run()
}
</script>

<style lang="scss">
Expand All @@ -275,36 +237,6 @@ const toggleCommentMode = () => {
min-width: 800px;
}
.comment-section {
article.comment {
padding: 0.5em;
display: flex;
flex-direction: column;
.comment-details {
display: flex;
flex-direction: column;
margin: 0 0 8px 0;
.date-time {
font-size: 0.7em;
}
}
}
textarea {
padding: 0.5em;
display: flex;
border-radius: 6px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
width: 100%;
}
}
.external-comment {
width: 400px;
}
.comment-adder-section {
display: flex;
flex-direction: column;
Expand Down

1 comment on commit 5b4a0e9

@vercel
Copy link

@vercel vercel bot commented on 5b4a0e9 Mar 26, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.