Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhtranchau committed May 6, 2018
1 parent 05191c1 commit 3cdce44
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 109 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"@types/ace": "^0.0.35",
"@types/es6-promise": "^3.3.0",
"@types/node": "^7.0.61",
"@types/node": "^7.0.63",
"autoprefixer": "^6.7.7",
"awesome-typescript-loader": "^3.4.1",
"chai": "^3.5.0",
Expand All @@ -56,7 +56,7 @@
"less-loader": "^4.1.0",
"mocha": "^3.4.2",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"postcss-loader": "^2.1.4",
"postcss-loader": "^2.1.5",
"precss": "^2.0.0",
"style-loader": "^0.13.2",
"svg-inline-loader": "^0.8.0",
Expand Down
16 changes: 15 additions & 1 deletion src/modules/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ export const dom = (html: string | HTMLElement, doc: Document): HTMLElement => {

div.innerHTML = <string>html;

return (div.firstChild !== div.lastChild || !div.firstChild) ? div : <HTMLElement>div.firstChild;
const child: HTMLElement = (div.firstChild !== div.lastChild || !div.firstChild) ? div : <HTMLElement>div.firstChild;

child.parentNode && child.parentNode.removeChild(child);

return child;
};

/**
Expand Down Expand Up @@ -1206,4 +1210,14 @@ export class JoditObject {
export const getRange = () => {
var range = window.getSelection().getRangeAt(0);
return [range.startContainer, range.startOffset, range.endContainer, range.endOffset];
};

export const innerWidth = (element: HTMLElement, win: Window): number => {
const computedStyle: CSSStyleDeclaration = win.getComputedStyle(element);

let elementWidth: number = element.clientWidth; // width with padding

elementWidth -= parseFloat(computedStyle.paddingLeft || '0') + parseFloat(computedStyle.paddingRight || '0');

return elementWidth;
};
26 changes: 19 additions & 7 deletions src/plugins/resizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {Jodit} from '../Jodit';
import {Config} from '../Config'
import * as consts from '../constants'
import {$$, css, debounce, dom, isIE, offset} from '../modules/Helpers'
import {$$, css, debounce, dom, innerWidth, isIE, offset} from '../modules/Helpers'

/**
* The module creates a supporting frame for resizing of the elements img and table
Expand Down Expand Up @@ -106,8 +106,10 @@ export function resizer(editor: Jodit) {
if (!editor.options.resizer.showSize) {
return;
}

sizeViewer.style.opacity = '1';
sizeViewer.innerHTML = `${w} x ${h}`;

window.clearTimeout(timeoutSizeViewer);
timeoutSizeViewer = window.setTimeout(() => {
sizeViewer.style.opacity = '0';
Expand Down Expand Up @@ -147,6 +149,10 @@ export function resizer(editor: Jodit) {
return;
}

if (!resizer.parentNode) {
editor.ownerDocument.body.appendChild(resizer);
}

resizerIsVisible = true;
resizer.style.display = 'block';

Expand Down Expand Up @@ -269,7 +275,6 @@ export function resizer(editor: Jodit) {
}
})
.on('afterInit', () => {
editor.ownerDocument.body.appendChild(resizer);
editor.events
.on(editor.editor, 'keydown', (e: KeyboardEvent) => {
if (resizerIsVisible && e.keyCode === consts.KEY_DELETE && currentElement && currentElement.tagName.toLowerCase() !== 'table') {
Expand All @@ -287,25 +292,32 @@ export function resizer(editor: Jodit) {
})
.on(editor.ownerWindow, 'mousemove touchmove', (e: MouseEvent) => {
if (isResizing) {
// resized = true;
diff_x = e.clientX - start_x;
diff_y = e.clientY - start_y;

if (!currentElement) {
return;
}

const className: string = handle.className;

if ('IMG' === currentElement.tagName) {
if (diff_x) {
new_w = width + (handle.className.match(/left/) ? -1 : 1) * diff_x;
new_w = width + (className.match(/left/) ? -1 : 1) * diff_x;
new_h = Math.round(new_w / ratio);
} else {
new_h = height + (handle.className.match(/top/) ? -1 : 1) * diff_y;
new_h = height + (className.match(/top/) ? -1 : 1) * diff_y;
new_w = Math.round(new_h * ratio);
}

if (new_w > innerWidth(editor.editor, editor.ownerWindow)) {
new_w = innerWidth(editor.editor, editor.ownerWindow);
new_h = Math.round(new_w / ratio);
}

} else {
new_w = width + (handle.className.match(/left/) ? -1 : 1) * diff_x;
new_h = height + (handle.className.match(/top/) ? -1 : 1) * diff_y;
new_w = width + (className.match(/left/) ? -1 : 1) * diff_x;
new_h = height + (className.match(/top/) ? -1 : 1) * diff_y;
}

if (new_w > editor.options.resizer.min_width) {
Expand Down
1 change: 1 addition & 0 deletions test/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ if ((typeof window.chai !== 'undefined')) {
break;
case 'getLocalFileByUrl':
switch (ajax.options.data.url) {
case location.protocol + '//' + location.host + '/test/tests/artio.jpg':
case 'https://xdsoft.net/jodit/files/th.jpg':
resolve({"success":true,"time":"2018-03-15 12:55:00","data":{"path":"","name":"th.jpg","source":"default","code":220}});
break;
Expand Down
8 changes: 4 additions & 4 deletions test/tests/enterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ describe('Enter behavior Jodit Editor Tests', function() {
describe('Backspace key', function () {
it('Should remove that element', function () {
var editor = new Jodit(appendTestArea())
editor.setEditorValue('<p><img src="https://xdsoft.net/jodit/images/artio.jpg"/>test</p>');
editor.setEditorValue('<p><img src="tests/artio.jpg"/>test</p>');

var sel = editor.editorWindow.getSelection(),
range = editor.editorDocument.createRange();
Expand All @@ -361,7 +361,7 @@ describe('Enter behavior Jodit Editor Tests', function() {
describe('Delete key', function () {
it('Should remove that element', function () {
var editor = new Jodit(appendTestArea())
editor.setEditorValue('<p>test<img src="https://xdsoft.net/jodit/images/artio.jpg"/></p>');
editor.setEditorValue('<p>test<img src="tests/artio.jpg"/></p>');

var sel = editor.editorWindow.getSelection(),
range = editor.editorDocument.createRange();
Expand Down Expand Up @@ -1190,7 +1190,7 @@ describe('Enter behavior Jodit Editor Tests', function() {
editor.setEditorValue('<ul>' +
'<li>1</li>' +
'<li>2</li>' +
'<li><img style="width:30px" src="https://xdsoft.net/jodit/images/artio.jpg"></li>' +
'<li><img style="width:30px" src="tests/artio.jpg"></li>' +
'</ul>');

editor.selection.setCursorBefore(editor.editor.firstChild.lastChild.firstChild);
Expand All @@ -1202,7 +1202,7 @@ describe('Enter behavior Jodit Editor Tests', function() {
'<li>1</li>' +
'<li>2</li>' +
'<li><br></li>' +
'<li>split <img src="https://xdsoft.net/jodit/images/artio.jpg" style="width:30px"></li>' +
'<li>split <img src="tests/artio.jpg" style="width:30px"></li>' +
'</ul>').to.be.equal(sortAtrtibutes(editor.getEditorValue()));
});
});
Expand Down
Loading

0 comments on commit 3cdce44

Please sign in to comment.