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

feat(base): added utils #1

Merged
merged 36 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
174d541
fixed lint errors
e11sy Aug 12, 2024
e75dfe2
added utils
e11sy Aug 12, 2024
274c71c
lint and build fix
e11sy Aug 12, 2024
6f94c22
update gitignore
e11sy Aug 12, 2024
040aeb4
build fix
e11sy Aug 12, 2024
709aafa
build
e11sy Aug 12, 2024
cc2cb0d
added flatconfig for eslint
e11sy Aug 13, 2024
843cbd7
fixed some eslint problems
e11sy Aug 13, 2024
4450be7
removed unneded dom methods
e11sy Aug 13, 2024
1ed6b49
removed unwanted utils
e11sy Aug 13, 2024
cca0323
added helpers package
e11sy Aug 13, 2024
0862046
eslint fix
e11sy Aug 13, 2024
b2db608
Dom relation removed from helprers
e11sy Aug 13, 2024
9057231
sesparate helpers
e11sy Aug 13, 2024
0d1441c
renaming
e11sy Aug 13, 2024
ed82ec7
export functions from helpers index
e11sy Aug 13, 2024
5779e54
separated some Dom funcitons
e11sy Aug 13, 2024
7410c1c
Dom fix
e11sy Aug 13, 2024
a9a9da9
separated Dom methods
e11sy Aug 13, 2024
01c6538
fixed build and eslint
e11sy Aug 13, 2024
d26c094
typo
e11sy Aug 13, 2024
93f1280
fix dependencies installation
e11sy Aug 13, 2024
3ac46c9
build fix
e11sy Aug 13, 2024
cb2e481
workflow fix
e11sy Aug 13, 2024
729326a
updated package dependencies
e11sy Aug 14, 2024
d2d7c2a
updated yarn
e11sy Aug 14, 2024
87bb1ac
rm build locks
e11sy Aug 14, 2024
f607595
update lock files
e11sy Aug 14, 2024
bee15da
gitignore update
e11sy Aug 14, 2024
1bb50af
update workflow
e11sy Aug 14, 2024
11c1ce4
fix
e11sy Aug 14, 2024
2930b55
update workflow
e11sy Aug 14, 2024
ff102a4
fix
e11sy Aug 14, 2024
1301e6b
packages updated
e11sy Aug 14, 2024
c17a865
add npm publish action
e11sy Aug 14, 2024
73cb97f
update node version in workflow
e11sy Aug 14, 2024
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
8 changes: 0 additions & 8 deletions .eslintrc

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
build:
name: Build notes.web
name: Build editorjs/utils
runs-on: ubuntu-latest

steps:
Expand All @@ -19,7 +19,7 @@ jobs:
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 20
node-version: 22.1.0

- name: Install dependencies
run: yarn install
Expand Down
15 changes: 4 additions & 11 deletions .github/workflows/eslint-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@ jobs:
uses: actions/checkout@v3

- name: Install dependencies
run: |
yarn install
run: yarn install

- name: Run ESLint
run: yarn lint
- name: Build packages
run: yarn build

- name: Install codex-ui dependencies
run: |
yarn install
working-directory: ./codex-ui

- name: Run ESLint on codex-ui
- name: Run ESLint
run: yarn lint
working-directory: ./codex-ui
13 changes: 13 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Publish package to NPM

on:
push:
branches:
- main

jobs:
publish-and-notify:
uses: codex-team/github-workflows/.github/workflows/npm-publish-and-notify-reusable.yml@main
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
CODEX_BOT_NOTIFY_EDITORJS_PUBLIC_CHAT: ${{ secrets.CODEX_BOT_NOTIFY_EDITORJS_PUBLIC_CHAT }}
123 changes: 4 additions & 119 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,123 +1,8 @@
# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# build filse
dist/

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
# module files
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
# build info lock
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IDE
.idea

build
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.1.0
Binary file added .yarn/install-state.gz
Binary file not shown.
925 changes: 925 additions & 0 deletions .yarn/releases/yarn-4.4.0.cjs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


yarn-path ".yarn/releases/yarn-4.4.0.cjs"
3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.4.0.cjs
24 changes: 21 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
/** eslint-disable-next-line n/no-unpublished-import */
import CodeX from 'eslint-config-codex';

