-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Link Inline Tool #264
Merged
Merged
Link Inline Tool #264
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d7b88cf
Link Inline Tool
neSpecc cdd59ac
Merge branch 'rewriting-version2.0' into link-inline-tool
neSpecc c93ed65
api injected
neSpecc 244569f
text improved
neSpecc f5fc6b1
Clear input on the reselection
neSpecc 3922a53
little improvements
neSpecc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,294 @@ | ||
import InlineTool from '../interfaces/inline-tool'; | ||
import Selection from '../selection'; | ||
|
||
declare var $: any; | ||
declare var _: any; | ||
|
||
/** | ||
* Link Tool | ||
* | ||
* Inline Toolbar Tool | ||
* | ||
* Wrap selected text with <a> tag | ||
*/ | ||
export default class LinkInlineTool implements InlineTool { | ||
|
||
/** | ||
* Native Document's commands for link/unlink | ||
*/ | ||
private readonly commandLink: string = 'createLink'; | ||
private readonly commandUnlink: string = 'unlink'; | ||
|
||
/** | ||
* Enter key code | ||
*/ | ||
private readonly ENTER_KEY: number = 13; | ||
|
||
/** | ||
* Styles | ||
*/ | ||
private readonly CSS = { | ||
button: 'ce-inline-tool', | ||
buttonActive: 'ce-inline-tool--active', | ||
buttonModifier: 'ce-inline-tool--link', | ||
buttonUnlink: 'ce-inline-tool--unlink', | ||
input: 'ce-inline-tool-input', | ||
inputShowed: 'ce-inline-tool-input--showed', | ||
}; | ||
|
||
/** | ||
* Elements | ||
*/ | ||
private nodes = { | ||
button: null, | ||
input: null, | ||
}; | ||
|
||
/** | ||
* Selection instance | ||
*/ | ||
private selection: Selection; | ||
|
||
/** | ||
* Input opening state | ||
*/ | ||
private inputOpened: boolean = false; | ||
|
||
/** | ||
* Available Inline Toolbar methods (open/close) | ||
*/ | ||
private inlineToolbar: any; | ||
|
||
/** | ||
* @param {object} api - CodeX Editor API | ||
* @param {object} api.toolbar - Inline Toolbar API | ||
*/ | ||
constructor(api) { | ||
this.inlineToolbar = api.toolbar; | ||
this.selection = new Selection(); | ||
} | ||
|
||
/** | ||
* Create button for Inline Toolbar | ||
*/ | ||
public render(): HTMLElement { | ||
this.nodes.button = document.createElement('button'); | ||
this.nodes.button.classList.add(this.CSS.button, this.CSS.buttonModifier); | ||
this.nodes.button.appendChild($.svg('link', 15, 14)); | ||
this.nodes.button.appendChild($.svg('unlink', 16, 18)); | ||
return this.nodes.button; | ||
} | ||
|
||
/** | ||
* Input for the link | ||
*/ | ||
public renderActions(): HTMLElement { | ||
this.nodes.input = document.createElement('input'); | ||
this.nodes.input.placeholder = 'Add a link'; | ||
this.nodes.input.classList.add(this.CSS.input); | ||
this.nodes.input.addEventListener('keydown', (event) => { | ||
if (event.keyCode === this.ENTER_KEY ) { | ||
this.enterPressed(event); | ||
} | ||
}); | ||
return this.nodes.input; | ||
} | ||
|
||
/** | ||
* Wrap range with <b> tag | ||
* @param {Range} range | ||
*/ | ||
public surround(range: Range): void { | ||
|
||
/** | ||
* Save selection before change focus to the input | ||
*/ | ||
this.selection.save(); | ||
|
||
const parentAnchor = this.selection.findParentTag('A'); | ||
|
||
if (parentAnchor) { | ||
this.selection.expandToTag(parentAnchor); | ||
this.unlink(); | ||
this.closeActions(); | ||
this.checkState(); | ||
this.inlineToolbar.close(); | ||
return; | ||
} | ||
|
||
this.toggleActions(); | ||
} | ||
|
||
/** | ||
* Check selection and set activated state to button if there are <b> tag | ||
* @param {Selection} selection | ||
*/ | ||
public checkState(selection?: Selection): boolean { | ||
const anchorTag = this.selection.findParentTag('A'); | ||
|
||
if (anchorTag) { | ||
this.nodes.button.classList.add(this.CSS.buttonUnlink); | ||
this.nodes.button.classList.add(this.CSS.buttonActive); | ||
this.openActions(); | ||
|
||
/** | ||
* Fill input value with link href | ||
*/ | ||
const hrefAttr = anchorTag.getAttribute('href'); | ||
this.nodes.input.value = hrefAttr !== 'null' ? hrefAttr : ''; | ||
} else { | ||
this.nodes.button.classList.remove(this.CSS.buttonUnlink); | ||
this.nodes.button.classList.remove(this.CSS.buttonActive); | ||
} | ||
|
||
return !!anchorTag; | ||
} | ||
|
||
private toggleActions() { | ||
if (!this.inputOpened) { | ||
this.openActions(true); | ||
} else { | ||
this.closeActions(); | ||
} | ||
} | ||
|
||
/** | ||
* @param {boolean} needFocus - on link creation we need to focus input. On editing - nope. | ||
*/ | ||
private openActions(needFocus: boolean = false) { | ||
this.nodes.input.classList.add(this.CSS.inputShowed); | ||
if (needFocus) { | ||
this.nodes.input.focus(); | ||
} | ||
} | ||
|
||
/** | ||
* Close input | ||
*/ | ||
private closeActions() { | ||
this.nodes.input.classList.remove(this.CSS.inputShowed); | ||
this.nodes.input.value = ''; | ||
} | ||
|
||
/** | ||
* Enter pressed on input | ||
* @param {KeyboardEvent} event | ||
*/ | ||
private enterPressed(event: KeyboardEvent) { | ||
let value = this.nodes.input.value || ''; | ||
|
||
if (!value.trim()) { | ||
this.selection.restore(); | ||
this.unlink(); | ||
event.preventDefault(); | ||
this.closeActions(); | ||
} | ||
|
||
if (!this.validateURL(value)) { | ||
/** | ||
* @todo show notification 'Incorrect Link' | ||
*/ | ||
_.log('Incorrect Link pasted', 'warn', value); | ||
return; | ||
} | ||
|
||
value = this.prepareLink(value); | ||
|
||
/** | ||
* Add 'http' protocol if need | ||
*/ | ||
value = this.addProtocol(value); | ||
|
||
this.selection.restore(); | ||
this.insertLink(value); | ||
|
||
/** | ||
* Preventing events that will be able to happen | ||
*/ | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
event.stopImmediatePropagation(); | ||
|
||
this.closeActions(); | ||
this.inlineToolbar.close(); | ||
this.checkState(); | ||
} | ||
|
||
/** | ||
* Detects if passed string is URL | ||
* @param {string} str | ||
* @return {Boolean} | ||
*/ | ||
private validateURL(str: string): boolean { | ||
/** | ||
* Don't allow spaces | ||
*/ | ||
return !/\s/.test(str); | ||
|
||
} | ||
/** | ||
* Process link before injection | ||
* - sanitize | ||
* - add protocol for links like 'google.com' | ||
* @param {string} link - raw user input | ||
*/ | ||
private prepareLink(link: string): string { | ||
link = link.trim(); | ||
link = this.addProtocol(link); | ||
return link; | ||
} | ||
|
||
/** | ||
* Add 'http' protocol to the links like 'vc.ru', 'google.com' | ||
* @param {String} link | ||
*/ | ||
private addProtocol(link: string): string { | ||
/** | ||
* If protocol already exists, do nothing | ||
*/ | ||
if (/^(https?|mailto|ftp):\/\//.test(link)) { | ||
return link; | ||
} | ||
|
||
/** | ||
* We need to add missed HTTP protocol to the link, but skip 2 cases: | ||
* 1) Internal links like "/general" | ||
* 2) Anchors looks like "#results" | ||
* 3) Protocol-relative URLs like "//google.com" | ||
*/ | ||
const isInternal = /^\/[^\/\s]/.test(link), | ||
isAnchor = link.substring(0, 1) === '#', | ||
isProtocolRelative = /^\/\/[^\/\s]/.test(link); | ||
|
||
if (!isInternal && !isAnchor && !isProtocolRelative) { | ||
link = 'http://' + link; | ||
} | ||
|
||
return link; | ||
} | ||
|
||
/** | ||
* Inserts <a> tag with "href" | ||
* @param {string} link - "href" value | ||
*/ | ||
private insertLink(link: string): void { | ||
|
||
/** | ||
* Edit all link, not selected part | ||
*/ | ||
const anchorTag = this.selection.findParentTag('A'); | ||
|
||
if (anchorTag) { | ||
this.selection.expandToTag(anchorTag); | ||
} | ||
|
||
document.execCommand(this.commandLink, false, link); | ||
} | ||
|
||
/** | ||
* Removes <a> tag | ||
*/ | ||
private unlink(): void { | ||
document.execCommand(this.commandUnlink); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
убрать, уже есть в prepare