Skip to content
Merged
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
11 changes: 11 additions & 0 deletions packages/web-app-text-editor/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<script setup lang="ts">
import { computed, toRef, unref } from 'vue'
import { useGettext } from 'vue3-gettext'
import {
ContentType,
useTextEditor,
Expand All @@ -34,6 +35,8 @@ const emit = defineEmits<{
(e: 'update:currentContent', value: string): void
}>()

const { $gettext } = useGettext()

const parsedContentType = computed<ContentType>(() => {
if (contentType !== undefined) {
return contentType
Expand All @@ -52,10 +55,18 @@ const parsedContentType = computed<ContentType>(() => {
return 'plain-text'
})

const placeholder = computed(() => {
if (isReadOnly || unref(parsedContentType) !== 'markdown') {
return undefined
}
return $gettext('Write or type / for formatting options...')
})

const textEditor = useTextEditor({
contentType: unref(parsedContentType),
modelValue: toRef(() => currentContent),
readonly: isReadOnly,
placeholder: unref(placeholder),
onUpdate: (content) => emit('update:currentContent', content)
})
</script>
5 changes: 3 additions & 2 deletions packages/web-app-text-editor/tests/unit/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PartialComponentProps, mount } from '@opencloud-eu/web-test-helpers'
import { PartialComponentProps, defaultPlugins, mount } from '@opencloud-eu/web-test-helpers'
import { mock } from 'vitest-mock-extended'
import type { Resource } from '@opencloud-eu/web-client'
import App from '../../src/App.vue'
Expand All @@ -21,7 +21,8 @@ function getWrapper(props: PartialComponentProps<typeof App> = {}) {
isReadOnly: false,
resource: mock<Resource>({ extension: 'txt', mimeType: 'text/plain' }),
...props
}
},
global: { plugins: defaultPlugins() }
})
}
}
1 change: 1 addition & 0 deletions packages/web-pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@tiptap/extension-image": "^3.20.4",
"@tiptap/extension-link": "^3.20.4",
"@tiptap/extension-paragraph": "^3.20.4",
"@tiptap/extension-placeholder": "^3.20.4",
"@tiptap/extension-text": "^3.20.4",
"@tiptap/extension-text-style": "^3.20.4",
"@tiptap/extension-table": "^3.20.4",
Expand Down
16 changes: 16 additions & 0 deletions packages/web-pkg/src/editor/composables/useTextEditor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref, computed, onBeforeUnmount, watch, unref, onMounted, triggerRef } from 'vue'
import { useEditor } from '@tiptap/vue-3'
import { Placeholder } from '@tiptap/extension-placeholder'
import type { ShallowRef } from 'vue'
import type { Editor } from '@tiptap/vue-3'
import type { TextEditorOptions, TextEditorInstance, TextEditorState } from '../types'
Expand Down Expand Up @@ -28,6 +29,12 @@ export function useTextEditor(options: TextEditorOptions): TextEditorInstance {
}
}

if (options.placeholder) {
extensions.push(
Placeholder.configure({ placeholder: options.placeholder }) as (typeof extensions)[number]
)
}

// useEditor returns ShallowRef<Editor | undefined>; we cast to ShallowRef<Editor | null>
// to satisfy TextEditorInstance. The destroy() method sets it to null explicitly.
const editorOptions: Record<string, any> = {
Expand All @@ -47,6 +54,15 @@ export function useTextEditor(options: TextEditorOptions): TextEditorInstance {
editorOptions.contentType = strategy.editorContentType()
}

if (options.placeholder) {
editorOptions.editorProps = {
attributes: {
'aria-placeholder': options.placeholder,
'aria-multiline': 'true'
}
}
}

const editor = useEditor({
...editorOptions,
onUpdate({ editor: e }) {
Expand Down
9 changes: 9 additions & 0 deletions packages/web-pkg/src/editor/styles/text-editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
box-shadow: none;
}

.text-editor-content .ProseMirror p.is-editor-empty:first-child::before {
content: attr(data-placeholder);
float: left;
color: var(--oc-color-role-outline-variant, rgba(0, 0, 0, 0.4));
pointer-events: none;
user-select: none;
height: 0;
}

.text-editor-content .ProseMirror p {
margin: 0 0 8px;
}
Expand Down
1 change: 1 addition & 0 deletions packages/web-pkg/src/editor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface TextEditorOptions {
modelValue?: Ref<string>
readonly?: boolean
slashCommands?: boolean
placeholder?: string
onUpdate?: (content: string) => void
onRequestLinkUrl?: (currentUrl?: string) => Promise<string | null>
onRequestImageUrl?: () => Promise<string | null>
Expand Down
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.