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

Added support for starting number in ordered list #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

&-settings {
display: flex;
align-items: center;

.cdx-settings-button {
width: 50%;
}
input {
padding: 5px;
}
}
}
93 changes: 72 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require('./index.css').toString();
/**
* @typedef {object} ListData
* @property {string} style - can be ordered or unordered
* @property {number} [start] - optional starting number of ordered list
* @property {Array} items - li elements
*/

Expand Down Expand Up @@ -82,6 +83,12 @@ class List {
icon: '<svg width="17" height="13" viewBox="0 0 17 13" xmlns="http://www.w3.org/2000/svg"><path d="M5.819 4.607h9.362a1.069 1.069 0 0 1 0 2.138H5.82a1.069 1.069 0 1 1 0-2.138zm0-4.607h9.362a1.069 1.069 0 0 1 0 2.138H5.82a1.069 1.069 0 1 1 0-2.138zm0 9.357h9.362a1.069 1.069 0 0 1 0 2.138H5.82a1.069 1.069 0 0 1 0-2.137zM1.468 4.155V1.33c-.554.404-.926.606-1.118.606a.338.338 0 0 1-.244-.104A.327.327 0 0 1 0 1.59c0-.107.035-.184.105-.234.07-.05.192-.114.369-.192.264-.118.475-.243.633-.373.158-.13.298-.276.42-.438a3.94 3.94 0 0 1 .238-.298C1.802.019 1.872 0 1.975 0c.115 0 .208.042.277.127.07.085.105.202.105.351v3.556c0 .416-.15.624-.448.624a.421.421 0 0 1-.32-.127c-.08-.085-.121-.21-.121-.376zm-.283 6.664h1.572c.156 0 .275.03.358.091a.294.294 0 0 1 .123.25.323.323 0 0 1-.098.238c-.065.065-.164.097-.296.097H.629a.494.494 0 0 1-.353-.119.372.372 0 0 1-.126-.28c0-.068.027-.16.081-.273a.977.977 0 0 1 .178-.268c.267-.264.507-.49.722-.678.215-.188.368-.312.46-.371.165-.11.302-.222.412-.334.109-.112.192-.226.25-.344a.786.786 0 0 0 .085-.345.6.6 0 0 0-.341-.553.75.75 0 0 0-.345-.08c-.263 0-.47.11-.62.329-.02.029-.054.107-.101.235a.966.966 0 0 1-.16.295c-.059.069-.145.103-.26.103a.348.348 0 0 1-.25-.094.34.34 0 0 1-.099-.258c0-.132.031-.27.093-.413.063-.143.155-.273.279-.39.123-.116.28-.21.47-.282.189-.072.411-.107.666-.107.307 0 .569.045.786.137a1.182 1.182 0 0 1 .618.623 1.18 1.18 0 0 1-.096 1.083 2.03 2.03 0 0 1-.378.457c-.128.11-.344.282-.646.517-.302.235-.509.417-.621.547a1.637 1.637 0 0 0-.148.187z"/></svg>',
default: true,
},
{
name: 'start',
title: this.api.i18n.t('Starting number'),
icon: '<input type="number"></input>',
default: false,
},
];

