Skip to content

Commit

Permalink
Merge pull request xdan#218 from superelement/master
Browse files Browse the repository at this point in the history
Issue xdan#217 Indent applying to every element after the first, when multiple elements selected
  • Loading branch information
xdan authored Jun 17, 2019
2 parents 1d0299d + d493fe5 commit e3ff807
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build/jodit.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"build": "rm -f build/* && webpack --mode production --config ./webpack.config.js --progress && npm run build-no-uglify",
"build-no-uglify": "cross-env NODE_ENV=production-no-uflify webpack --progress --mode production",
"test": "karma start --browsers Firefox karma.conf.js --single-run",
"test-ff-watch": "karma start --browsers Firefox karma.conf.js",
"test-chrome-watch": "karma start --browsers Chrome karma.conf.js",
"jodit": "cd ../jodit-react/ && npm update && npm run newversion && cd ../jodit-angular/ && npm run newversion && cd ../jodit-joomla && npm run newversion && cd ../jodit-docs && npm run newversion",
"types": "rm -rf types/* && tsc --project . --declaration --outDir types --emitDeclarationOnly",
"pretty": "prettier --write ./src/*.{ts,less} ./src/**/*.{ts,less} ./src/**/**/*.{ts,less}",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const SPACE_REG_EXP = /[\s\n\t\r\uFEFF\u200b]+/g;
export const SPACE_REG_EXP_START = /^[\s\n\t\r\uFEFF\u200b]+/g;
export const SPACE_REG_EXP_END = /[\s\n\t\r\uFEFF\u200b]+$/g;

export const IS_BLOCK = /^(PRE|DIV|P|LI|H[1-6]|BLOCKQUOTE|TD|TH|TABLE|BODY|HTML|FIGCAPTION|FIGURE)$/i;
export const IS_BLOCK = /^(PRE|DIV|P|LI|H[1-6]|BLOCKQUOTE|TD|TH|TABLE|BODY|HTML|FIGCAPTION|FIGURE|DT|DD)$/i;
export const IS_INLINE = /^(STRONG|SPAN|I|EM|B|SUP|SUB)$/;
export const MAY_BE_REMOVED_WITH_KEY = /^(IMG|BR|IFRAME|SCRIPT|INPUT|TEXTAREA|HR|JODIT|JODIT-MEDIA)$/;

Expand Down
3 changes: 2 additions & 1 deletion src/modules/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ export class Select {
nodes.push(node);
}

