From e04786dd76a025cafde3091885bbfdd7aa483d59 Mon Sep 17 00:00:00 2001 From: Micle Bu Date: Mon, 13 Apr 2020 21:37:21 +0800 Subject: [PATCH] feat: line height --- src/lib/components/QuasarTiptap.vue | 2 + .../buttons/OLineHeightDropdown.vue | 59 +++++++++++++++++++ .../components/menubars/OEditorMenuBar.vue | 3 + src/lib/css/tiptap.styl | 20 +++++++ src/lib/extentions/LineHeight.js | 25 ++++++++ src/lib/extentions/Paragraph.js | 6 +- src/lib/extentions/index.js | 1 + src/lib/utils/line_height.js | 58 ++++++++++++++++++ src/pages/quasar-tiptap/basic.vue | 1 + 9 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 src/lib/components/buttons/OLineHeightDropdown.vue create mode 100644 src/lib/extentions/LineHeight.js create mode 100644 src/lib/utils/line_height.js diff --git a/src/lib/components/QuasarTiptap.vue b/src/lib/components/QuasarTiptap.vue index a64ad78..0456193 100644 --- a/src/lib/components/QuasarTiptap.vue +++ b/src/lib/components/QuasarTiptap.vue @@ -51,6 +51,7 @@ import { OTodoItem, OAlignment, OIndent, + OLineHeight, OBackColor, OForeColor, OFontFamily, @@ -164,6 +165,7 @@ export default { new OHeading({ levels: [1, 2, 3, 4, 5] }), new OAlignment(), new OIndent(), + new OLineHeight(), new OForeColor(), new OBackColor(), new OFontFamily(), diff --git a/src/lib/components/buttons/OLineHeightDropdown.vue b/src/lib/components/buttons/OLineHeightDropdown.vue new file mode 100644 index 0000000..c44400c --- /dev/null +++ b/src/lib/components/buttons/OLineHeightDropdown.vue @@ -0,0 +1,59 @@ + + + + + diff --git a/src/lib/components/menubars/OEditorMenuBar.vue b/src/lib/components/menubars/OEditorMenuBar.vue index 84a8a98..c788284 100644 --- a/src/lib/components/menubars/OEditorMenuBar.vue +++ b/src/lib/components/menubars/OEditorMenuBar.vue @@ -10,6 +10,7 @@ + @@ -55,6 +56,7 @@ import OBackColorDropdown from 'src/lib/components/buttons/OBackColorDropdown' import OFontFamilyDropdown from 'src/lib/components/buttons/OFontFamilyDropdown' import OAlignDropdown from 'src/lib/components/buttons/OAlignDropdown' import OAlignGroup from 'src/lib/components/buttons/OAlignGroup' +import OLineHeightDropdown from 'src/lib/components/buttons/OLineHeightDropdown' import OHeadingDropdown from 'src/lib/components/buttons/OHeadingDropdown' import OHeadingGroup from 'src/lib/components/buttons/OHeadingGroup' import OHeadingList from 'src/lib/components/buttons/OHeadingList' @@ -115,6 +117,7 @@ export default { OFontFamilyDropdown, OAlignDropdown, OAlignGroup, + OLineHeightDropdown, OHeadingDropdown, OHeadingGroup, OHeadingList diff --git a/src/lib/css/tiptap.styl b/src/lib/css/tiptap.styl index 9583125..45e70a4 100644 --- a/src/lib/css/tiptap.styl +++ b/src/lib/css/tiptap.styl @@ -373,6 +373,26 @@ margin-left: 210px !important; } + // Line Height + .editor__content *[data-line-height="1"] { + line-height: 1 !important; + } + .editor__content *[data-line-height="1.15"] { + line-height: 1.15 !important; + } + .editor__content *[data-line-height="1.5"] { + line-height: 1.5 !important; + } + .editor__content *[data-line-height="2"] { + line-height: 2 !important; + } + .editor__content *[data-line-height="2.5"] { + line-height: 2.5 !important; + } + .editor__content *[data-line-height="3"] { + line-height: 3 !important; + } + .menubar { //margin-bottom: 1rem; -webkit-transition: visibility .2s .4s, opacity .2s .4s; diff --git a/src/lib/extentions/LineHeight.js b/src/lib/extentions/LineHeight.js new file mode 100644 index 0000000..0b19f29 --- /dev/null +++ b/src/lib/extentions/LineHeight.js @@ -0,0 +1,25 @@ +import { Extension } from 'tiptap' +import { setLineHeight } from '../utils/line_height' + +export default class LineHeight extends Extension { + get name () { + return 'lineHeight' + } + + commands () { + return attrs => (state, dispatch) => { + let { selection } = state + const tr = setLineHeight( + state.tr.setSelection(selection), + attrs.lineHeight, + ) + + if (tr.docChanged) { + dispatch && dispatch(tr) + return true + } else { + return false + } + } + } +} diff --git a/src/lib/extentions/Paragraph.js b/src/lib/extentions/Paragraph.js index 6ea20cc..802f9f5 100644 --- a/src/lib/extentions/Paragraph.js +++ b/src/lib/extentions/Paragraph.js @@ -39,7 +39,6 @@ function toDOM (node) { lineHeight, } = node.attrs - let style = '' const attrs = {} if (textAlign && textAlign !== 'left') { @@ -51,12 +50,9 @@ function toDOM (node) { } if (lineHeight) { - // const cssLineHeight = transformLineHeightToCSS(lineHeight) - // style += `line-height: ${cssLineHeight};` + attrs['data-line-height'] = lineHeight } - style && (attrs.style = style) - return ['p', attrs, 0] } diff --git a/src/lib/extentions/index.js b/src/lib/extentions/index.js index f540629..64536ce 100644 --- a/src/lib/extentions/index.js +++ b/src/lib/extentions/index.js @@ -27,3 +27,4 @@ export { default as OFontFamily } from './FontFamily' // Extensions export { default as OAlignment } from './Alignment' export { default as OIndent } from './Indent' +export { default as OLineHeight } from './LineHeight' diff --git a/src/lib/utils/line_height.js b/src/lib/utils/line_height.js new file mode 100644 index 0000000..e55fb9a --- /dev/null +++ b/src/lib/utils/line_height.js @@ -0,0 +1,58 @@ +/** + * Utils: alignment + * @see https://github.com/Leecason/element-tiptap/blob/master/src/extensions/text_align.ts + * @todo Table + */ + +const ALLOWED_NODE_TYPES = [ + 'paragraph', + 'heading', + 'list_item', + 'todo_item', +] + +export function setLineHeight (tr, lineHeight) { + console.log('line-height', lineHeight) + const { selection, doc } = tr + + if (!selection || !doc) { + return tr + } + + const { from, to } = selection + + const tasks = [] + lineHeight = lineHeight || null + + doc.nodesBetween(from, to, (node, pos) => { + const nodeType = node.type + if (ALLOWED_NODE_TYPES.includes(nodeType.name)) { + const height = node.attrs.lineHeight || null + if (height !== lineHeight) { + tasks.push({ + node, + pos, + nodeType, + }) + + return nodeType.name !== 'list_item' && nodeType.name !== 'todo_item' + } + } + return true + }) + + if (!tasks.length) return tr + + tasks.forEach(job => { + const { node, pos, nodeType } = job + let { attrs } = node + attrs = { + ...attrs, + lineHeight: lineHeight, + } + + tr = tr.setNodeMarkup(pos, nodeType, attrs, node.marks) + }) + + return tr +} diff --git a/src/pages/quasar-tiptap/basic.vue b/src/pages/quasar-tiptap/basic.vue index 5684b37..e8b8f94 100644 --- a/src/pages/quasar-tiptap/basic.vue +++ b/src/pages/quasar-tiptap/basic.vue @@ -38,6 +38,7 @@ export default { 'align-group', 'indent', 'outdent', + 'line-height', 'separator', 'heading', 'horizontal',