Skip to content

Commit

Permalink
feat: add support for clear formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Jul 26, 2022
1 parent 40dd011 commit 8fc2647
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class AppComponent implements OnInit, OnDestroy {
['link', 'image'],
['text_color', 'background_color'],
['align_left', 'align_center', 'align_right', 'align_justify'],
['horizontal_rule'],
['horizontal_rule', 'format_clear'],
];
colorPresets = ['red', '#FF0000', 'rgb(255, 0, 0)'];

Expand Down
1 change: 1 addition & 0 deletions projects/ngx-editor/src/lib/Locals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const defaults: Record<string, string> = {
text_color: 'Text Color',
background_color: 'Background Color',
horizontal_rule: 'Horizontal rule',
format_clear: 'Clear Formatting',
insertLink: 'Insert Link',
removeLink: 'Remove Link',
insertImage: 'Insert Image',
Expand Down
35 changes: 35 additions & 0 deletions projects/ngx-editor/src/lib/commands/FormatClear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { EditorState, Transaction, Command } from 'prosemirror-state';

const SAFE_MARKS = ['link'];

class FormatClear {
insert(): Command {
return (state: EditorState, dispatch?: (tr: Transaction) => void): boolean => {
const { tr } = state;
const { ranges, empty } = tr.selection;

if (empty) {
return true;
}

Object.entries(state.schema.marks).forEach(([markType, mark]) => {
if (SAFE_MARKS.includes(markType)) {
return;
}

ranges.forEach((range) => {
tr.removeMark(range.$from.pos, range.$to.pos, mark as any);
});
});

dispatch(tr);
return true;
};
}

canExecute(): boolean {
return true;
}
}

export default FormatClear;
2 changes: 2 additions & 0 deletions projects/ngx-editor/src/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TextAlign from './TextAlign';
import Link from './Link';
import Image from './Image';
import TextColor from './TextColor';
import FormatClear from './FormatClear';

export const STRONG = new Mark('strong');
export const EM = new Mark('em');
Expand All @@ -15,6 +16,7 @@ export const UNDERLINE = new Mark('u');
export const STRIKE = new Mark('s');
export const BLOCKQUOTE = new Blockquote();
export const HORIZONTAL_RULE = new HorizontalRule();
export const FORMAT_CLEAR = new FormatClear();
export const UL = new ListItem(true);
export const OL = new ListItem(false);
export const H1 = new Heading(1);
Expand Down
3 changes: 3 additions & 0 deletions projects/ngx-editor/src/lib/icons/format_clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default `
<path d="M0 0h24v24H0z" fill="none"/><path d="M3.27 5L2 6.27l6.97 6.97L6.5 19h3l1.57-3.66L16.73 21 18 19.73 3.55 5.27 3.27 5zM6 5v.18L8.82 8h2.4l-.72 1.68 2.1 2.1L14.21 8H20V5H6z"/>
`;
2 changes: 2 additions & 0 deletions projects/ngx-editor/src/lib/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import alignJustify from './align_justify';
import textColor from './text_color';
import colorFill from './color_fill';
import horizontalRule from './horizontal_rule';
import formatClear from './format_clear';

const DEFAULT_ICON_HEIGHT = 20;
const DEFAULT_ICON_WIDTH = 20;
Expand All @@ -42,6 +43,7 @@ const icons: Record<string, any> = {
text_color: textColor,
color_fill: colorFill,
horizontal_rule: horizontalRule,
format_clear: formatClear,
};

class Icon {
Expand Down
1 change: 1 addition & 0 deletions projects/ngx-editor/src/lib/modules/menu/MenuCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const ToggleCommands: Record<string, ToggleCommand> = {

export const InsertCommands: Record<string, InsertCommand> = {
horizontal_rule: Commands.HORIZONTAL_RULE,
format_clear: Commands.FORMAT_CLEAR,
};

export const Link = Commands.LINK;
Expand Down
2 changes: 2 additions & 0 deletions projects/ngx-editor/src/lib/modules/menu/menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const DEFAULT_TOOLBAR: Toolbar = [
['link', 'image'],
['text_color', 'background_color'],
['align_left', 'align_center', 'align_right', 'align_justify'],
['format_clear'],
];

export const TOOLBAR_MINIMAL: Toolbar = [
Expand Down Expand Up @@ -89,6 +90,7 @@ export class MenuComponent implements OnInit {

insertCommands: ToolbarItem[] = [
'horizontal_rule',
'format_clear',
];

iconContainerClass = ['NgxEditor__MenuItem', 'NgxEditor__MenuItem--Icon'];
Expand Down
3 changes: 2 additions & 1 deletion projects/ngx-editor/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export type TBItems = 'bold'
| 'align_center'
| 'align_right'
| 'align_justify'
| 'horizontal_rule';
| 'horizontal_rule'
| 'format_clear';

export type ToolbarDropdown = { heading?: TBHeadingItems[] };
export type ToolbarCustomMenuItem = (editorView: EditorView) => TCR;
Expand Down

0 comments on commit 8fc2647

Please sign in to comment.