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

Fix whole editor paste with locked code #125

Merged
merged 31 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f2e327c
fix: select first editable range when pasting for locked code with de…
KaiSaba Dec 5, 2024
f538ee0
fix: recompute editor operations for locking code using decorations
KaiSaba Dec 6, 2024
04bfcfb
cleanup: remove override of editor paste
KaiSaba Dec 6, 2024
c40f3c1
fix: increment uneditable range index correctly when computing new op…
KaiSaba Dec 6, 2024
7ee4792
refactor: use position in minUsRange instead of lines and columns
KaiSaba Dec 6, 2024
b598071
refactor: split compute of new operations in multiple functions
KaiSaba Dec 6, 2024
8faaeb6
refactor: move range and operations functions in separate files
KaiSaba Dec 9, 2024
c291e16
refactor: split editable ranges and operation text separately
KaiSaba Dec 9, 2024
8412173
fix: allow paste when selected locked code doesn't change
KaiSaba Dec 9, 2024
e47df09
fix: use string indexOf instead of split to split text at the first o…
KaiSaba Dec 9, 2024
f8cea32
refactor: refactor minusRanges function to return all ranges
KaiSaba Dec 11, 2024
f8c1ed4
fix: use minusRanges to get hidden areas and visible ranges in hideCo…
KaiSaba Dec 11, 2024
0c5ae90
fix: fix split operation text function when uneditable range text not…
KaiSaba Dec 11, 2024
cf76aee
fix: fix locked code ranges and text split
KaiSaba Dec 11, 2024
8c76d0f
fix: fix ranges computation
KaiSaba Dec 12, 2024
8ba06e8
fix: fix operation split and operation text split
KaiSaba Dec 12, 2024
acd5928
test: add jest config and tests on locked code
KaiSaba Dec 12, 2024
8ec2433
test: simplify createTestRange function
KaiSaba Dec 12, 2024
0759eb0
refactor: rename computeNewOperationsForLockedCode to tryIgnoreLocked…
KaiSaba Dec 12, 2024
bbf980c
fix: throw custom error when operation is invalid with locked code
KaiSaba Dec 12, 2024
4211c36
refactor: rename functions on locked code and their variables
KaiSaba Dec 12, 2024
59ba25d
cleanup: remove unecessary configs in tsconfig
KaiSaba Dec 12, 2024
896f2d2
refactor: rename minusRange function and return values
KaiSaba Dec 12, 2024
7c5a675
cleanup: remove unneeded console.info
KaiSaba Dec 12, 2024
b749674
fix: removed line bug when pasting code
KaiSaba Dec 12, 2024
b450e74
fix: remove pollution logs in tests
Dec 12, 2024
6308574
refactor: change code so it's easier to test
Dec 12, 2024
cb5a8a4
refactor: extract error handling to facilitate tests
Dec 12, 2024
36c4ef1
lib: install @jest/globals
Dec 12, 2024
28be49e
test: update test by mocking code lock and edit operations
KaiSaba Dec 12, 2024
5cfb9f1
cleanup: remove unecessary constructor for LockedCodeError
KaiSaba Dec 12, 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
13 changes: 13 additions & 0 deletions FixJSDOMEnvironment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TestEnvironment } from 'jest-environment-jsdom'

class FixJSDOMEnvironment extends TestEnvironment {
constructor(...args) {
super(...args);

// FIXME https://github.com/jsdom/jsdom/issues/3363
this.global.structuredClone = structuredClone;
}
}

// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
export default FixJSDOMEnvironment
12 changes: 12 additions & 0 deletions babel.test.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
plugins: [
['@babel/plugin-transform-modules-commonjs', {
importInterop: 'babel'
}],
'babel-plugin-transform-import-meta'
],
presets: [
'@babel/preset-env',
'@babel/preset-typescript'
]
}
163 changes: 163 additions & 0 deletions browserMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-useless-constructor */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { fetch as fetchPolyfill } from 'whatwg-fetch'
import fs from 'fs/promises'
import { performance } from 'perf_hooks'

Object.defineProperty(document, 'queryCommandSupported', {
value: jest.fn().mockImplementation(() => true)
})

