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

Fix #909 Add option to provide pre-defined classes for img elements. #910

Merged
merged 1 commit into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions src/plugins/image-properties/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ declare module 'jodit/config' {
*/
editClass: boolean;

/**
* Pre-define available classes to select from
*
* Classes can be provided as list of strings or as list of tuples
* `["classname", "human label"]`.
*
* @example
* ```javascript
* new Jodit('#editor', {
* image: {
* availableClasses: [
* "rte-image-width-50",
* ["rte-image-width-75", "75 % width"]
* ]
* }
* })
* ```
*/

availableClasses: [string, string][] | string[];

/**
* Show style edit input
*/
Expand Down Expand Up @@ -101,6 +122,7 @@ Config.prototype.image = {
editBorderRadius: true,
editMargins: true,
editClass: true,
availableClasses: [],
editStyle: true,
editId: true,
editAlign: true,
Expand Down
86 changes: 86 additions & 0 deletions src/plugins/image-properties/image-properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,5 +544,91 @@ describe('Edit image tests', function () {
}).timeout(7000);
});
});
describe('Classes', function () {
describe('No available classes defined', function () {
it('Should render as input box', function (done) {
const area = appendTestArea();
const editor = Jodit.make(area, {
history: {
timeout: 0
},
disablePlugins: 'mobile'
});

editor.value =
'<img alt="" src="https://xdsoft.net/jodit/files/th.jpg">';

simulateEvent(
'dblclick',
editor.editor.querySelector('img')
);

const dialog = getOpenedDialog(editor);

expect(dialog).is.not.null;
expect(
dialog.querySelectorAll('[data-ref="editImage"]').length
).equals(1);

expect(
dialog.querySelectorAll('input[data-ref="classes"]')
.length
).equals(1);

done();
}).timeout(7000);
});
describe('Available classes defined', function () {
it('Should render as select box', function (done) {
const area = appendTestArea();
const editor = Jodit.make(area, {
history: {
timeout: 0
},
image: {
availableClasses: [
'rte-image-width-50',
['rte-image-width-75', '75 % width']
]
},
disablePlugins: 'mobile'
});

editor.value =
'<img alt="" src="https://xdsoft.net/jodit/files/th.jpg">';

simulateEvent(
'dblclick',
editor.editor.querySelector('img')
);

const dialog = getOpenedDialog(editor);

expect(dialog).is.not.null;
expect(
dialog.querySelectorAll('[data-ref="editImage"]').length
).equals(1);

expect(
dialog.querySelectorAll('select[data-ref="classes"]')
.length
).equals(1);

const options = [];
dialog
.querySelectorAll('select[data-ref="classes"] option')
.forEach(option => {
options.push([option.value, option.textContent]);
});

expect(options).to.eql([
['rte-image-width-50', 'rte-image-width-50'],
['rte-image-width-75', '75 % width']
]);

done();
}).timeout(7000);
});
});
});
});
23 changes: 22 additions & 1 deletion src/plugins/image-properties/templates/position-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ export function positionTab(editor: IJodit): HTMLElement {
i18n = editor.i18n.bind(editor),
gi = Icon.get.bind(Icon);

const classInput = [];
if (opt.image.availableClasses.length > 0) {
classInput.push(
'<select data-ref="classes" class="jodit-input jodit-select">'
);
opt.image.availableClasses.forEach(item => {
if (typeof item === 'string') {
classInput.push(`<option value="${item}">${item}</option>`);
} else {
classInput.push(
`<option value="${item[0]}">${item[1]}</option>`
);
}
});
classInput.push('</select>');
} else {
classInput.push(
'<input data-ref="classes" type="text" class="jodit-input"/>'
);
}

return editor.c.fromHTML(`<div style="${
!opt.image.editMargins ? 'display:none' : ''
}" class="jodit-form__group">
Expand Down Expand Up @@ -60,7 +81,7 @@ export function positionTab(editor: IJodit): HTMLElement {
!opt.image.editClass ? 'display:none' : ''
}" class="jodit-form__group">
<label>${i18n('Classes')}</label>
<input data-ref="classes" type="text" class="jodit-input"/>
${classInput.join('')}
</div>
<div style="${
!opt.image.editId ? 'display:none' : ''
Expand Down