-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
17 changed files
with
208 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { EditorState } from 'prosemirror-state'; | ||
import type { NodeType } from 'prosemirror-model'; | ||
|
||
export const canInsert = (state: EditorState, nodeType: NodeType) => { | ||
const { $from } = state.selection; | ||
|
||
for (let d = $from.depth; d >= 0; d -= 1) { | ||
const index = $from.index(d); | ||
|
||
if ($from.node(d).canReplaceWith(index, index, nodeType)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export default canInsert; |
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,28 @@ | ||
import { NodeType } from 'prosemirror-model'; | ||
import { EditorState, Transaction } from 'prosemirror-state'; | ||
import { Command } from 'prosemirror-commands'; | ||
|
||
import { canInsert } from 'ngx-editor/helpers'; | ||
|
||
class HorizontalRule { | ||
insert(): Command { | ||
return (state: EditorState, dispatch?: (tr: Transaction) => void): boolean => { | ||
const { schema, tr } = state; | ||
|
||
const type: NodeType = schema.nodes.horizontal_rule; | ||
|
||
if (!type) { | ||
return false; | ||
} | ||
|
||
dispatch(tr.replaceSelectionWith(type.create()).scrollIntoView()); | ||
return true; | ||
}; | ||
} | ||
|
||
canExecute(state: EditorState): boolean { | ||
return canInsert(state, state.schema.nodes.horizontal_rule); | ||
} | ||
} | ||
|
||
export default HorizontalRule; |
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,6 @@ | ||
export default ` | ||
<g> | ||
<rect fill="none" fill-rule="evenodd" height="24" width="24"/> | ||
<rect fill-rule="evenodd" height="2" width="16" x="4" y="11"/> | ||
</g> | ||
`; |
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
3 changes: 3 additions & 0 deletions
3
projects/ngx-editor/src/lib/modules/menu/insert-command/insert-command.component.html
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 @@ | ||
<div class="NgxEditor__MenuItem--IconContainer" [innerHTML]="html | sanitizeHtml" (mousedown)="insert($event)" | ||
[title]="getTitle(name)"> | ||
</div> |
Empty file.
43 changes: 43 additions & 0 deletions
43
projects/ngx-editor/src/lib/modules/menu/insert-command/insert-command.component.spec.ts
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,43 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { InsertCommandComponent } from './insert-command.component'; | ||
import { SanitizeHtmlPipe } from '../../../pipes/sanitize/sanitize-html.pipe'; | ||
import { MenuService } from '../menu.service'; | ||
import Editor from '../../../Editor'; | ||
|
||
describe('InsertCommandComponent', () => { | ||
let component: InsertCommandComponent; | ||
let fixture: ComponentFixture<InsertCommandComponent>; | ||
let menuService: MenuService; | ||
let editor: Editor; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [ | ||
InsertCommandComponent, | ||
SanitizeHtmlPipe, | ||
], | ||
providers: [MenuService], | ||
}) | ||
.compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(InsertCommandComponent); | ||
component = fixture.componentInstance; | ||
menuService = fixture.debugElement.injector.get(MenuService); | ||
|
||
editor = new Editor(); | ||
menuService.editor = editor; | ||
|
||
fixture.detectChanges(); | ||
}); | ||
|
||
afterEach(() => { | ||
editor.destroy(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
70 changes: 70 additions & 0 deletions
70
projects/ngx-editor/src/lib/modules/menu/insert-command/insert-command.component.ts
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,70 @@ | ||
import { Component, HostBinding, Input, OnDestroy, OnInit } from '@angular/core'; | ||
import { EditorView } from 'prosemirror-view'; | ||
import { Subscription } from 'rxjs'; | ||
|
||
import Icon from '../../../icons'; | ||
import { InsertCommands } from '../MenuCommands'; | ||
import { NgxEditorService } from '../../../editor.service'; | ||
import { MenuService } from '../menu.service'; | ||
import { TBItems, ToolbarItem } from '../../../types'; | ||
|
||
@Component({ | ||
selector: 'ngx-insert-command', | ||
templateUrl: './insert-command.component.html', | ||
styleUrls: ['./insert-command.component.scss'], | ||
}) | ||
|
||
export class InsertCommandComponent implements OnInit, OnDestroy { | ||
@Input() toolbarItem: ToolbarItem; | ||
|
||
get name(): TBItems { | ||
return this.toolbarItem as TBItems; | ||
} | ||
|
||
html: string; | ||
editorView: EditorView; | ||
private updateSubscription: Subscription; | ||
|
||
constructor( | ||
private ngxeService: NgxEditorService, | ||
private menuService: MenuService, | ||
) { } | ||
|
||
@HostBinding('class.NgxEditor--Disabled') disabled = false; | ||
|
||
insert(e: MouseEvent): void { | ||
e.preventDefault(); | ||
|
||
if (e.button !== 0) { | ||
return; | ||
} | ||
|
||
const { state, dispatch } = this.editorView; | ||
const command = InsertCommands[this.name]; | ||
command.insert()(state, dispatch); | ||
} | ||
|
||
update = (view: EditorView): void => { | ||
const { state } = view; | ||
const command = InsertCommands[this.name]; | ||
this.disabled = !command.canExecute(state); | ||
}; | ||
|
||
getTitle(name: string): string { | ||
return this.ngxeService.locals.get(name); | ||
} | ||
|
||
ngOnInit(): void { | ||
this.html = Icon.get(this.name); | ||
|
||
this.editorView = this.menuService.editor.view; | ||
|
||
this.updateSubscription = this.menuService.editor.update.subscribe((view: EditorView) => { | ||
this.update(view); | ||
}); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.updateSubscription.unsubscribe(); | ||
} | ||
} |
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