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

Replace flow + babel with tsc #15

Merged
merged 7 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dist
prettier.config.js
*.config.js
7 changes: 3 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": [
"plugin:flowtype/recommended",
"plugin:github/browser",
"plugin:github/es6",
"plugin:github/browser"
],
"parser": "babel-eslint"
"plugin:github/typescript"
]
}
9 changes: 0 additions & 9 deletions .flowconfig

This file was deleted.

5 changes: 0 additions & 5 deletions index.d.ts

This file was deleted.

1,792 changes: 570 additions & 1,222 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"repository": "github/paste-markdown",
"main": "dist/index.umd.js",
"module": "dist/index.esm.js",
"types": "index.d.ts",
"types": "dist/index.d.ts",
"scripts": {
"clean": "rm -rf dist",
"lint": "github-lint",
"prebuild": "npm run clean && npm run lint && mkdir dist",
"build": "rollup -c && cp src/index.js.flow dist/index.umd.js.flow && cp src/index.js.flow dist/index.esm.js.flow",
"build": "rollup -c",
"pretest": "npm run build",
"test": "karma start karma.config.js",
"prepublishOnly": "npm run build",
Expand All @@ -22,24 +22,23 @@
],
"license": "MIT",
"files": [
"dist",
"index.d.ts"
"dist/index.d.ts",
"dist/index.esm.js",
"dist/index.umd.js"
Comment on lines +25 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not all of dist/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dist contains *.d.ts for all the *.ts files even though we only outputted one.

],
"devDependencies": {
"@babel/core": "^7.7.0",
"babel-eslint": "^10.0.3",
"babel-preset-github": "^3.2.1",
"@rollup/plugin-typescript": "^3.0.0",
"chai": "^4.2.0",
"eslint": "^6.6.0",
"eslint-plugin-github": "^3.2.1",
"flow-bin": "^0.111.1",
"eslint": "^6.8.0",
"eslint-plugin-github": "^3.4.0",
"karma": "^4.4.1",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"mocha": "^7.0.1",
"rollup": "^1.26.3",
"rollup-plugin-babel": "^4.3.3"
"rollup": "^1.31.1",
"rollup-plugin-typescript2": "^0.26.0",
"typescript": "^3.7.5"
}
}
14 changes: 4 additions & 10 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/* @flow strict */

import babel from 'rollup-plugin-babel'

const pkg = require('./package.json')

import typescript from 'rollup-plugin-typescript2'
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am hoping that rollup-plugin-typescript2 will down the line be improved to only produce declaration for the output file. tsc would not know about the final output in that case.

