Skip to content

Commit

Permalink
fix: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
mekery committed Apr 17, 2020
1 parent 4142525 commit 9bfcb6b
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 68 deletions.
38 changes: 19 additions & 19 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,6 @@ const isProduction = process.env.BUILD === 'production'
const libDir = path.resolve(__dirname, 'lib')
const srcDir = path.resolve(__dirname, 'src')

export default () => [
getConfig({
optimize: true,
file: path.resolve(libDir, 'quasar-tiptap.min.js'),
format: 'umd',
esModule: true,
}),
getConfig({
optimize: true,
file: path.resolve(libDir, 'quasar-tiptap.common.js'),
format: 'cjs',
}),
getConfig({
file: path.resolve(libDir, 'quasar-tiptap.esm.js'),
format: 'es',
esModule: true,
}),
]

function getConfig ({
file,
format,
Expand Down Expand Up @@ -137,3 +118,22 @@ function getConfig ({
],
}
}

export default () => [
getConfig({
optimize: true,
file: path.resolve(libDir, 'quasar-tiptap.min.js'),
format: 'umd',
esModule: true,
}),
getConfig({
optimize: true,
file: path.resolve(libDir, 'quasar-tiptap.common.js'),
format: 'cjs',
}),
getConfig({
file: path.resolve(libDir, 'quasar-tiptap.esm.js'),
format: 'es',
esModule: true,
}),
]
26 changes: 13 additions & 13 deletions src/extentions/Heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import { Node } from 'tiptap'
import { setBlockType, textblockTypeInputRule, toggleBlockType } from 'tiptap-commands'
import { ParagraphNodeSpec, getParagraphNodeAttrs, toParagraphDOM } from 'src/extentions/Paragraph'

function getUuid () {
const s = []
const hexDigits = '0123456789abcdef'
for (let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '' // -

return s.join('').substr(0, 6)
}

function getAttrs (dom) {
const attrs = getParagraphNodeAttrs(dom)
const id = dom.getAttribute('id')
Expand Down Expand Up @@ -78,16 +91,3 @@ export default class Heading extends Node {
))
}
}

