-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
333 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Heading as TiptapHeading } from 'tiptap-extensions'; | ||
|
||
import { ParagraphNodeSpec, getParagraphNodeAttrs, toParagraphDOM } from './paragraph'; | ||
|
||
function getAttrs (dom) { | ||
const attrs = getParagraphNodeAttrs(dom); | ||
const level = dom.nodeName.match(/[H|h](\d)/)[1]; | ||
attrs.level = level; | ||
return attrs; | ||
} | ||
|
||
function toDOM (node) { | ||
const dom = toParagraphDOM(node); | ||
const level = node.attrs.level || 1; | ||
dom[0] = `h${level}`; | ||
return dom; | ||
} | ||
|
||
export default class Heading extends TiptapHeading { | ||
get schema () { | ||
return { | ||
...ParagraphNodeSpec, | ||
attrs: { | ||
...ParagraphNodeSpec.attrs, | ||
level: { | ||
default: 1, | ||
}, | ||
}, | ||
defining: true, | ||
draggable: false, | ||
parseDOM: this.options.levels | ||
.map(level => ({ | ||
tag: `h${level}`, | ||
getAttrs, | ||
})), | ||
toDOM, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// nodes | ||
export { default as Paragraph } from './paragraph'; | ||
export { default as Heading } from './heading'; | ||
export { default as ListItem } from './list_item'; | ||
|
||
// extensions | ||
export { default as TextAlign } from './text_align'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ListItem as TiptapListItem } from 'tiptap-extensions'; | ||
|
||
import { ALIGN_PATTERN } from './text_align'; | ||
|
||
const ListItemNodeSpec = { | ||
attrs: { | ||
textAlign: { default: null }, | ||
}, | ||
content: 'paragraph block*', | ||
defining: true, | ||
draggable: false, | ||
parseDOM: [{ | ||
tag: 'li', | ||
getAttrs, | ||
}], | ||
toDOM, | ||
}; | ||
|
||
function getAttrs (dom) { | ||
const attrs = {}; | ||
const { textAlign } = dom.style; | ||
let align = dom.getAttribute('data-text-align') || textAlign || ''; | ||
align = ALIGN_PATTERN.test(align) ? align : null; | ||
|
||
if (align) { | ||
attrs.textAlign = align; | ||
} | ||
return attrs; | ||
} | ||
|
||
function toDOM (node) { | ||
const { | ||
textAlign, | ||
} = node.attrs; | ||
|
||
const attrs = {}; | ||
|
||
if (textAlign && textAlign !== 'left') { | ||
attrs['data-text-align'] = textAlign; | ||
} | ||
|
||
return ['li', attrs, 0]; | ||
} | ||
|
||
export default class ListItem extends TiptapListItem { | ||
get schema () { | ||
return ListItemNodeSpec; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Paragraph as TiptapParagraph } from 'tiptap'; | ||
|
||
import { ALIGN_PATTERN } from './text_align'; | ||
|
||
export const ParagraphNodeSpec = { | ||
attrs: { | ||
textAlign: { default: null }, | ||
}, | ||
content: 'inline*', | ||
group: 'block', | ||
parseDOM: [{ | ||
tag: 'p', | ||
getAttrs, | ||
}], | ||
toDOM, | ||
}; | ||
|
||
function getAttrs (dom) { | ||
const { | ||
textAlign, | ||
} = dom.style; | ||
|
||
let align = dom.getAttribute('align') || textAlign || ''; | ||
align = ALIGN_PATTERN.test(align) ? align : null; | ||
|
||
return { | ||
textAlign: align, | ||
}; | ||
} | ||
|
||
function toDOM (node) { | ||
const { | ||
textAlign, | ||
} = node.attrs; | ||
|
||
const attrs = {}; | ||
|
||
if (textAlign && textAlign !== 'left') { | ||
attrs['data-text-align'] = textAlign; | ||
} | ||
|
||
return ['p', attrs, 0]; | ||
} | ||
|
||
export default class Paragraph extends TiptapParagraph { | ||
get schema () { | ||
return ParagraphNodeSpec; | ||
} | ||
} | ||
|
||
export const toParagraphDOM = toDOM; | ||
export const getParagraphNodeAttrs = getAttrs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Extension } from 'tiptap'; | ||
|
||
const ALLOWED_NODE_TYPES = [ | ||
'paragraph', | ||
'heading', | ||
'list_item', | ||
]; | ||
|
||
export const ALIGN_PATTERN = /(left|right|center|justify)/; | ||
|
||
export default class TextAlign extends Extension { | ||
get name () { | ||
return 'text_align'; | ||
} | ||
|
||
commands () { | ||
return { | ||
align_left: this.setTextAlign({ alignment: 'null' }), | ||
align_center: this.setTextAlign({ alignment: 'center' }), | ||
align_right: this.setTextAlign({ alignment: 'right' }), | ||
align_justify: this.setTextAlign({ alignment: 'justify' }), | ||
}; | ||
} | ||
|
||
setTextAlign ({ alignment }) { | ||
return () => (state, dispatch) => { | ||
let { tr } = state; | ||
const { selection, doc } = tr; | ||
|
||
if (!selection || !doc) { | ||
return tr; | ||
} | ||
|
||
const { from, to } = selection; | ||
|
||
const jobs = []; | ||
alignment = alignment || null; | ||
|
||
doc.nodesBetween(from, to, (node, pos) => { | ||
const nodeType = node.type; | ||
const align = node.attrs.align || null; | ||
if (align !== alignment && ALLOWED_NODE_TYPES.includes(nodeType.name)) { | ||
jobs.push({ | ||
node, | ||
pos, | ||
nodeType, | ||
}); | ||
} | ||
return true; | ||
}); | ||
|
||
if (!jobs.length) return tr; | ||
|
||
jobs.forEach(job => { | ||
const { node, pos, nodeType } = job; | ||
let { attrs } = node; | ||
if (alignment) { | ||
attrs = { | ||
...attrs, | ||
textAlign: alignment, | ||
}; | ||
} else { | ||
attrs = { | ||
...attrs, | ||
textAlign: null, | ||
}; | ||
} | ||
tr = tr.setNodeMarkup(pos, nodeType, attrs, node.marks); | ||
}); | ||
|
||
if (tr.docChanged) { | ||
dispatch && dispatch(tr); | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
} | ||
} | ||
|
||
export function isTextAlignActive (state, align) { | ||
const { selection, doc } = state; | ||
const { from, to } = selection; | ||
|
||
let keepLooking = true; | ||
let active = false; | ||
|
||
doc.nodesBetween(from, to, (node, _pos) => { | ||
if (keepLooking && node.attrs.textAlign === align) { | ||
keepLooking = false; | ||
active = true; | ||
} | ||
return keepLooking; | ||
}); | ||
|
||
return active; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters