-
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): Inline tool adapter check for tool required data (#…
…87) * 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 * added inline tool data former * fix lint * rm unwanted changes * lint fix * fixed build * docs improved * fix build * naming improved * Update packages/core/src/ui/InlineToolbar/index.ts Co-authored-by: Peter <[email protected]> * Update packages/dom-adapters/src/FormattingAdapter/index.ts Co-authored-by: Peter <[email protected]> * rm unwanted change * naming * separated renderToolActions and apply method in formatting adapter * naming * moved surround to utils * lint fix * last naming fix 🤞 * made renderToolActions method private --------- Co-authored-by: George Berezhnoy <[email protected]> Co-authored-by: George Berezhnoy <[email protected]> Co-authored-by: Peter <[email protected]>
- Loading branch information
1 parent
c04c0ea
commit 83662ff
Showing
12 changed files
with
321 additions
and
46 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
101 changes: 101 additions & 0 deletions
101
packages/core/src/tools/internal/inline-tools/link/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,101 @@ | ||
import type { ActionsElementWithOptions, ToolFormattingOptions, InlineTool, InlineToolFormatData } 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'; | ||
|
||
/** | ||
* Link Tool | ||
* | ||
* Inline Toolbar Tool | ||
* | ||
* Makes selected text linked | ||
*/ | ||
export default class LinkInlineTool 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, existing fragment should be replaced with new one | ||
*/ | ||
public intersectType: IntersectType = IntersectType.Replace; | ||
|
||
/** | ||
* Renders wrapper for tool without actual content | ||
* @param data - inline tool data formed in toolbar | ||
* @returns Created html element | ||
*/ | ||
public createWrapper(data: InlineToolFormatData): HTMLElement { | ||
const linkElement = make('a') as HTMLLinkElement; | ||
|
||
if (typeof data.link === 'string') { | ||
linkElement.href = data.link; | ||
} | ||
|
||
return linkElement; | ||
} | ||
|
||
/** | ||
* Returns formatting action and range for it to be applied | ||
* @param range - range of current text selection | ||
* @param fragments - all fragments of the bold inline tool inside of the current input | ||
*/ | ||
public getFormattingOptions(range: TextRange, fragments: InlineFragment[]): ToolFormattingOptions { | ||
return { | ||
action: this.isActive(range, fragments) ? FormattingAction.Unformat : FormattingAction.Format, | ||
range, | ||
}; | ||
}; | ||
|
||
/** | ||
* Returns state of the bold inline tool | ||
* @param range - range 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(range: TextRange, fragments: InlineFragment[]): boolean { | ||
let isActive = false; | ||
|
||
fragments.forEach((fragment) => { | ||
/** | ||
* Check if current index is inside of model fragment | ||
*/ | ||
if (range[0] === fragment.range[0] && range[1] === fragment.range[1]) { | ||
isActive = true; | ||
|
||
/** | ||
* No need to check other fragments if state already chaned | ||
*/ | ||
return; | ||
} | ||
}); | ||
|
||
return isActive; | ||
} | ||
|
||
/** | ||
* Function that is responsible for rendering data form element | ||
* @param callback function that should be triggered, when data completely formed | ||
* @returns rendered data form element with options required in toolbar | ||
*/ | ||
public renderActions(callback: (data: InlineToolFormatData) => void): ActionsElementWithOptions { | ||
const linkInput = make('input') as HTMLInputElement; | ||
|
||
linkInput.addEventListener('keydown', (event: KeyboardEvent) => { | ||
if (event.key === 'Enter') { | ||
/** | ||
* Remove link input, when data formed and trigger callback | ||
*/ | ||
linkInput.remove(); | ||
|
||
callback({ link: linkInput.value }); | ||
} | ||
}); | ||
|
||
return { element: linkInput }; | ||
} | ||
} |
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
Oops, something went wrong.
83662ff
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 83662ff
83662ff
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/collaboration-manager
Test suite run success
6 tests passing in 1 suite.
Report generated by 🧪jest coverage report action from 83662ff