return node === end;
// checks parentElement as well because partial selections are not equal to entire element
return node === end || (node && node.contains(end));
},
this.area,
true,
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Config.prototype.indentMargin = 10;
*/
export function indent(editor: IJodit) {
const callback = (command: string): void | false => {
const indentedBoxes: HTMLElement[] = [];
editor.selection.eachSelection(
(current: Node): false | void => {
const selectionInfo = editor.selection.save();
Expand All @@ -77,7 +78,10 @@ export function indent(editor: IJodit) {
return false;
}

if (currentBox && currentBox.style) {
const alreadyIndented: boolean = indentedBoxes.indexOf(currentBox) !== -1;
if (currentBox && currentBox.style && !alreadyIndented) {
indentedBoxes.push(currentBox);

let marginLeft: number = currentBox.style.marginLeft
? parseInt(currentBox.style.marginLeft, 10)
: 0;
Expand Down
2 changes: 2 additions & 0 deletions test/tests/domTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe('Check Dom module', function() {
expect(true).to.equal(Jodit.modules.Dom.isBlock(document.documentElement, window));
expect(true).to.equal(Jodit.modules.Dom.isBlock(document.createElement('div'), window));
expect(true).to.equal(Jodit.modules.Dom.isBlock(document.createElement('table'), window));
expect(true).to.equal(Jodit.modules.Dom.isBlock(document.createElement('dt'), window));
expect(true).to.equal(Jodit.modules.Dom.isBlock(document.createElement('dd'), window));
});
it('Should return false then it gets not BLOCK element', function () {
expect(false).to.equal(Jodit.modules.Dom.isBlock(document.createTextNode('test'), window));
Expand Down
4 changes: 2 additions & 2 deletions test/tests/interfaceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ describe('Test interface', function() {

var sel = editor.editorWindow.getSelection(), range = editor.editorDocument.createRange();

range.setStart(editor.editor.firstChild.firstChild, 5)
range.setStart(editor.editor.firstChild.firstChild, 0)
range.collapse(true)
sel.removeAllRanges();
sel.addRange(range)
Expand All @@ -1416,7 +1416,7 @@ describe('Test interface', function() {

editor.selection.insertHTML('test ');

expect(editor.getEditorValue()).to.equal('<ul><li>Text test to text</li></ul>');
expect(editor.getEditorValue()).to.equal('<ul><li>test Text to text</li></ul>');
});
});
describe('Inline', function () {
Expand Down
89 changes: 89 additions & 0 deletions test/tests/plugins/indent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
describe('Test editor indent plugin', function () {

it(`should indent multi-line selection of various child elements only on 1st 2 lines`, function () {
var editor = new Jodit(appendTestArea());
editor.value = `
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <i>Quonam, inquit, modo?</i> <span>prima quaeque bene</span>
</p>
<blockquote cite="http://loripsum.net">
Nec enim absolvi beata vita sapientis neque ad exitum perduci poterit, si prima quaeque bene ab eo consulta atque facta ipsius oblivione obruentur.
</blockquote>
<pre>
Tum ille: Tu autem cum ipse tantum librorum habeas, quos hic tandem requiris?
</pre>
`;

const children = editor.container.querySelector('.jodit_wysiwyg').children;
const el1 = children[0];
const el1Child = el1.querySelector('i');
const el2 = children[1];
const el3 = children[2];

var sel = editor.editorWindow.getSelection(),
range = editor.editorDocument.createRange();

range.setStart(el1Child, 0);
range.setEnd(el2, 1);

sel.removeAllRanges();
sel.addRange(range);

editor.execCommand('indent');

expect(el1.style.marginLeft).to.be.equal('10px');
expect(el2.style.marginLeft).to.be.equal('10px');
expect(el3.style.marginLeft).to.be.equal('');
});

it(`should indent multi-line selection of "dd" and "dt" child elements only on 1st 2 dt/dd groups`, function () {

var editor = new Jodit(appendTestArea());
editor.value = `
<dl>
<dt><dfn>Falli igitur possumus.</dfn></dt>
<dd>Quid enim ab antiquis ex eo genere, quod ad disserendum valet, praetermissum est?</dd>
<dt><dfn>Scrupulum, inquam, abeunti;</dfn></dt>
<dd>Scio enim esse quosdam, qui quavis lingua philosophari possint;</dd>
<dt><dfn>Poterat autem inpune;</dfn></dt>
<dd>Tum ille: Tu autem cum ipse tantum librorum habeas, quos hic tandem requiris?</dd>
</dl>
`;

const children = editor.container.querySelectorAll('.jodit_wysiwyg dt,.jodit_wysiwyg dd');

const el1 = children[0];
const el2 = children[1];
const el3 = children[2];
const el4 = children[3];
const el5 = children[4];
const el6 = children[5];

var sel = editor.editorWindow.getSelection(),
range = editor.editorDocument.createRange();

range.setStart(el1, 0);
range.setEnd(el4, 1);

sel.removeAllRanges();
sel.addRange(range);

editor.execCommand('indent');

expect(el1.style.marginLeft).to.be.equal('10px');
expect(el2.style.marginLeft).to.be.equal('10px');
expect(el3.style.marginLeft).to.be.equal('10px');
expect(el4.style.marginLeft).to.be.equal('10px');
expect(el5.style.marginLeft).to.be.equal('');
expect(el6.style.marginLeft).to.be.equal('');

});

afterEach(() => {
// console.log(stuff)
removeStuff();
});

})

0 comments on commit e3ff807

Please sign in to comment.