Skip to content

Commit

Permalink
refactor: adjust to typescript@4.7 (#968)
Browse files Browse the repository at this point in the history
* refactor: assign ClipboardStubControl

* refactor: remove unnecessary type assertions
  • Loading branch information
ph-fritsche authored Jun 9, 2022
1 parent 8215e2e commit 9dd3985
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/document/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function setUISelection(
const anchorOffset =
mode === 'replace' || element[UISelection] === undefined
? sanitizeOffset(anchorOffsetParam)
: (element[UISelection] as UISelection).anchorOffset
: element[UISelection].anchorOffset
const focusOffset = sanitizeOffset(focusOffsetParam)

const startOffset = Math.min(anchorOffset, focusOffset)
Expand Down
76 changes: 41 additions & 35 deletions src/utils/dataTransfer/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,43 +54,50 @@ export function createClipboardItem(
}

const ClipboardStubControl = Symbol('Manage ClipboardSub')
type ClipboardStubControlInstance = {
resetClipboardStub: () => void
detachClipboardStub: () => void
}

function createClipboardStub(window: Window & typeof globalThis) {
return new (class Clipboard extends window.EventTarget {
private items: ClipboardItem[] = []
function createClipboardStub(
window: Window & typeof globalThis,
control: ClipboardStubControlInstance,
) {
return Object.assign(
new (class Clipboard extends window.EventTarget {
private items: ClipboardItem[] = []

async read() {
return Array.from(this.items)
}
async read() {
return Array.from(this.items)
}

async readText() {
let text = ''
for (const item of this.items) {
const type = item.types.includes('text/plain')
? 'text/plain'
: item.types.find(t => t.startsWith('text/'))
if (type) {
text += await item
.getType(type)
.then(b => readBlobText(b, window.FileReader))
async readText() {
let text = ''
for (const item of this.items) {
const type = item.types.includes('text/plain')
? 'text/plain'
: item.types.find(t => t.startsWith('text/'))
if (type) {
text += await item
.getType(type)
.then(b => readBlobText(b, window.FileReader))
}
}
return text
}
return text
}

async write(data: ClipboardItem[]) {
this.items = data
}

async writeText(text: string) {
this.items = [createClipboardItem(window, text)]
}
async write(data: ClipboardItem[]) {
this.items = data
}

[ClipboardStubControl]: {
resetClipboardStub: () => void
detachClipboardStub: () => void
}
})()
async writeText(text: string) {
this.items = [createClipboardItem(window, text)]
}
})(),
{
[ClipboardStubControl]: control,
},
)
}
type ClipboardStub = ReturnType<typeof createClipboardStub>

Expand All @@ -112,11 +119,10 @@ export function attachClipboardStubToView(window: Window & typeof globalThis) {
'clipboard',
)

let stub = createClipboardStub(window)
const control = {
let stub: ClipboardStub
const control: ClipboardStubControlInstance = {
resetClipboardStub: () => {
stub = createClipboardStub(window)
stub[ClipboardStubControl] = control
stub = createClipboardStub(window, control)
},
detachClipboardStub: () => {
/* istanbul ignore if */
Expand All @@ -130,7 +136,7 @@ export function attachClipboardStubToView(window: Window & typeof globalThis) {
}
},
}
stub[ClipboardStubControl] = control
stub = createClipboardStub(window, control)

Object.defineProperty(window.navigator, 'clipboard', {
get: () => stub,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/misc/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare module '../../setup' {

export function setLevelRef(config: Config, level: ApiLevel) {
config[Level] ??= {}
;(config[Level] as LevelRefs)[level] = {}
config[Level][level] = {}
}

export function getLevelRef(config: Config, level: ApiLevel) {
Expand Down

0 comments on commit 9dd3985

Please sign in to comment.