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,178 @@
<template>
<node-view-wrapper class="draw">
<input type="color" v-model="color">
<input
type="number"
min="1"
max="10"
v-model="size"
>
<button @click="clear">
clear
</button>
<svg viewBox="0 0 500 250" ref="canvas">
<template v-for="item in node.attrs.lines">
<path
v-if="item.id !== id"
:key="item.id"
:d="item.path"
:id="`id-${item.id}`"
:stroke="item.color"
:stroke-width="item.size"
/>
</template>
</svg>
</node-view-wrapper>
</template>
<script>
import { NodeViewWrapper } from '@tiptap/vue-3'
import { v4 as uuid } from 'uuid'
import * as d3 from 'd3'
import simplify from 'simplify-js'
const getRandomElement = list => {
return list[Math.floor(Math.random() * list.length)]
}
export default {
name: 'Paper',
components: {
NodeViewWrapper,
},
props: {
updateAttributes: {
type: Function,
required: true,
},
node: {
type: Object,
required: true,
},
},
data() {
return {
color: getRandomElement([
'#A975FF',
'#FB5151',
'#FD9170',
'#FFCB6B',
'#68CEF8',
'#80CBC4',
'#9DEF8F',
]),
size: Math.ceil(Math.random() * Math.floor(10)),
svg: null,
path: null,
points: [],
drawing: false,
id: uuid(),
}
},
methods: {
onStartDrawing(event) {
this.drawing = true
this.points = []
this.path = this.svg
.append('path')
.data([this.points])
.attr('id', `id-${this.id}`)
.attr('stroke', this.color)
.attr('stroke-width', this.size)
const moveEvent = event.type === 'mousedown'
? 'mousemove'
: 'touchmove'
this.svg.on(moveEvent, this.onMove)
},
onMove(event) {
event.preventDefault()
this.points.push(d3.pointers(event)[0])
this.tick()
},
onEndDrawing() {
this.svg.on('mousemove', null)
this.svg.on('touchmove', null)
if (!this.drawing) {
return
}
this.drawing = false
this.svg.select(`#id-${this.id}`).remove()
this.id = uuid()
},
simplifyPoints(points) {
return simplify(points.map(point => ({ x: point[0], y: point[1] }))).map(point => [point.x, point.y])
},
tick() {
requestAnimationFrame(() => {
this.path.attr('d', points => {
const path = d3.line().curve(d3.curveBasis)(points)
// const simplifiedPath = d3.line().curve(d3.curveBasis)(this.simplifyPoints(points))
const lines = this.node.attrs.lines.filter(item => item.id !== this.id)
this.updateAttributes({
lines: [
...lines,
{
id: this.id,
color: this.color,
size: this.size,
path,
// path: simplifiedPath,
},
],
})
return path
})
})
},
clear() {
this.updateAttributes({
lines: [],
})
},
},
mounted() {
this.svg = d3.select(this.$refs.canvas)
this.svg
.on('mousedown', this.onStartDrawing)
.on('mouseup', this.onEndDrawing)
.on('mouseleave', this.onEndDrawing)
.on('touchstart', this.onStartDrawing)
.on('touchend', this.onEndDrawing)
.on('touchleave', this.onEndDrawing)
},
}
</script>
<style lang="scss">
.draw {
svg {
background: #f1f3f5;
cursor: crosshair;
}
path {
fill: none;
stroke-linecap: round;
stroke-linejoin: round;
}
}
</style>

View File

@@ -0,0 +1,35 @@
import { Node, mergeAttributes } from '@tiptap/core'
import { VueNodeViewRenderer } from '@tiptap/vue-3'
import Component from './Component.vue'
export default Node.create({
name: 'paper',
group: 'block',
atom: true,
addAttributes() {
return {
lines: {
default: [],
},
}
},
parseHTML() {
return [
{
tag: 'div[data-type="paper"]',
},
]
},
renderHTML({ HTMLAttributes }) {
return ['div', mergeAttributes(HTMLAttributes, { 'data-type': 'paper' })]
},
addNodeView() {
return VueNodeViewRenderer(Component)
},
})

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/Drawing', source)
</script>
</body>
</html>

View File

@@ -0,0 +1,40 @@
<template>
<div v-if="editor">
<editor-content :editor="editor" />
</div>
</template>
<script>
import { Editor, EditorContent } from '@tiptap/vue-3'
import Document from '@tiptap/extension-document'
import Text from '@tiptap/extension-text'
import Paper from './Paper.js'
export default {
components: {
EditorContent,
},
data() {
return {
editor: null,
}
},
mounted() {
this.editor = new Editor({
extensions: [
Document.extend({
content: 'paper',
}),
Text,
Paper,
],
})
},
beforeDestroy() {
this.editor.destroy()
},
}
</script>