window.process = undefined

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
}))
})

Object.defineProperty(window, 'fetch', {
value: jest.fn(async (url, options) => {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (url.startsWith('file:')) {
const content = await fs.readFile(new URL(url).pathname)
return {
json: async () => JSON.stringify(JSON.parse(content.toString())),
arrayBuffer: async () => content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength),
status: 200
}
} else {
return fetchPolyfill(url, options)
}
})
})

Object.defineProperty(URL, 'createObjectURL', {
value: jest.fn((blob) => {
return 'blob:not-working'
})
})

Object.defineProperty(window, 'Worker', {
value: class Worker {
constructor (stringUrl) {}
postMessage (msg) {}
terminate () {}
removeEventListener () {}
}
})

Object.defineProperty(window, 'ResizeObserver', {
value: class ResizeObserver {
constructor (stringUrl) {}
observe () {}
}
})

// These 2 classes come from https://gist.github.com/Yaffle/5458286
Object.defineProperty(window, 'TextEncoder', {
value: class TextEncoder {
encode (string) {
const octets = []
const length = string.length
let i = 0
while (i < length) {
const codePoint = string.codePointAt(i)
let c = 0
let bits = 0
if (codePoint <= 0x0000007F) {
c = 0
bits = 0x00
} else if (codePoint <= 0x000007FF) {
c = 6
bits = 0xC0
} else if (codePoint <= 0x0000FFFF) {
c = 12
bits = 0xE0
} else if (codePoint <= 0x001FFFFF) {
c = 18
bits = 0xF0
}
octets.push(bits | (codePoint >> c))
c -= 6
while (c >= 0) {
octets.push(0x80 | ((codePoint >> c) & 0x3F))
c -= 6
}
i += codePoint >= 0x10000 ? 2 : 1
}
return Uint8Array.from(octets)
}
}
})
Object.defineProperty(window, 'TextDecoder', {
value: class TextDecoder {
decode (octets) {
if (octets == null) {
return ''
}
let string = ''
let i = 0
while (i < octets.length) {
let octet = octets[i]
let bytesNeeded = 0
let codePoint = 0
if (octet <= 0x7F) {
bytesNeeded = 0
codePoint = octet & 0xFF
} else if (octet <= 0xDF) {
bytesNeeded = 1
codePoint = octet & 0x1F
} else if (octet <= 0xEF) {
bytesNeeded = 2
codePoint = octet & 0x0F
} else if (octet <= 0xF4) {
bytesNeeded = 3
codePoint = octet & 0x07
}
if (octets.length - i - bytesNeeded > 0) {
let k = 0
while (k < bytesNeeded) {
octet = octets[i + k + 1]
codePoint = (codePoint << 6) | (octet & 0x3F)
k += 1
}
} else {
codePoint = 0xFFFD
bytesNeeded = octets.length - i
}
string += String.fromCodePoint(codePoint)
i += bytesNeeded + 1
}
return string
}
}
})

Object.defineProperty(window, 'Buffer', {
value: undefined
})

// Force override performance, for some reason the implementation is empty otherwise
const _performance = performance
Object.defineProperty(global, 'performance', {
get () { return _performance },
set (v) {
// ignore
}
})

global.CSS = {
escape: v => v
}

Element.prototype.scrollIntoView = jest.fn()
14 changes: 14 additions & 0 deletions jest/cssTransform.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

// This is a custom Jest transformer turning style imports into empty objects.
// http://facebook.github.io/jest/docs/en/webpack.html

module.exports = {
process () {
return { code: 'module.exports = {};' }
},
getCacheKey () {
// The output is always the same.
return 'cssTransform'
}
}
7 changes: 7 additions & 0 deletions jest/fileTransform.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
process(src, filename) {
const assetFilename = JSON.stringify(filename);

return { code: `module.exports = ${assetFilename};` };
},
};
9 changes: 9 additions & 0 deletions jest/resolver.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const browserResolve = require('browser-resolve')

module.exports = (path, options) => {
try {
return browserResolve.sync(path, options)
} catch (error) {
return options.defaultResolver(path, options)
}
}
Loading
Loading