/**
Expand All @@ -104,7 +111,7 @@ class List {
* @public
*/
render() {
this._elements.wrapper = this.makeMainTag(this._data.style);
this._elements.wrapper = this.makeMainTag(this._data.style, this._data.start);

// fill with data
if (this._data.items.length) {
Expand Down Expand Up @@ -199,23 +206,45 @@ class List {
const wrapper = this._make('div', [ this.CSS.settingsWrapper ], {});

this.settings.forEach((item) => {
const itemEl = this._make('div', this.CSS.settingsButton, {
innerHTML: item.icon,
});

itemEl.addEventListener('click', () => {
this.toggleTune(item.name);

// clear other buttons
const buttons = itemEl.parentNode.querySelectorAll('.' + this.CSS.settingsButton);

Array.from(buttons).forEach((button) =>
button.classList.remove(this.CSS.settingsButtonActive)
);

// mark active
itemEl.classList.toggle(this.CSS.settingsButtonActive);
});
let itemEl

// check if settings item is button or number input
if (item.name === 'start') {
itemEl = this._make('input', this.CSS.settingsInput, {
type: 'number'
});
itemEl.addEventListener('input', (e) => {
this.setStartNumber(e.target.value);
})
itemEl.value = this._data.start;
} else {
// make buttons
itemEl = this._make('div', this.CSS.settingsButton, {
innerHTML: item.icon,
});

// run click eventlisteners on settings that are buttons
itemEl.addEventListener('click', () => {

this.toggleTune(item.name);

// clear other buttons
const buttons = itemEl.parentNode.querySelectorAll('.' + this.CSS.settingsButton);

Array.from(buttons).forEach((button) =>
button.classList.remove(this.CSS.settingsButtonActive)
);

// clear input value if 'unordered' is selected
if (item.name === 'unordered') {
itemEl.parentNode.querySelector('.' + this.CSS.settingsInput).value = ''
}

// mark active
itemEl.classList.toggle(this.CSS.settingsButtonActive);
});
}

this.api.tooltip.onHover(itemEl, item.title, {
placement: 'top',
Expand All @@ -227,6 +256,7 @@ class List {
}

wrapper.appendChild(itemEl);

});

return wrapper;
Expand Down Expand Up @@ -258,24 +288,28 @@ class List {
* Creates main <ul> or <ol> tag depended on style
*
* @param {string} style - 'ordered' or 'unordered'
* @param {string} [start] - optional start attribute
* @returns {HTMLOListElement|HTMLUListElement}
*/
makeMainTag(style){
makeMainTag(style, start){
const styleClass = style === 'ordered' ? this.CSS.wrapperOrdered : this.CSS.wrapperUnordered;
const tag = style === 'ordered' ? 'ol' : 'ul';

if (!start || start === 0) start = 1;

return this._make(tag, [this.CSS.baseBlock, this.CSS.wrapper, styleClass], {
contentEditable: !this.readOnly,
contentEditable: !this.readOnly, start
});
}

/**
* Toggles List style
*
* @param {string} style - 'ordered'|'unordered'
* @param {number} [number] - starting number of ordered list
*/
toggleTune(style) {
const newTag = this.makeMainTag(style);
toggleTune(style, number) {
const newTag = this.makeMainTag(style, number);

while (this._elements.wrapper.hasChildNodes()) {
newTag.appendChild(this._elements.wrapper.firstChild);
Expand All @@ -284,6 +318,18 @@ class List {
this._elements.wrapper.replaceWith(newTag);
this._elements.wrapper = newTag;
this._data.style = style;
if (style === 'unordered') this._data.start = null;
else if (number) this._data.start = number;
}

/**
* Set start number attribute
*
* @param {string} num
*/
setStartNumber(num) {
// setting number must default style to ordered list
this.toggleTune('ordered', Number(num));
}

/**
Expand All @@ -301,6 +347,7 @@ class List {
settingsWrapper: 'cdx-list-settings',
settingsButton: this.api.styles.settingsButton,
settingsButtonActive: this.api.styles.settingsButtonActive,
settingsInput: this.api.styles.input
};
}

Expand All @@ -315,6 +362,7 @@ class List {
}

this._data.style = listData.style || this.settings.find((tune) => tune.default === true).name;
this._data.start = listData.start;
this._data.items = listData.items || [];

const oldView = this._elements.wrapper;
Expand Down Expand Up @@ -463,10 +511,12 @@ class List {
pasteHandler(element) {
const { tagName: tag } = element;
let style;
let start;

switch (tag) {
case 'OL':
style = 'ordered';
start = 1;
break;
case 'UL':
case 'LI':
Expand All @@ -475,6 +525,7 @@ class List {

const data = {
style,
start,
items: [],
};

Expand Down