Skip to content

Commit

Permalink
feat: add support for superscript, subscript
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed May 13, 2024
1 parent ac2d4e7 commit 65a95ff
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/src/content/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ NgxEditorModule.forRoot({
insertLink: 'Insert Link',
removeLink: 'Remove Link',
insertImage: 'Insert Image',
superscript: 'Superscript',
subscript: 'Subscript',

// pupups, forms, others...
url: 'URL',
Expand Down
1 change: 1 addition & 0 deletions docs/src/content/docs/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class AppComponent implements OnInit, OnDestroy {
['text_color', 'background_color'],
['align_left', 'align_center', 'align_right', 'align_justify'],
['horizontal_rule', 'format_clear', 'indent', 'outdent'],
['superscript', 'subscript'],
];
colorPresets = ['red', '#FF0000', 'rgb(255, 0, 0)'];

Expand Down
24 changes: 24 additions & 0 deletions projects/ngx-editor/schema/marks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ const textBackgroundColor: MarkSpec = {
},
};

const sup: MarkSpec = {
attrs: {},
parseDOM: [
{ tag: 'sup' },
{ style: 'vertical-align=super' },
],
toDOM() {
return ['sup', 0];
},
};

const sub: MarkSpec = {
attrs: {},
parseDOM: [
{ tag: 'sub' },
{ style: 'vertical-align=sub' },
],
toDOM() {
return ['sub', 0];
},
};

const marks = {
link,
em,
Expand All @@ -154,6 +176,8 @@ const marks = {
s,
text_color: textColor,
text_background_color: textBackgroundColor,
sup,
sub,
};

export default marks;
10 changes: 10 additions & 0 deletions projects/ngx-editor/src/lib/EditorCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ class EditorCommands {
return this;
}

superscript(): this {
execMark('sup')(this.state, this.dispatch);
return this;
}

subscript(): this {
execMark('sub')(this.state, this.dispatch);
return this;
}

toggleOrderedList(): this {
const command = new ListCommand(false);
command.toggle()(this.state, this.dispatch);
Expand Down
2 changes: 2 additions & 0 deletions projects/ngx-editor/src/lib/Locals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const defaults: Record<string, string | Observable<string>> = {
insertImage: 'Insert Image',
indent: 'Increase Indent',
outdent: 'Decrease Indent',
superscript: 'Superscript',
subscript: 'Subscript',

// pupups, forms, others...
url: 'URL',
Expand Down
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 @@ -36,3 +36,5 @@ export const TEXT_COLOR = new TextColor('text_color', 'color');
export const TEXT_BACKGROUND_COLOR = new TextColor('text_background_color', 'backgroundColor');
export const INDENT = new Indent('increase');
export const OUTDENT = new Indent('decrease');
export const SUPERSCRIPT = new Mark('sup');
export const SUBSCRIPT = new Mark('sub');
4 changes: 4 additions & 0 deletions projects/ngx-editor/src/lib/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import horizontalRule from './horizontal_rule';
import formatClear from './format_clear';
import indent from './indent';
import outdent from './outdent';
import superscript from './superscript';
import subscript from './subscript';

const DEFAULT_ICON_HEIGHT = 20;
const DEFAULT_ICON_WIDTH = 20;
Expand Down Expand Up @@ -49,6 +51,8 @@ export const icons: Record<string, any> = {
format_clear: formatClear,
indent,
outdent,
superscript,
subscript,
path: '<path></path>',
};

Expand Down
1 change: 1 addition & 0 deletions projects/ngx-editor/src/lib/icons/subscript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '<g><rect fill="none" height="20" width="20"/><path d="M17,15l-1,0v1h2v1h-3v-2c0-0.55,0.45-1,1-1l1,0l0-1h-2v-1l2,0c0.55,0,1,0.45,1,1v1C18,14.55,17.55,15,17,15z M5.63,14h1.9 l2.43-3.87h0.08L12.47,14h1.9l-3.32-5.2l3.1-4.8h-1.91l-2.19,3.56H9.96L7.75,4h-1.9l3.09,4.8L5.63,14z"/></g>';
1 change: 1 addition & 0 deletions projects/ngx-editor/src/lib/icons/superscript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '<g><rect fill="none" height="20" width="20"/><path d="M17,6l-1,0v1h2v1l-3,0V6c0-0.55,0.45-1,1-1l1,0l0-1h-2V3l2,0c0.55,0,1,0.45,1,1v1C18,5.55,17.55,6,17,6z M5.63,16h1.9 l2.43-3.87h0.08L12.47,16h1.9l-3.32-5.2l3.1-4.8h-1.91l-2.19,3.56H9.96L7.75,6h-1.9l3.09,4.8L5.63,16z"/></g>';
2 changes: 2 additions & 0 deletions projects/ngx-editor/src/lib/modules/menu/MenuCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const ToggleCommands: Record<string, ToggleCommand> = {
align_center: Commands.ALIGN_CENTER,
align_right: Commands.ALIGN_RIGHT,
align_justify: Commands.ALIGN_JUSTIFY,
superscript: Commands.SUPERSCRIPT,
subscript: Commands.SUBSCRIPT,
};

export const InsertCommands: Record<string, InsertCommand> = {
Expand Down
3 changes: 3 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 @@ -37,6 +37,7 @@ export const TOOLBAR_FULL: Toolbar = [
['text_color', 'background_color'],
['align_left', 'align_center', 'align_right', 'align_justify'],
['horizontal_rule', 'format_clear', 'indent', 'outdent'],
['superscript', 'subscript'],
];

const DEFAULT_COLOR_PRESETS = [
Expand Down Expand Up @@ -86,6 +87,8 @@ export class MenuComponent implements OnInit {
'align_center',
'align_right',
'align_justify',
'superscript',
'subscript',
];

insertCommands: ToolbarItem[] = [
Expand Down
4 changes: 3 additions & 1 deletion projects/ngx-editor/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export type TBItems = 'bold'
| 'horizontal_rule'
| 'format_clear'
| 'indent'
| 'outdent';
| 'outdent'
| 'superscript'
| 'subscript';

export type ToolbarDropdown = { heading?: TBHeadingItems[] };
export type ToolbarLinkOptions = Partial<LinkOptions>;
Expand Down

0 comments on commit 65a95ff

Please sign in to comment.