import { plugin as TsPlugin, parser as TsParser } from 'typescript-eslint';
/**
* @todo connect architecture config
*/
export default [
...CodeX,
{
name: 'ts-editorjs/utils',

ignores: ['eslint.config.mjs'],
/**
* This are the options for typescript files
*/
languageOptions: {
parser: TsParser,
parserOptions: {
project: './tsconfig.json',
project: './tsconfig.eslint.json',
tsconfigRootDir: './',
sourceType: 'module', // Allows for the use of imports
},
},
plugins: {
'@typescript-eslint': TsPlugin,
},

rules: {
'n/no-missing-import': ['off'],
'n/no-extraneous-import': ['error', {
allowModules: ['nanoid'],
}],
'n/no-unpublished-import': ['off'],
'n/no-unsupported-features/node-builtins': ['error', {
version: '>=22.1.0',
allowExperimental: true,
}],
'@typescript-eslint/naming-convention': ['error', {
selector: 'property',
format: ['UPPER_CASE', 'camelCase', 'PascalCase'],
}],
},
},
];
35 changes: 25 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
{
"name": "utils",
"name": "@editorjs/utils",
"private": true,
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
"workspaces": [
"packages/caret",
"packages/dom",
"packages/helpers",
"packages/keyboard"
],
"version": "1.0.0",
"description": "Utils that are used by Editor.js and it's tools",
"main": "build/index.js",
"repository": "https://github.com/editor-js/utils",
"description": "Useful utils for working with Editor.js",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/editor-js/utils.git"
},
"author": "CodeX Team <all@codex.so>",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"build": "yarn build:helpers && yarn build:dom && yarn build:caret && yarn build:keyboard && tsc",
"build:helpers": "cd packages/helpers && yarn build",
"build:dom": "cd packages/dom && yarn build",
"build:caret": "cd packages/caret && yarn build",
"build:keyboard": "cd packages/keyboard && yarn build",
"lint": "eslint",
"lint:fix": "yarn lint --fix"
},
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"eslint": "^9.3.0",
"@types/node": "^20.10.7",
"eslint": "^9.0.0",
"eslint-config-codex": "^2.0.0",
"ts-node": "^9.1.1",
"typescript": "^5.4.5"
}
"typescript": "^5.4.5",
"typescript-eslint": "^7.9.0"
},
"packageManager": "yarn@4.4.0"
}
12 changes: 12 additions & 0 deletions packages/caret/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@editorjs/caret",
"version": "0.0.1",
"main": "dist/index.js",
"license": "MIT",
"scripts": {
"build": "tsc"
},
"dependencies": {
"@editorjs/dom": "workspace:*"
}
}
49 changes: 49 additions & 0 deletions packages/caret/src/getCaretNodeAndOffset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Returns TextNode containing a caret and a caret offset in it
* Returns null if there is no caret set
*
* Handles a case when focusNode is an ElementNode and focusOffset is a child index,
* returns child node with focusOffset index as a new focusNode
*/
export function getCaretNodeAndOffset(): [ Node | null, number ] {
const selection = window.getSelection();

if (selection === null) {
return [null, 0];
}

let focusNode = selection.focusNode;
let focusOffset = selection.focusOffset;

if (focusNode === null) {
return [null, 0];
}

/**
* Case when focusNode is an Element (or Document). In this case, focusOffset is a child index.
* We need to return child with focusOffset index as a new focusNode.
*
* <div>|hello</div> <---- Selection references to <div> instead of text node
*
*
*/
if (focusNode.nodeType !== Node.TEXT_NODE && focusNode.childNodes.length > 0) {
/**
* In normal cases, focusOffset is a child index.
*/
if (focusNode.childNodes[focusOffset] !== undefined) {
focusNode = focusNode.childNodes[focusOffset];
focusOffset = 0;
/**
* But in Firefox, focusOffset can be 1 with the single child.
*/
} else {
focusNode = focusNode.childNodes[focusOffset - 1];
if (focusNode.textContent !== null) {
focusOffset = focusNode.textContent.length;
}
}
}

return [focusNode, focusOffset];
}
7 changes: 7 additions & 0 deletions packages/caret/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getCaretNodeAndOffset } from './getCaretNodeAndOffset';
import { isCaretAtEndOfInput } from './isCaretAtEndOfInput';
import { isCaretAtStartOfInput } from './isCaretAtStartOfInput';

export { getCaretNodeAndOffset,
isCaretAtEndOfInput,
isCaretAtStartOfInput };
Loading
Loading