platform/plugins/document-resources/src/components/DocumentEditor.svelte
Alexander Onnikov 2220d7e19e
UBERF-6068 Show collaborators in collaborative editors (#5335)
Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
2024-04-15 18:11:47 +07:00

109 lines
3.1 KiB
Svelte

<!--
//
// Copyright © 2022-2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
-->
<script lang="ts">
import contact from '@hcengineering/contact'
import { Document } from '@hcengineering/document'
import { getResource } from '@hcengineering/platform'
import {
CollaboratorEditor,
HeadingsExtension,
ImageOptions,
SvelteNodeViewRenderer,
TodoItemExtension,
TodoListExtension
} from '@hcengineering/text-editor'
import { AnySvelteComponent } from '@hcengineering/ui'
import { getCollaborationUser } from '@hcengineering/view-resources'
import { Extensions, FocusPosition } from '@tiptap/core'
import { createEventDispatcher } from 'svelte'
import ToDoItemNodeView from './node-view/ToDoItemNodeView.svelte'
import ToDoListNodeView from './node-view/ToDoListNodeView.svelte'
export let object: Document
export let readonly = false
export let boundary: HTMLElement | undefined = undefined
export let attachFile: ImageOptions['attachFile'] | undefined = undefined
export let focusIndex = -1
export let overflow: 'auto' | 'none' = 'none'
export let editorAttributes: Record<string, string> = {}
const user = getCollaborationUser()
let userComponent: AnySvelteComponent | undefined
void getResource(contact.component.CollaborationUserAvatar).then((component) => {
userComponent = component
})
let collabEditor: CollaboratorEditor
export function focus (position?: FocusPosition): void {
collabEditor.focus(position)
}
const dispatch = createEventDispatcher()
const handleExtensions = (): Extensions => [
HeadingsExtension.configure({
onChange: (headings) => {
dispatch('headings', headings)
}
}),
TodoItemExtension.extend({
addNodeView () {
return SvelteNodeViewRenderer(ToDoItemNodeView, {
contentAs: 'li',
contentClass: 'todo-item',
componentProps: { object },
ignoreMutation: () => true
})
}
}).configure({
HTMLAttributes: {
class: 'todo-item'
}
}),
TodoListExtension.extend({
addNodeView () {
return SvelteNodeViewRenderer(ToDoListNodeView, { ignoreMutation: () => true })
}
}).configure({
HTMLAttributes: {
class: 'todo-list'
}
})
]
</script>
<CollaboratorEditor
collaborativeDoc={object.content}
objectClass={object._class}
objectId={object._id}
objectAttr="content"
{user}
{userComponent}
{focusIndex}
{readonly}
{attachFile}
{boundary}
{overflow}
{editorAttributes}
onExtensions={handleExtensions}
on:update
on:open-document
bind:this={collabEditor}
/>