-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add text and background color picker
- Loading branch information
Showing
29 changed files
with
528 additions
and
16 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
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
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,53 @@ | ||
import { MarkType, Node as PrmosemirroNode } from 'prosemirror-model'; | ||
import { EditorState, SelectionRange, TextSelection, Transaction } from 'prosemirror-state'; | ||
|
||
// Ref: https://github.com/ProseMirror/prosemirror-commands/blob/master/src/commands.js | ||
|
||
function markApplies(doc: PrmosemirroNode, ranges: SelectionRange[], type: MarkType): boolean { | ||
for (const range of ranges) { | ||
const { $from, $to } = range; | ||
|
||
let canApply = $from.depth === 0 ? doc.type.allowsMarkType(type) : false; | ||
|
||
doc.nodesBetween($from.pos, $to.pos, (node: PrmosemirroNode): boolean => { | ||
if (canApply) { | ||
return false; | ||
} | ||
|
||
canApply = node.inlineContent && node.type.allowsMarkType(type); | ||
return true; | ||
}); | ||
|
||
if (canApply) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export const applyMark = (type: MarkType, attrs: { [key: string]: any } = {}) => { | ||
return (state: EditorState, dispatch?: (tr: Transaction) => void): boolean => { | ||
const { tr, selection } = state; | ||
const { $from, $to, empty, ranges } = selection; | ||
|
||
if (empty && selection instanceof TextSelection) { | ||
const { $cursor } = selection; | ||
|
||
if (!$cursor || !markApplies(state.doc, ranges, type)) { | ||
return false; | ||
} | ||
|
||
|
||
tr.addStoredMark(type.create(attrs)); | ||
} else { | ||
tr.addMark($from.pos, $to.pos, type.create(attrs)); | ||
|
||
if (!tr.docChanged) { | ||
return false; | ||
} | ||
} | ||
|
||
dispatch?.(tr.scrollIntoView()); | ||
return true; | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './removeLink'; | ||
export * from './applyMark'; |
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,99 @@ | ||
import { MarkType } from 'prosemirror-model'; | ||
import { EditorState, NodeSelection, TextSelection } from 'prosemirror-state'; | ||
|
||
import { getSelectionMarks, isMarkActive } from 'ngx-editor/helpers'; | ||
import { applyMark } from 'ngx-editor/commands'; | ||
|
||
import { Dispatch } from './types'; | ||
|
||
type Execute = (state: EditorState, dispatch?: Dispatch) => boolean; | ||
|
||
type Name = 'text_color' | 'text_background_color'; | ||
|
||
class TextColor { | ||
name: Name; | ||
|
||
constructor(name: Name) { | ||
this.name = name; | ||
} | ||
|
||
execute(attrs: {}): Execute { | ||
return (state: EditorState, dispatch?: Dispatch): boolean => { | ||
const { schema, selection, doc } = state; | ||
|
||
const type: MarkType = schema.marks[this.name]; | ||
if (!type) { | ||
return false; | ||
} | ||
|
||
const { from, empty } = selection; | ||
if (!empty) { | ||
const node = doc.nodeAt(from); | ||
if (node.isAtom && !node.isText && node.isLeaf) { | ||
// An atomic node (e.g. Image) is selected. | ||
return false; | ||
} | ||
} | ||
|
||
return applyMark(type, attrs)(state, dispatch); | ||
}; | ||
} | ||
|
||
isActive(state: EditorState): boolean { | ||
const { schema } = state; | ||
const type: MarkType = schema.marks[this.name]; | ||
|
||
if (!type) { | ||
return false; | ||
} | ||
|
||
return isMarkActive(state, type); | ||
} | ||
|
||
getActiveColors(state: EditorState): string[] { | ||
if (!this.isActive(state)) { | ||
return []; | ||
} | ||
|
||
const { schema } = state; | ||
const marks = getSelectionMarks(state); | ||
|
||
const colors = marks | ||
.filter(mark => mark.type === schema.marks[this.name]) | ||
.map(mark => mark.attrs.color) | ||
.filter(Boolean); | ||
|
||
return colors; | ||
} | ||
|
||
remove(state: EditorState, dispatch: Dispatch): boolean { | ||
const { tr } = state; | ||
const { selection, schema } = state; | ||
|
||
const { empty, from, to } = selection; | ||
|
||
const type = schema.marks[this.name]; | ||
if (!type) { | ||
return false; | ||
} | ||
|
||
if (empty) { | ||
tr.removeStoredMark(type); | ||
} else { | ||
tr.removeMark(from, to, type); | ||
|
||
if (!tr.docChanged) { | ||
return false; | ||
} | ||
} | ||
|
||
dispatch(tr.scrollIntoView()); | ||
return true; | ||
} | ||
|
||
canExecute(state: EditorState): boolean { | ||
return this.execute({})(state, null); | ||
} | ||
} | ||
|
||
export default TextColor; |
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,3 @@ | ||
export default ` | ||
<path d="M16.56,8.94L7.62,0L6.21,1.41l2.38,2.38L3.44,8.94c-0.59,0.59-0.59,1.54,0,2.12l5.5,5.5C9.23,16.85,9.62,17,10,17 s0.77-0.15,1.06-0.44l5.5-5.5C17.15,10.48,17.15,9.53,16.56,8.94z M5.21,10L10,5.21L14.79,10H5.21z M19,11.5c0,0-2,2.17-2,3.5 c0,1.1,0.9,2,2,2s2-0.9,2-2C21,13.67,19,11.5,19,11.5z M2,20h20v4H2V20z"/> | ||
`; |
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
Oops, something went wrong.