add example demos

This commit is contained in:
Philipp Kühn
2021-08-25 12:13:46 +02:00
parent 15c7e1955a
commit 4607a2dbd5
82 changed files with 4993 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
<template>
<div>
<template v-for="(item, index) in items">
<div class="divider" v-if="item.type === 'divider'" :key="`divider${index}`" />
<menu-item v-else :key="index" v-bind="item" />
</template>
</div>
</template>
<script>
import MenuItem from './MenuItem.vue'
export default {
components: {
MenuItem,
},
props: {
editor: {
type: Object,
required: true,
},
},
data() {
return {
items: [
{
icon: 'bold',
title: 'Bold',
action: () => this.editor.chain().focus().toggleBold().run(),
isActive: () => this.editor.isActive('bold'),
},
{
icon: 'italic',
title: 'Italic',
action: () => this.editor.chain().focus().toggleItalic().run(),
isActive: () => this.editor.isActive('italic'),
},
{
icon: 'strikethrough',
title: 'Strike',
action: () => this.editor.chain().focus().toggleStrike().run(),
isActive: () => this.editor.isActive('strike'),
},
{
icon: 'code-view',
title: 'Code',
action: () => this.editor.chain().focus().toggleCode().run(),
isActive: () => this.editor.isActive('code'),
},
{
icon: 'mark-pen-line',
title: 'Highlight',
action: () => this.editor.chain().focus().toggleHighlight().run(),
isActive: () => this.editor.isActive('highlight'),
},
{
type: 'divider',
},
{
icon: 'h-1',
title: 'Heading 1',
action: () => this.editor.chain().focus().toggleHeading({ level: 1 }).run(),
isActive: () => this.editor.isActive('heading', { level: 1 }),
},
{
icon: 'h-2',
title: 'Heading 2',
action: () => this.editor.chain().focus().toggleHeading({ level: 2 }).run(),
isActive: () => this.editor.isActive('heading', { level: 2 }),
},
{
icon: 'paragraph',
title: 'Paragraph',
action: () => this.editor.chain().focus().setParagraph().run(),
isActive: () => this.editor.isActive('paragraph'),
},
{
icon: 'list-unordered',
title: 'Bullet List',
action: () => this.editor.chain().focus().toggleBulletList().run(),
isActive: () => this.editor.isActive('bulletList'),
},
{
icon: 'list-ordered',
title: 'Ordered List',
action: () => this.editor.chain().focus().toggleOrderedList().run(),
isActive: () => this.editor.isActive('orderedList'),
},
{
icon: 'list-check-2',
title: 'Task List',
action: () => this.editor.chain().focus().toggleTaskList().run(),
isActive: () => this.editor.isActive('taskList'),
},
{
icon: 'code-box-line',
title: 'Code Block',
action: () => this.editor.chain().focus().toggleCodeBlock().run(),
isActive: () => this.editor.isActive('codeBlock'),
},
{
type: 'divider',
},
{
icon: 'double-quotes-l',
title: 'Blockquote',
action: () => this.editor.chain().focus().toggleBlockquote().run(),
isActive: () => this.editor.isActive('blockquote'),
},
{
icon: 'separator',
title: 'Horizontal Rule',
action: () => this.editor.chain().focus().setHorizontalRule().run(),
},
{
type: 'divider',
},
{
icon: 'text-wrap',
title: 'Hard Break',
action: () => this.editor.chain().focus().setHardBreak().run(),
},
{
icon: 'format-clear',
title: 'Clear Format',
action: () => this.editor.chain()
.focus()
.clearNodes()
.unsetAllMarks()
.run(),
},
{
type: 'divider',
},
{
icon: 'arrow-go-back-line',
title: 'Undo',
action: () => this.editor.chain().focus().undo().run(),
},
{
icon: 'arrow-go-forward-line',
title: 'Redo',
action: () => this.editor.chain().focus().redo().run(),
},
],
}
},
}
</script>
<style lang="scss" scoped>
.divider {
width: 2px;
height: 1.25rem;
background-color: rgba(#000, 0.1);
margin-left: 0.5rem;
margin-right: 0.75rem;
}
</style>

View File

@@ -0,0 +1,65 @@
<template>
<button
class="menu-item"
:class="{ 'is-active': isActive ? isActive(): null }"
@click="action"
:title="title"
>
<!-- TODO: -->
<!-- <svg class="remix">
<use :xlink:href="require('remixicon/fonts/remixicon.symbol.svg') + `#ri-${icon}`" />
</svg> -->
{{ icon }}
</button>
</template>
<script>
export default {
props: {
icon: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
action: {
type: Function,
required: true,
},
isActive: {
type: Function,
default: null,
},
},
}
</script>
<style lang="scss" scoped>
.menu-item {
width: 1.75rem;
height: 1.75rem;
color: #0D0D0D;
border: none;
background-color: transparent;
border-radius: 0.4rem;
padding: 0.25rem;
margin-right: 0.25rem;
svg {
width: 100%;
height: 100%;
fill: currentColor;
}
&.is-active,
&:hover {
color: #FFF;
background-color: #0D0D0D;
}
}
</style>

View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div id="app"></div>
<script type="module">
import setup from '../../../../setup/vue.ts'
import source from '@source'
setup('Examples/CollaborativeEditing', source)
</script>
</body>
</html>

View File

@@ -0,0 +1,7 @@
context('/demos/Examples/CollaborativeEditing', () => {
before(() => {
cy.visit('/demos/Examples/CollaborativeEditing')
})
// TODO: Write tests
})

View File

@@ -0,0 +1,349 @@
<template>
<div class="editor" v-if="editor">
<menu-bar class="editor__header" :editor="editor" />
<editor-content class="editor__content" :editor="editor" />
<div class="editor__footer">
<div :class="`editor__status editor__status--${status}`">
<template v-if="status === 'connected'">
{{ users.length }} user{{ users.length === 1 ? '' : 's' }} online in {{ room }}
</template>
<template v-else>
offline
</template>
</div>
<div class="editor__name">
<button @click="setName">
{{ currentUser.name }}
</button>
</div>
</div>
</div>
</template>
<script>
import { Editor, EditorContent } from '@tiptap/vue-3'
import StarterKit from '@tiptap/starter-kit'
import Collaboration from '@tiptap/extension-collaboration'
import CollaborationCursor from '@tiptap/extension-collaboration-cursor'
import TaskList from '@tiptap/extension-task-list'
import TaskItem from '@tiptap/extension-task-item'
import Highlight from '@tiptap/extension-highlight'
import CharacterCount from '@tiptap/extension-character-count'
import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'
import { IndexeddbPersistence } from 'y-indexeddb'
import MenuBar from './MenuBar.vue'
const getRandomElement = list => {
return list[Math.floor(Math.random() * list.length)]
}
const getRandomRoom = () => {
return getRandomElement([
'rooms.7',
'rooms.8',
'rooms.9',
])
}
export default {
components: {
EditorContent,
MenuBar,
},
data() {
return {
currentUser: JSON.parse(localStorage.getItem('currentUser')) || {
name: this.getRandomName(),
color: this.getRandomColor(),
},
provider: null,
indexdb: null,
editor: null,
users: [],
status: 'connecting',
room: getRandomRoom(),
}
},
mounted() {
const ydoc = new Y.Doc()
this.provider = new WebsocketProvider('wss://websocket.tiptap.dev', this.room, ydoc)
this.provider.on('status', event => {
this.status = event.status
})
window.ydoc = ydoc
this.indexdb = new IndexeddbPersistence(this.room, ydoc)
this.editor = new Editor({
extensions: [
StarterKit.configure({
history: false,
}),
Highlight,
TaskList,
TaskItem,
Collaboration.configure({
document: ydoc,
}),
CollaborationCursor.configure({
provider: this.provider,
user: this.currentUser,
onUpdate: users => {
this.users = users
},
}),
CharacterCount.configure({
limit: 10000,
}),
],
})
localStorage.setItem('currentUser', JSON.stringify(this.currentUser))
},
methods: {
setName() {
const name = (window.prompt('Name') || '')
.trim()
.substring(0, 32)
if (name) {
return this.updateCurrentUser({
name,
})
}
},
updateCurrentUser(attributes) {
this.currentUser = { ...this.currentUser, ...attributes }
this.editor.chain().focus().user(this.currentUser).run()
localStorage.setItem('currentUser', JSON.stringify(this.currentUser))
},
getRandomColor() {
return getRandomElement([
'#958DF1',
'#F98181',
'#FBBC88',
'#FAF594',
'#70CFF8',
'#94FADB',
'#B9F18D',
])
},
getRandomName() {
return getRandomElement([
'Lea Thompson', 'Cyndi Lauper', 'Tom Cruise', 'Madonna', 'Jerry Hall', 'Joan Collins', 'Winona Ryder', 'Christina Applegate', 'Alyssa Milano', 'Molly Ringwald', 'Ally Sheedy', 'Debbie Harry', 'Olivia Newton-John', 'Elton John', 'Michael J. Fox', 'Axl Rose', 'Emilio Estevez', 'Ralph Macchio', 'Rob Lowe', 'Jennifer Grey', 'Mickey Rourke', 'John Cusack', 'Matthew Broderick', 'Justine Bateman', 'Lisa Bonet',
])
},
},
beforeDestroy() {
this.editor.destroy()
this.provider.destroy()
},
}
</script>
<style lang="scss" scoped>
.editor {
display: flex;
flex-direction: column;
max-height: 400px;
color: #0D0D0D;
background-color: #FFF;
border: 3px solid #0D0D0D;
border-radius: 0.75rem;
&__header {
display: flex;
align-items: center;
flex: 0 0 auto;
flex-wrap: wrap;
padding: 0.25rem;
border-bottom: 3px solid #0D0D0D;
}
&__content {
padding: 1.25rem 1rem;
flex: 1 1 auto;
overflow-x: hidden;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
&__footer {
display: flex;
flex: 0 0 auto;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
white-space: nowrap;
border-top: 3px solid #0D0D0D;
font-size: 12px;
font-weight: 600;
color: #0D0D0D;
white-space: nowrap;
padding: 0.25rem 0.75rem;
}
/* Some information about the status */
&__status {
display: flex;
align-items: center;
border-radius: 5px;
&::before {
content: ' ';
flex: 0 0 auto;
display: inline-block;
width: 0.5rem;
height: 0.5rem;
background: rgba(#0D0D0D, 0.5);
border-radius: 50%;
margin-right: 0.5rem;
}
&--connecting::before {
background: #616161;
}
&--connected::before {
background: #B9F18D;
}
}
&__name {
button {
background: none;
border: none;
font: inherit;
font-size: 12px;
font-weight: 600;
color: #0D0D0D;
border-radius: 0.4rem;
padding: 0.25rem 0.5rem;
&:hover {
color: #FFF;
background-color: #0D0D0D;
}
}
}
}
</style>
<style lang="scss">
/* Give a remote user a caret */
.collaboration-cursor__caret {
position: relative;
margin-left: -1px;
margin-right: -1px;
border-left: 1px solid #0D0D0D;
border-right: 1px solid #0D0D0D;
word-break: normal;
pointer-events: none;
}
/* Render the username above the caret */
.collaboration-cursor__label {
position: absolute;
top: -1.4em;
left: -1px;
font-size: 12px;
font-style: normal;
font-weight: 600;
line-height: normal;
user-select: none;
color: #0D0D0D;
padding: 0.1rem 0.3rem;
border-radius: 3px 3px 3px 0;
white-space: nowrap;
}
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
ul,
ol {
padding: 0 1rem;
}
h1,
h2,
h3,
h4,
h5,
h6 {
line-height: 1.1;
}
code {
background-color: rgba(#616161, 0.1);
color: #616161;
}
pre {
background: #0D0D0D;
color: #FFF;
font-family: 'JetBrainsMono', monospace;
padding: 0.75rem 1rem;
border-radius: 0.5rem;
code {
color: inherit;
padding: 0;
background: none;
font-size: 0.8rem;
}
}
mark {
background-color: #FAF594;
}
img {
max-width: 100%;
height: auto;
}
hr {
margin: 1rem 0;
}
blockquote {
padding-left: 1rem;
border-left: 2px solid rgba(#0D0D0D, 0.1);
}
hr {
border: none;
border-top: 2px solid rgba(#0D0D0D, 0.1);
margin: 2rem 0;
}
ul[data-type="taskList"] {
list-style: none;
padding: 0;
li {
display: flex;
align-items: center;
> label {
flex: 0 0 auto;
margin-right: 0.5rem;
}
}
}
}
</style>