function getUuid () {
const s = []
const hexDigits = '0123456789abcdef'
for (let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '' // -

return s.join('').substr(0, 6)
}
30 changes: 15 additions & 15 deletions src/extentions/Paragraph.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import { Paragraph as TiptapParagraph } from 'tiptap'

export const ParagraphNodeSpec = {
attrs: {
textAlign: { default: null },
indent: { default: null },
lineHeight: { default: null },
},
content: 'inline*',
group: 'block',
parseDOM: [{
tag: 'p',
getAttrs,
}],
toDOM,
}

function getAttrs (dom) {
let {
textAlign,
Expand Down Expand Up @@ -56,6 +41,21 @@ function toDOM (node) {
return ['p', attrs, 0]
}

export const ParagraphNodeSpec = {
attrs: {
textAlign: { default: null },
indent: { default: null },
lineHeight: { default: null },
},
content: 'inline*',
group: 'block',
parseDOM: [{
tag: 'p',
getAttrs,
}],
toDOM,
}

export default class Paragraph extends TiptapParagraph {
get schema () {
return ParagraphNodeSpec
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const QuasarTiptapPlugin = {
spellcheck: true,

install (Vue, opts = {}) {
this.spellcheck = opts.spellcheck == null
this.spellcheck = opts.spellcheck === null
? true
: opts.spellcheck
Vue.prototype.$o.lang.set(opts.language)
Expand Down
4 changes: 2 additions & 2 deletions src/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
lang.set = this.set
lang.getLocale = this.getLocale

if ($o !== undefined && $o.lang !== undefined) {
if ($o && $o.lang) {
$o.lang = lang
} else {
Vue.util.defineReactive($o, 'lang', lang)
Expand All @@ -19,7 +19,7 @@ export default {
this.isoName = lang.isoName
this.nativeName = lang.nativeName
this.props = lang
console.log('install lang, $o', language, lang, $o)
// console.log('install lang, $o', language, lang, $o)
}

this.set(language)
Expand Down
4 changes: 3 additions & 1 deletion src/utils/alignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export function setAlignment (tr, alignment) {
return true
})

if (!tasks.length) return tr
if (!tasks.length) {
return tr
}

tasks.forEach(job => {
const { node, pos, nodeType } = job
Expand Down
12 changes: 9 additions & 3 deletions src/utils/format_clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ const FORMAT_MARK_NAMES = [

export function clearMarks (tr, schema) {
const { doc, selection } = tr
if (!selection || !doc) return tr
if (!selection || !doc) {
return tr
}

const { from, to, empty } = selection
if (empty) return tr
if (empty) {
return tr
}

const markTypesToRemove = new Set(
FORMAT_MARK_NAMES.map(n => schema.marks[n]).filter(Boolean)
)

if (!markTypesToRemove.size) return tr
if (!markTypesToRemove.size) {
return tr
}

const tasks = []
doc.nodesBetween(from, to, (node, pos) => {
Expand Down
17 changes: 12 additions & 5 deletions src/utils/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { clamp } from './shared'
function updateIndentLevel (tr, delta) {
const { doc, selection } = tr

if (!doc || !selection) return tr
if (!doc || !selection) {
return tr
}

if (!(selection instanceof TextSelection || selection instanceof AllSelection)) {
return tr
Expand Down Expand Up @@ -37,10 +39,14 @@ function updateIndentLevel (tr, delta) {
}

function setNodeIndentMarkup (tr, pos, delta) {
if (!tr.doc) return tr
if (!tr.doc) {
return tr
}

const node = tr.doc.nodeAt(pos)
if (!node) return tr
if (!node) {
return tr
}

const minIndent = 0
const maxIndent = 7
Expand All @@ -57,7 +63,6 @@ function setNodeIndentMarkup (tr, pos, delta) {
...node.attrs,
indent,
}
console.log('indent', nodeAttrs, node.marks)

return tr.setNodeMarkup(pos, node.type, nodeAttrs, node.marks)
}
Expand All @@ -81,7 +86,9 @@ export function createIndentCommand (delta) {
export function cleanIndent (tr) {
const { doc, selection } = tr

if (!doc || !selection) return tr
if (!doc || !selection) {
return tr
}

if (!(selection instanceof TextSelection || selection instanceof AllSelection)) {
return tr
Expand Down
4 changes: 3 additions & 1 deletion src/utils/line_height.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export function setLineHeight (tr, lineHeight) {
return true
})

if (!tasks.length) return tr
if (!tasks.length) {
return tr
}

tasks.forEach(job => {
const { node, pos, nodeType } = job
Expand Down
28 changes: 21 additions & 7 deletions src/utils/mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ function markApplies (doc, ranges, type) {
can = node.inlineContent && node.type.allowsMarkType(type)
return true
})
if (can) return true
if (can) {
return true
}
}
return false
}

// https://github.com/ProseMirror/prosemirror-commands/blob/master/src/commands.js
function applyMark (tr, markType, attrs) {
if (!tr.selection || !tr.doc || !markType) return tr
if (!tr.selection || !tr.doc || !markType) {
return tr
}

// @ts-ignore
const { empty, $cursor, ranges } = tr.selection

if ((empty && !$cursor) || !markApplies(tr.doc, ranges, markType)) return tr
if ((empty && !$cursor) || !markApplies(tr.doc, ranges, markType)) {
return tr
}

if ($cursor) {
tr = tr.removeStoredMark(markType)
Expand Down Expand Up @@ -57,7 +63,9 @@ function findActiveFontFamily (state) {
const { schema, selection, tr } = state
const markType = schema.marks.fontFamily

if (!markType) return ''
if (!markType) {
return ''
}

const { empty } = selection

Expand All @@ -79,7 +87,9 @@ function findActiveFontFamily (state) {
const attrs = getMarkAttrs(state, markType)
const fontFamily = attrs.fontFamily

if (!fontFamily) return ''
if (!fontFamily) {
return ''
}

return fontFamily
}
Expand All @@ -88,7 +98,9 @@ function findActiveMarkAttribute (state, name) {
const { schema, selection, tr } = state
const markType = schema.marks[name]

if (!markType) return ''
if (!markType) {
return ''
}

const { empty } = selection

Expand All @@ -110,7 +122,9 @@ function findActiveMarkAttribute (state, name) {
const attrs = getMarkAttrs(state, markType)
const value = attrs[name]

if (!value) return ''
if (!value) {
return ''
}

return value
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function noop () {}
/**
* Check whether a value is NaN
*/
export function isNaN (any) {
export function isNaN (val) {
return Number.isNaN(val)
}

Expand Down

0 comments on commit 9bfcb6b

Please sign in to comment.