Skip to content

Commit

Permalink
♻️ (#2345): make Callout as a component
Browse files Browse the repository at this point in the history
Signed-off-by: Vinicius Reis <vinicius.reis@nextcloud.com>
  • Loading branch information
Vinicius Reis committed May 26, 2022
1 parent 11bd79a commit 46ad510
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 65 deletions.
62 changes: 0 additions & 62 deletions css/prosemirror.scss
Original file line number Diff line number Diff line change
Expand Up @@ -192,68 +192,6 @@ div.ProseMirror {
margin-right: 0;
}

// Callout Block
.callout {
background-color: var(--callout-background, var(--color-background-hover));
border-left-color: var(--callout-border, var(--color-primary-element));
border-radius: var(--border-radius);
padding: 1em;
padding-left: 2em;
border-left-width: 0.3em;
border-left-style: solid;
position: relative;
margin-bottom: 0.5em;
+ & {
margin-top: 0.5em;
}

// Block icon
&::before {
content: '';
background-image: var(--callout-icon, var(--icon-info-000));
background-size: contain;
position: absolute;
top: calc(50% - 8px);
left: 0.4em;
height: 16px;
width: 16px;
}

> p {
&:last-child {
margin-bottom: 0;
}
}

// Info (default) variables
&, &-info {
// --callout-background: var(--color-primary-light);
--callout-border: var(--color-primary-element);
--callout-icon: var(--icon-info-000);
}

// Warn variables
&-warn {
// --callout-background: var(--color-warning-hover);
--callout-border: var(--color-warning);
--callout-icon: var(--icon-text-warn-000);
}

// Error variables
&-error {
// --callout-background: var(--color-error-hover);
--callout-border: var(--color-error);
--callout-icon: var(--icon-error-000);
}

// Success variables
&-success {
// --callout-background: var(--color-success-hover);
--callout-border: var(--color-success);
--callout-icon: var(--icon-text-success-000);
}
}

// table variables
@at-root :root {
--table-color-border: var(--color-border);
Expand Down
4 changes: 2 additions & 2 deletions cypress/integration/workspace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('Workspace', function() {
.click()
.then(() => {
// check content
cy.get(`.ProseMirror .callout.callout-${type}`)
cy.get(`.ProseMirror .callout.callout--${type}`)
.should('contain', 'Callout')

// disable
Expand All @@ -216,7 +216,7 @@ describe('Workspace', function() {
const actionName = `callout-${type}`
return getSubmenuItem('callouts', actionName)
.click()
.then(() => cy.get(`.ProseMirror .callout.callout-${type}`))
.then(() => cy.get(`.ProseMirror .callout.callout--${type}`))
.should('contain', 'Callout')
.then(() => {
last = type
Expand Down
122 changes: 122 additions & 0 deletions src/nodes/Callout.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<!--
- @copyright Copyright (c) 2022 Vinicius Reis <vinicius@nextcloud.com>
-
- @author Vinicius Reis <vinicius@nextcloud.com>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<NodeViewWrapper data-text-el="callout"
class="callout"
:class="`callout--${type}`"
as="div">
<component :is="icon" class="callout__icon" />
<NodeViewContent class="callout__content" />
</NodeViewWrapper>
</template>

<script>
import { NodeViewWrapper, NodeViewContent } from '@tiptap/vue-2'
import { Positive, Warn, Danger, Info } from '../components/icons.js'

const ICONS_MAP = {
info: Info,
success: Positive,
error: Danger,
warn: Warn,
}

export default {
// eslint-disable-next-line vue/match-component-file-name
name: 'Callout',
components: {
NodeViewWrapper,
NodeViewContent,
},
props: {
node: {
type: Object,
required: true,
},
},
computed: {
icon() {
return ICONS_MAP[this.type] || Info
},
type() {
return this.node?.attrs?.type || 'info'
},
},
}
</script>

<style lang="scss" scoped>
.callout {
background-color: var(--callout-background, var(--color-background-hover));
border-left-color: var(--callout-border, var(--color-primary-element));
border-radius: var(--border-radius);
padding: 1em;
padding-left: 0.5em;
border-left-width: 0.3em;
border-left-style: solid;
position: relative;
margin-bottom: 0.5em;

display: flex;
align-items: center;
justify-content: flex-start;

+ & {
margin-top: 0.5em;
}

.callout__content {
margin-left: 1em;
&::v-deep p {
&:last-child {
margin-bottom: 0;
}
}
}

.callout__icon {
&, ::v-deep svg {
color: var(--callout-border);
}
}

// Info (default) variables
&, &--info {
--callout-border: var(--color-primary-element);
}

// Warn variables
&--warn {
--callout-border: var(--color-warning);
}

// Error variables
&--error {
--callout-border: var(--color-error);
}

// Success variables
&--success {
--callout-border: var(--color-success);
}
}
</style>
9 changes: 8 additions & 1 deletion src/nodes/Callouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
*
*/

import { Node, mergeAttributes, isNodeActive } from '@tiptap/core'
import { Node, isNodeActive, mergeAttributes } from '@tiptap/core'
import { VueNodeViewRenderer } from '@tiptap/vue-2'
import { typesAvailable } from './../markdownit/callouts.js'

import Callout from './Callout.vue'

export default Node.create({

name: 'callout',
Expand Down Expand Up @@ -94,6 +97,10 @@ export default Node.create({
state.closeBlock(node)
},

addNodeView() {
return VueNodeViewRenderer(Callout)
},

addCommands() {
return {
setCallout: attributes => ({ commands }) => {
Expand Down

0 comments on commit 46ad510

Please sign in to comment.