Skip to content

adding typescript support#47

Open
arenac wants to merge 3 commits intoeditor-js:masterfrom
arenac:typescript-support
Open

adding typescript support#47
arenac wants to merge 3 commits intoeditor-js:masterfrom
arenac:typescript-support

Conversation

@arenac
Copy link
Copy Markdown

@arenac arenac commented Jun 10, 2024

Adding Typescript support to improve developement experience.
And generating types declarion during build, to be published and also to improve development experirce for the library users.

Copy link
Copy Markdown
Contributor

@neSpecc neSpecc left a comment

Choose a reason for hiding this comment

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

Nice work

Comment thread src/index.ts Outdated
* @property {boolean} checked - is the item checked
*/
import './polyfills.js';
interface ChecklistItem {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lets export it

Comment thread src/index.ts Outdated
* @typedef {object} ChecklistData
* @property {ChecklistItem[]} items - checklist elements
*/
interface ChecklistData {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and this

Comment thread package.json Outdated
"scripts": {
"dev": "vite",
"build": "vite build"
"build": "tsc && vite build"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

vite will automatically compile the .ts file, isn't it?

Comment thread src/index.ts
Comment on lines +34 to +37
private _elements: { wrapper: HTMLElement | null; items: HTMLElement[] };
private readOnly: boolean;
private api: any;
private data: ChecklistData;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please, add docs for all interfaces and properties

Comment thread src/index.ts Outdated
Comment on lines +7 to +9
* @typedef {object} ChecklistItem
* @property {string} text - item label
* @property {boolean} checked - is the item checked
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

move docs to each property, please

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

still actual. Typedef is not needed anymore, use comments near properties instead

Comment thread src/index.ts Outdated

const items = this.items;
const currentItem = document.activeElement.closest(`.${this.CSS.item}`);
const currentItem = document.activeElement?.closest(`.${this.CSS.item}`) as HTMLElement;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same

Comment thread src/index.ts
backspace(event) {
const currentItem = event.target.closest(`.${this.CSS.item}`);
backspace(event: KeyboardEvent): void {
const currentItem = (event.target as HTMLElement).closest(`.${this.CSS.item}`) as HTMLElement;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

still actual

Comment thread src/index.ts
}

const selection = window.getSelection();
const selection = window.getSelection() as Selection;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and here

Comment thread src/utils.ts Outdated
Comment on lines +1 to +5
/**
* Remove and return HTML content after carer position in current input
*
* @returns {DocumentFragment} extracted HTML nodes
*/
export function extractContentAfterCaret() {
const input = document.activeElement;
const selection = window.getSelection();
* Remove and return HTML content after carer position in current input
*
* @returns {DocumentFragment} extracted HTML nodes
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bad "*" aligning

Comment thread src/utils.ts
Comment on lines +7 to +8
const input = document.activeElement as HTMLElement;
const selection = window.getSelection() as Selection;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

unexpected casting

@arenac arenac force-pushed the typescript-support branch 2 times, most recently from da57ad8 to 6d8a249 Compare June 17, 2024 20:31
@arenac arenac force-pushed the typescript-support branch from 6d8a249 to 9c848ec Compare June 17, 2024 20:33
@arenac
Copy link
Copy Markdown
Author

arenac commented Jun 17, 2024

@neSpecc thanks for the review so far. I pushed some udpates.

Comment thread src/index.ts Outdated
Comment on lines +7 to +9
* @typedef {object} ChecklistItem
* @property {string} text - item label
* @property {boolean} checked - is the item checked
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

still actual. Typedef is not needed anymore, use comments near properties instead

Comment thread src/index.ts
* @returns {{export: Function, import: Function}}
*/
static get conversionConfig() {
static get conversionConfig(): { export: (data: ChecklistData) => string; import: (str: string) => ChecklistData } {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

show me the error you got. I think, it should work fine. Maybe you need to make ChecklistData extend BlockToolData ?

Comment thread src/index.ts Outdated
if (isLastItem) {
const currentItemText = getHTML(this.getItemInput(currentItem));
const isEmptyItem = currentItemText.length === 0;
if (isLastItem && currentItem) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this change can break the logic. Its better to handle currentItem=undefined case separately above

Comment thread src/index.ts
backspace(event) {
const currentItem = event.target.closest(`.${this.CSS.item}`);
backspace(event: KeyboardEvent): void {
const currentItem = (event.target as HTMLElement).closest(`.${this.CSS.item}`) as HTMLElement;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

still actual

Comment thread src/index.ts
* Checklist Tool for the Editor.js 2.0
*/
export default class Checklist {
/** @private HTML nodes */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@private is not needed in typescript since you explicitly specify access modifier

Comment thread package.json
{
"name": "@editorjs/checklist",
"version": "1.6.0",
"version": "1.7.0",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
"version": "1.7.0",
"version": "1.6.1",

Comment thread src/index.ts
* @property {boolean} checked - is the item checked
* CheckList constructor options
*/
export interface ChecklistOptions {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lets use the BlockToolConstructorOptions the from editorjs package

Comment thread src/index.ts
* @returns {{icon: string, title: string}}
*/
static get toolbox() {
static get toolbox(): { icon: string; title: string } {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
static get toolbox(): { icon: string; title: string } {
static get toolbox(): ToolboxConfig {

Comment thread src/index.ts
/**
* Checklist Tool for the Editor.js 2.0
*/
export default class Checklist {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
export default class Checklist {
export default class Checklist implements BlockTool {

Comment thread src/index.ts
checkListItem.classList.toggle(this.CSS.itemChecked);
checkbox.classList.add(this.CSS.noHover);
checkbox.addEventListener('mouseleave', () => this.removeSpecialHoverBehavior(checkbox), { once: true });
toggleCheckbox(target: HTMLElement | null, event: MouseEvent): void {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why target has been added?

Comment thread src/utils.ts
export function moveCaret(element: HTMLElement, toStart: boolean = false, offset?: number): void {
const range = document.createRange();
const selection = window.getSelection();
const selection = window.getSelection() as Selection;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

casting is not expected here, it can lead errors since there can be no selection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants