-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dom-adapters): basic inline tool adapter implementation (#74)
* Implmenet global CaretAdapter * Handle native inputs * Pass input type to Input component props * Use class to represent index * Fix lint in dom-adapters * fix linter * added inline tool adapter * implement model updates * lint fix * fix index * adapter renders inline tools * lint fix and clean up * jsdoc * clean up * jsdoc * jsdoc * surround content replaced * suggestions * lint fix * jsdoc * added bold and italic inline tools into core package * naming * naming * added inline toolbar and inlineToolAdapter init into core * update packages and lock * build fix * implement inline tool adapter to core - fully implement current realization of inline tool adapter to core - remove from the playground * clean up * jsdoc and naming improvements * naming * naming * renaming * fix hardcoded * tools are initialized inside of the inline toolbar initialization * fixed inline tool attaching * jsdoc * naming fix * fixed imports * lint fix * try build fix * install dependencies * add sdk package * fix build for core * change package name in actions * add references * typo * fix build --------- Co-authored-by: George Berezhnoy <gohabereg@users.noreply.github.com> Co-authored-by: George Berezhnoy <gohabereg@gmail.com>
- Loading branch information
1 parent
884d0ca
commit 50f0db4
Showing
37 changed files
with
816 additions
and
43 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,2 @@ | ||
export * from './BlockTool.js'; | ||
export * from './Config.js'; |
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
2 changes: 1 addition & 1 deletion
2
...ore/src/tools/internal/paragraph/index.ts → ...s/internal/block-tools/paragraph/index.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
72 changes: 72 additions & 0 deletions
72
packages/core/src/tools/internal/inline-tools/bold/index.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,72 @@ | ||
import type { FormattingActionWithRange, InlineTool } from '@editorjs/sdk'; | ||
import type { InlineFragment, TextRange } from '@editorjs/model'; | ||
import { FormattingAction } from '@editorjs/model'; | ||
import { IntersectType } from '@editorjs/model'; | ||
import { make } from '@editorjs/dom'; | ||
|
||
/** | ||
* Bold Tool | ||
* | ||
* Inline Toolbar Tool | ||
* | ||
* Makes selected text bolder | ||
*/ | ||
export default class BoldInlineTool implements InlineTool { | ||
/** | ||
* Specifies Tool as Inline Toolbar Tool | ||
* @returns {boolean} | ||
*/ | ||
public static isInline = true; | ||
|
||
/** | ||
* Type of behaviour of the tool if new selection range intersect with existing fragment | ||
* If two fragment intersect, they should be merged | ||
*/ | ||
public intersectType: IntersectType = IntersectType.Extend; | ||
|
||
/** | ||
* Renders wrapper for tool without actual content | ||
* @returns Created html element | ||
*/ | ||
public createWrapper(): HTMLElement { | ||
return make('b'); | ||
} | ||
|
||
/** | ||
* Returns formatting action and range for it to be applied | ||
* @param index - index of current text selection | ||
* @param fragments - all fragments of the bold inline tool inside of the current input | ||
*/ | ||
public getAction(index: TextRange, fragments: InlineFragment[]): FormattingActionWithRange { | ||
return { | ||
action: this.isActive(index, fragments) ? FormattingAction.Unformat : FormattingAction.Format, | ||
range: index, | ||
}; | ||
}; | ||
|
||
/** | ||
* Returns state of the bold inline tool | ||
* @param index - index of current selection | ||
* @param fragments - all fragments of the bold inline tool inside of the current input | ||
* @returns true if tool is active, false otherwise | ||
*/ | ||
public isActive(index: TextRange, fragments: InlineFragment[]): boolean { | ||
let isActive = false; | ||
|
||
fragments.forEach((fragment) => { | ||
/** | ||
* Check if current index is inside of model fragment | ||
*/ | ||
if (index[0] >= fragment.range[0] && index[1] <= fragment.range[1]) { | ||
isActive = true; | ||
|
||
/** | ||
* No need to check other fragments if state already chaned | ||
*/ | ||
return; | ||
} | ||
}); | ||
|
||
return isActive; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/core/src/tools/internal/inline-tools/italic/index.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,72 @@ | ||
import type { FormattingActionWithRange, InlineTool } from '@editorjs/sdk'; | ||
import type { InlineFragment, TextRange } from '@editorjs/model'; | ||
import { FormattingAction } from '@editorjs/model'; | ||
import { IntersectType } from '@editorjs/model'; | ||
import { make } from '@editorjs/dom'; | ||
|
||
/** | ||
* Italic Tool | ||
* | ||
* Inline Toolbar Tool | ||
* | ||
* Makes selected text italic | ||
*/ | ||
export default class ItalicInlineTool implements InlineTool { | ||
/** | ||
* Specifies Tool as Inline Toolbar Tool | ||
* @returns {boolean} | ||
*/ | ||
public static isInline = true; | ||
|
||
/** | ||
* Type of behaviour of the tool if new selection range intersect with existing fragment | ||
* If two fragment intersect, they should be merged | ||
*/ | ||
public intersectType: IntersectType = IntersectType.Extend; | ||
|
||
/** | ||
* Renders wrapper for tool without actual content | ||
* @returns Created html element | ||
*/ | ||
public createWrapper(): HTMLElement { | ||
return make('i'); | ||
} | ||
|
||
/** | ||
* Returns formatting action and range for it to be applied | ||
* @param index - index of current text selection | ||
* @param fragments - all fragments of the bold inline tool inside of the current input | ||
*/ | ||
public getAction(index: TextRange, fragments: InlineFragment[]): FormattingActionWithRange { | ||
return { | ||
action: this.isActive(index, fragments) ? FormattingAction.Unformat : FormattingAction.Format, | ||
range: index, | ||
}; | ||
}; | ||
|
||
/** | ||
* Returns state of the bold inline tool | ||
* @param index - index of current selection | ||
* @param fragments - all fragments of the bold inline tool inside of the current input | ||
* @returns true if tool is active, false otherwise | ||
*/ | ||
public isActive(index: TextRange, fragments: InlineFragment[]): boolean { | ||
let isActive = false; | ||
|
||
fragments.forEach((fragment) => { | ||
/** | ||
* Check if current index is inside of model fragment | ||
*/ | ||
if (index[0] >= fragment.range[0] && index[1] <= fragment.range[1]) { | ||
isActive = true; | ||
|
||
/** | ||
* Don't need to check other fragments if state already chaned | ||
*/ | ||
return; | ||
} | ||
}); | ||
|
||
return isActive; | ||
} | ||
} |
Oops, something went wrong.
50f0db4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for
./packages/model
Test suite run success
389 tests passing in 24 suites.
Report generated by 🧪jest coverage report action from 50f0db4