Skip to content
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 6 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
448 changes: 436 additions & 12 deletions build/codex-editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/codex-editor.js.map

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions build/sprite.svg
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
3 changes: 3 additions & 0 deletions src/assets/link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/assets/icon-plus.svg → src/assets/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/assets/unlink.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/components/inline-tools/inline-tool-bold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class BoldInlineTool implements InlineTool {
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('icon-bold', 13, 15));
this.nodes.button.appendChild($.svg('bold', 13, 15));
return this.nodes.button;
}

Expand Down
293 changes: 293 additions & 0 deletions src/components/inline-tools/inline-tool-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
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 - Inline Toolbar API
*/
constructor(api) {
this.inlineToolbar = api;
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрать, уже есть в prepare


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);
}
}
6 changes: 6 additions & 0 deletions src/components/interfaces/inline-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export default interface InlineTool {
*/
render(): HTMLElement;

/**
* Make additional element with actions
* For example, input for the 'link' tool or textarea for the 'comment' tool
*/
renderActions?(): HTMLElement;

/**
* Method that accepts selected range and wrap it somehow
* @param {Range} range - selection's range
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/api-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default class BlocksAPI extends Module implements IBlocksAPI {
get methods(): IBlocksAPI {
return {
moveDown: () => this.moveDown(),
moveUp: () => this.moveUp()
}
moveUp: () => this.moveUp(),
};
}

public moveDown(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/components/modules/api-sanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class SanitizerAPI extends Module implements ISanitizerAPI {
get methods(): ISanitizerAPI {
return {
clean: (taintString, config) => this.clean(taintString, config),
}
};
}

public clean(taintString, config) {
Expand Down
Loading