Skip to content

Commit

Permalink
refact(kt-comment): refact CommentInlineEdit.vue and KtCommentInput.vue
Browse files Browse the repository at this point in the history
- Add isInternal and allowInternal flags
- Refact comment docs
  • Loading branch information
santiagoballadares authored and carsoli committed May 22, 2023
1 parent 4c4956a commit d2fc284
Show file tree
Hide file tree
Showing 16 changed files with 663 additions and 374 deletions.
356 changes: 209 additions & 147 deletions packages/documentation/pages/usage/components/comment.vue

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions packages/kotti-ui/source/kotti-comment/KtComment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@
<div :class="classes">
<CommentEntry
v-bind="commentProps"
:type="EntryType.POST"
@delete="onDelete"
@edit="onEdit"
@reply="onReply"
/>
<div class="kt-comment__thread">
<div v-if="showCommentThread" class="kt-comment__thread">
<CommentEntry
v-for="reply in replies"
:key="reply.id"
v-bind="reply"
:dangerouslyOverrideParser="dangerouslyOverrideParser"
isReply
:parentId="id"
:postEscapeParser="postEscapeParser"
:type="EntryType.REPLY"
:tabIndex="tabIndex"
@delete="onDelete"
@edit="onEdit"
/>
<KtCommentInput
v-if="userToReply"
isInline
v-bind="{ isInternal, placeholder, tabIndex, userAvatar }"
autofocus
isReply
:parentId="id"
:placeholder="placeholder"
:replyToUserId="userToReply.id"
:userAvatar="user.avatar"
@add="onAdd"
@cancel="onCancel"
/>
</div>
</div>
Expand Down Expand Up @@ -58,14 +59,17 @@ export default defineComponent<KottiComment.PropsInternal>({
return {
classes: computed(() => ({
'kt-comment': true,
'kt-comment--is-internal-thread': props.isInternalThread,
'kt-comment--is-internal': props.isInternal,
})),
commentProps: computed<KottiComment.Entry.PropsInternal>(() => ({
...omit(props, 'replies'),
isReply: false,
})),
commentProps: computed(() => omit(props, 'replies')),
EntryType: KottiComment.EntryType,
onAdd: (payload: KottiComment.Events.Add) => {
userToReply.value = null
emit('add', payload)
},
onCancel: () => (userToReply.value = null),
onDelete: (payload: KottiComment.Events.Delete) =>
emit('delete', payload),
onEdit: (payload: KottiComment.Events.Edit) => emit('edit', payload),
Expand All @@ -75,6 +79,9 @@ export default defineComponent<KottiComment.PropsInternal>({
? [translations.value.replyToLabel, userToReply.value.name].join(' ')
: undefined,
),
showCommentThread: computed(
() => props.replies.length > 0 || userToReply.value !== null,
),
userToReply,
}
},
Expand All @@ -85,7 +92,7 @@ export default defineComponent<KottiComment.PropsInternal>({
.kt-comment {
padding: var(--unit-4);
&--is-internal-thread {
&--is-internal {
background-color: var(--ui-01);
}
Expand Down
154 changes: 44 additions & 110 deletions packages/kotti-ui/source/kotti-comment/KtCommentInput.vue
Original file line number Diff line number Diff line change
@@ -1,140 +1,74 @@
<template>
<div :class="containerClass">
<div :class="wrapperClass">
<KtAvatar
v-if="!isInline"
class="kt-comment-input__avatar"
size="sm"
:src="userAvatar"
/>
<textarea
ref="textarea"
v-model="text"
class="kt-comment-input__textarea"
:placeholder="placeholder"
@blur="textFocused = false"
@focus="textFocused = true"
@input="updateHeight"
/>
<KtButton
:disabled="!text"
:label="replyButtonText"
type="text"
@click="handleSubmitClick"
/>
</div>
<div class="kt-comment-input">
<KtAvatar size="sm" :src="userAvatar" />
<CommentTextArea
v-model="_message"
v-bind="{ allowInternal, autofocus, isReply, placeholder, tabIndex }"
:isInternal="_isInternal"
@cancel="onCancel"
@confirm="onConfirm"
@toggleInternal="onToggleInternal"
/>
</div>
</template>

<script lang="ts">
import { computed, defineComponent, ref } from '@vue/composition-api'
import { defineComponent, ref, watch } from '@vue/composition-api'
import { KtAvatar } from '../kotti-avatar'
import { KtButton } from '../kotti-button'
import { useTranslationNamespace } from '../kotti-i18n/hooks'
import { makeProps } from '../make-props'
import { KottiCommentInput } from './types'
import CommentTextArea from './components/CommentTextArea.vue'
import { KottiComment, KottiCommentInput } from './types'
export default defineComponent<KottiCommentInput.PropsInternal>({
name: 'KtCommentInput',
components: {
KtAvatar,
KtButton,
CommentTextArea,
},
props: makeProps(KottiCommentInput.propsSchema),
setup(props, { emit }) {
const translations = useTranslationNamespace('KtComment')
const _isInternal = ref<KottiComment.PropsInternal['isInternal']>(
props.isInternal,
)
const _message = ref<KottiComment.PropsInternal['message']>('')
watch(
() => props.isInternal,
(isInternal) => (_isInternal.value = isInternal),
)
const text = ref<string | null>(null)
const textarea = ref<HTMLElement | null>(null)
const textFocused = ref(false)
return {
containerClass: computed(() => ({
'kt-comment-input': true,
'kt-comment-input--inline': props.isInline,
})),
handleSubmitClick: () => {
if (text.value === null) return
emit('add', {
message: text.value,
_isInternal,
_message,
onCancel: () => {
_isInternal.value = props.isInternal
_message.value = ''
emit('cancel')
},
onConfirm: () => {
if (_message.value === '') return
const payload: KottiComment.Events.Add = {
isInternal: _isInternal.value,
message: _message.value,
replyToUserId: props.replyToUserId,
parentId: props.parentId,
})
text.value = null
if (textarea.value) textarea.value.style.height = '1.2rem'
},
replyButtonText: computed(() =>
props.isInline
? translations.value.replyButton
: translations.value.postButton,
),
text,
textarea,
textFocused,
updateHeight: () => {
if (textarea.value === null) return '1.2rem'
const height = textarea.value.scrollHeight
textarea.value.style.height = `${height}px`
}
emit('add', payload)
_isInternal.value = props.isInternal
_message.value = ''
},
wrapperClass: computed(() => ({
'kt-comment-input__wrapper': true,
'kt-comment-input__wrapper--focus': textFocused.value,
'kt-comment-input__wrapper--inline': props.isInline,
})),
onToggleInternal: () => (_isInternal.value = !_isInternal.value),
}
},
})
</script>

<style lang="scss" scoped>
.kt-comment-input {
box-sizing: border-box;
display: flex;
&--inline {
margin: 0 0 var(--unit-1) 0;
}
&__wrapper {
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
padding: var(--unit-2);
background-color: var(--white);
border: 1px solid #dbdbdb;
border-radius: var(--border-radius);
&--focus {
border: 1px solid #bbb;
}
&--inline {
padding: var(--unit-1);
}
}
&__avatar {
margin-right: var(--unit-1);
}
&__textarea {
flex: 1 1;
width: 100%;
height: 1.2rem;
padding: 0;
margin: 0 0.2rem;
resize: none;
border: 0;
&:focus {
outline: none;
}
}
.kt-button {
cursor: pointer;
}
column-gap: var(--unit-2);
}
</style>
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<template>
<div class="kt-comment__actions">
<KtButton
v-if="showReply"
v-if="!isReply"
class="kt-comment__actions__reply-button"
:label="translations.replyButton"
:tabIndex="tabIndex"
type="text"
@click.stop="onReply"
/>
<KtButton
v-if="isEditable"
class="kt-comment__actions__edit-button"
:label="translations.editButton"
:tabIndex="tabIndex"
type="text"
@click.stop="onEdit"
/>
<KtButton
v-if="isDeletable"
class="kt-comment__actions__delete-button"
:label="translations.deleteButton"
:tabIndex="tabIndex"
type="text"
@click.stop="onDelete"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
<KtAvatar size="sm" :src="user.avatar" />
<div class="kt-comment__entry__wrapper">
<CommentHeader
v-bind="{ createdAt, isInternalThread, isModified, user }"
v-bind="{ createdAt, isInternal, isModified, isReply, user }"
/>
<CommentInlineEdit
v-bind="{
allowInternal,
dangerouslyOverrideParser,
id,
isEditing,
isInternal,
isReply,
message,
parentId,
postEscapeParser,
tabIndex,
}"
@edit="onEdit"
@update:isEditing="isEditing = $event"
/>
<CommentActions
v-if="!isEditing"
v-bind="{ isDeletable, isEditable, showReply }"
v-bind="{ isDeletable, isEditable, isReply, tabIndex }"
@delete="onDelete"
@reply="onReply"
@update:isEditing="isEditing = $event"
Expand All @@ -29,7 +33,7 @@
</template>

<script lang="ts">
import { computed, defineComponent, ref } from '@vue/composition-api'
import { defineComponent, ref } from '@vue/composition-api'
import { pick } from 'lodash'
import { makeProps } from '../../make-props'
Expand All @@ -50,8 +54,6 @@ export default defineComponent<KottiComment.Entry.PropsInternal>({
setup(props, { emit }) {
const isEditing = ref(false)
const isReply = computed(() => props.type === KottiComment.EntryType.REPLY)
return {
isEditing,
onDelete: () => {
Expand All @@ -63,7 +65,6 @@ export default defineComponent<KottiComment.Entry.PropsInternal>({
},
onEdit: (payload: KottiComment.Events.Edit) => emit('edit', payload),
onReply: () => emit('reply', props.user),
showReply: computed(() => !isReply.value),
}
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<div class="kt-comment__header">
<div class="kt-comment__header__left-wrapper">
<span class="kt-comment__header__user-name" v-text="user.name" />
<span v-if="isInternalThread" class="kt-comment__header__internal-thread">
<span v-if="isInternal && !isReply" class="kt-comment__header__internal">
<i class="yoco" v-text="Yoco.Icon.PERMISSION" />
{{ translations.internalThreadLabel }}
{{ translations.internalLabel }}
</span>
</div>
<div class="kt-comment__header__right-wrapper">
Expand Down Expand Up @@ -69,7 +69,7 @@ export default defineComponent<KottiComment.Header.PropsInternal>({
color: var(--text-03);
}
&__internal-thread {
&__internal {
display: flex;
gap: var(--unit-1);
color: var(--text-02);
Expand Down
Loading

0 comments on commit d2fc284

Please sign in to comment.