import pkg from './package.json'
export default {
input: 'src/index.js',
input: 'src/index.ts',
output: [
{
file: pkg['module'],
Expand All @@ -18,8 +14,6 @@ export default {
}
],
plugins: [
babel({
presets: ['github']
})
typescript()
]
}
7 changes: 0 additions & 7 deletions src/index.js.flow

This file was deleted.

8 changes: 3 additions & 5 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* @flow strict */

import {install as installLink, uninstall as uninstallLink} from './paste-markdown-image-link'
import {install as installTable, uninstall as uninstallTable} from './paste-markdown-table'
import {install as installText, uninstall as uninstallText} from './paste-markdown-text'

type Subscription = {|
interface Subscription {
unsubscribe: () => void
|}
}

export default function subscribe(el: Element): Subscription {
export default function subscribe(el: HTMLElement): Subscription {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(1/2) This is to ensure event listeners are correctly typed.

installTable(el)
installLink(el)
installText(el)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import {insertText} from './text'

export function install(el: Element) {
export function install(el: HTMLElement) {
el.addEventListener('dragover', onDragover)
el.addEventListener('drop', onDrop)
el.addEventListener('paste', onPaste)
}

export function uninstall(el: Element) {
export function uninstall(el: HTMLElement) {
el.removeEventListener('dragover', onDragover)
el.removeEventListener('drop', onDrop)
el.removeEventListener('paste', onPaste)
Expand Down Expand Up @@ -64,7 +64,7 @@ function hasLink(transfer: DataTransfer): boolean {
return Array.from(transfer.types).indexOf('text/uri-list') >= 0
}

function extractLinks(transfer: DataTransfer): Array<string> {
function extractLinks(transfer: DataTransfer): string[] {
return (transfer.getData('text/uri-list') || '').split('\r\n')
}

Expand Down
16 changes: 9 additions & 7 deletions src/paste-markdown-table.js → src/paste-markdown-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import {insertText} from './text'

export function install(el: Element) {
export function install(el: HTMLElement) {
el.addEventListener('dragover', onDragover)
el.addEventListener('drop', onDrop)
el.addEventListener('paste', onPaste)
}

export function uninstall(el: Element) {
export function uninstall(el: HTMLElement) {
el.removeEventListener('dragover', onDragover)
el.removeEventListener('drop', onDrop)
el.removeEventListener('paste', onPaste)
Expand Down Expand Up @@ -58,21 +58,23 @@ function hasFile(transfer: DataTransfer): boolean {

function columnText(column: Element): string {
const noBreakSpace = '\u00A0'
const text = column.textContent
const text = (column.textContent || '')
.trim()
.replace(/\|/g, '\\|')
.replace(/\n/g, ' ')
return text || noBreakSpace
}

function tableHeaders(row: Element): Array<string> {
function tableHeaders(row: Element): string[] {
return Array.from(row.querySelectorAll('td, th')).map(columnText)
}

function tableMarkdown(node: Element): string {
const rows = Array.from(node.querySelectorAll('tr'))

const headers = tableHeaders(rows.shift())
const firstRow = rows.shift()
if (!firstRow) return ''
const headers = tableHeaders(firstRow)
const spacers = headers.map(() => '--')
const header = `${headers.join(' | ')}\n${spacers.join(' | ')}\n`

Expand All @@ -87,13 +89,13 @@ function tableMarkdown(node: Element): string {
return `\n${header}${body}\n\n`
}

function parseTable(html: string): ?Element {
function parseTable(html: string): HTMLElement | null {
const el = document.createElement('div')
el.innerHTML = html
return el.querySelector('table')
}

function hasTable(transfer: DataTransfer): ?Element {
function hasTable(transfer: DataTransfer): HTMLElement | null | void {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can avoid the void return type if you just return null instead of the bare returns in the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is edited in the followup PR.

if (Array.from(transfer.types).indexOf('text/html') === -1) return

const html = transfer.getData('text/html')
Expand Down
6 changes: 2 additions & 4 deletions src/paste-markdown-text.js → src/paste-markdown-text.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* @flow strict */

import {insertText} from './text'

export function install(el: Element) {
export function install(el: HTMLElement) {
el.addEventListener('paste', onPaste)
}

export function uninstall(el: Element) {
export function uninstall(el: HTMLElement) {
el.removeEventListener('paste', onPaste)
}

Expand Down
6 changes: 2 additions & 4 deletions src/text.js → src/text.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* @flow strict */

export function insertText(textarea: HTMLInputElement | HTMLTextAreaElement, text: string): void {
const beginning = textarea.value.substring(0, textarea.selectionStart)
const remaining = textarea.value.substring(textarea.selectionEnd)
const beginning = textarea.value.substring(0, textarea.selectionStart || 0)
const remaining = textarea.value.substring(textarea.selectionEnd || 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(2/2) selectionStart and selectionEned only applies to input elements of certain types. https://html.spec.whatwg.org/#do-not-apply


const newline = beginning.length === 0 || beginning.match(/\n$/) ? '' : '\n'
const textBeforeCursor = beginning + newline + text
Expand Down
3 changes: 0 additions & 3 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"rules": {
"flowtype/require-valid-file-annotation": "off"
},
"env": {
"mocha": true
},
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "esnext",
"target": "es2017",
"lib": [
"es2018",
"dom"
],
"strict": true,
"declaration": true,
"outDir": "dist",
"removeComments": true,
"preserveConstEnums": true
}
}