editor.spec.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. import { expect } from '@playwright/test'
  2. import { test } from './fixtures'
  3. import {
  4. createRandomPage,
  5. enterNextBlock,
  6. modKey,
  7. repeatKeyPress,
  8. moveCursor,
  9. selectCharacters,
  10. getSelection,
  11. getCursorPos,
  12. } from './utils'
  13. import { dispatch_kb_events } from './util/keyboard-events'
  14. import * as kb_events from './util/keyboard-events'
  15. test('hashtag and quare brackets in same line #4178', async ({ page }) => {
  16. try {
  17. await page.waitForSelector('.notification-clear', { timeout: 10 })
  18. page.click('.notification-clear')
  19. } catch (error) {
  20. }
  21. await createRandomPage(page)
  22. await page.type('textarea >> nth=0', '#foo bar')
  23. await enterNextBlock(page)
  24. await page.type('textarea >> nth=0', 'bar [[blah]]', { delay: 100 })
  25. for (let i = 0; i < 12; i++) {
  26. await page.press('textarea >> nth=0', 'ArrowLeft')
  27. }
  28. await page.type('textarea >> nth=0', ' ')
  29. await page.press('textarea >> nth=0', 'ArrowLeft')
  30. await page.type('textarea >> nth=0', '#')
  31. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  32. await page.type('textarea >> nth=0', 'fo')
  33. await page.click('.absolute >> text=' + 'foo')
  34. expect(await page.inputValue('textarea >> nth=0')).toBe(
  35. '#foo bar [[blah]]'
  36. )
  37. })
  38. test('hashtag search page auto-complete', async ({ page, block }) => {
  39. await createRandomPage(page)
  40. await block.activeEditing(0)
  41. await page.type('textarea >> nth=0', '#', { delay: 100 })
  42. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  43. await page.keyboard.press('Escape', { delay: 50 })
  44. await block.mustFill("done")
  45. await enterNextBlock(page)
  46. await page.type('textarea >> nth=0', 'Some #', { delay: 100 })
  47. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  48. await page.keyboard.press('Escape', { delay: 50 })
  49. await block.mustFill("done")
  50. })
  51. test('hashtag search #[[ page auto-complete', async ({ page, block }) => {
  52. await createRandomPage(page)
  53. await block.activeEditing(0)
  54. await page.type('textarea >> nth=0', '#[[', { delay: 100 })
  55. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  56. await page.keyboard.press('Escape', { delay: 50 })
  57. })
  58. test('disappeared children #4814', async ({ page, block }) => {
  59. await createRandomPage(page)
  60. await block.mustType('parent')
  61. await block.enterNext()
  62. expect(await block.indent()).toBe(true)
  63. for (let i = 0; i < 5; i++) {
  64. await block.mustType(i.toString())
  65. await block.enterNext()
  66. }
  67. // collapse
  68. await page.click('.block-control >> nth=0')
  69. // expand
  70. await page.click('.block-control >> nth=0')
  71. await block.waitForBlocks(7) // 1 + 5 + 1 empty
  72. // Ensures there's no active editor
  73. await expect(page.locator('.editor-inner')).toHaveCount(0, { timeout: 500 })
  74. })
  75. test('create new page from bracketing text #4971', async ({ page, block }) => {
  76. let title = 'Page not Exists yet'
  77. await createRandomPage(page)
  78. await block.mustType(`[[${title}]]`)
  79. await page.keyboard.press(modKey + '+o')
  80. // Check page title equals to `title`
  81. await page.waitForTimeout(100)
  82. expect(await page.locator('h1.title').innerText()).toContain(title)
  83. // Check there're linked references
  84. await page.waitForSelector(`.references .ls-block >> nth=1`, { state: 'detached', timeout: 100 })
  85. })
  86. test.skip('backspace and cursor position #4897', async ({ page, block }) => {
  87. await createRandomPage(page)
  88. // Delete to previous block, and check cursor position, with markup
  89. await block.mustFill('`012345`')
  90. await block.enterNext()
  91. await block.mustType('`abcdef', { toBe: '`abcdef`' }) // "`" auto-completes
  92. expect(await block.selectionStart()).toBe(7)
  93. expect(await block.selectionEnd()).toBe(7)
  94. for (let i = 0; i < 7; i++) {
  95. await page.keyboard.press('ArrowLeft')
  96. }
  97. expect(await block.selectionStart()).toBe(0)
  98. await page.keyboard.press('Backspace')
  99. await block.waitForBlocks(1) // wait for delete and re-render
  100. expect(await block.selectionStart()).toBe(8)
  101. })
  102. test.skip('next block and cursor position', async ({ page, block }) => {
  103. await createRandomPage(page)
  104. // Press Enter and check cursor position, with markup
  105. await block.mustType('abcde`12345', { toBe: 'abcde`12345`' }) // "`" auto-completes
  106. for (let i = 0; i < 7; i++) {
  107. await page.keyboard.press('ArrowLeft')
  108. }
  109. expect(await block.selectionStart()).toBe(5) // after letter 'e'
  110. await block.enterNext()
  111. expect(await block.selectionStart()).toBe(0) // should at the beginning of the next block
  112. const locator = page.locator('textarea >> nth=0')
  113. await expect(locator).toHaveText('`12345`', { timeout: 1000 })
  114. })
  115. test(
  116. "Press CJK Left Black Lenticular Bracket `【` by 2 times #3251 should trigger [[]], " +
  117. "but dont trigger RIME #3440 ",
  118. // cases should trigger [[]] #3251
  119. async ({ page, block }) => {
  120. // This test requires dev mode
  121. test.skip(process.env.RELEASE === 'true', 'not available for release version')
  122. // @ts-ignore
  123. for (let [idx, events] of [
  124. kb_events.win10_pinyin_left_full_square_bracket,
  125. kb_events.macos_pinyin_left_full_square_bracket
  126. // TODO: support #3741
  127. // kb_events.win10_legacy_pinyin_left_full_square_bracket,
  128. ].entries()) {
  129. await createRandomPage(page)
  130. let check_text = "#3251 test " + idx
  131. await block.mustFill(check_text + "【")
  132. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  133. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text + '【')
  134. await block.mustFill(check_text + "【【")
  135. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  136. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text + '[[]]')
  137. };
  138. // @ts-ignore dont trigger RIME #3440
  139. for (let [idx, events] of [
  140. kb_events.macos_pinyin_selecting_candidate_double_left_square_bracket,
  141. kb_events.win10_RIME_selecting_candidate_double_left_square_bracket
  142. ].entries()) {
  143. await createRandomPage(page)
  144. let check_text = "#3440 test " + idx
  145. await block.mustFill(check_text)
  146. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  147. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text)
  148. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  149. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text)
  150. }
  151. })
  152. test('copy & paste block ref and replace its content', async ({ page, block }) => {
  153. await createRandomPage(page)
  154. await block.mustType('Some random text')
  155. await page.keyboard.press(modKey + '+c')
  156. await page.waitForTimeout(200)
  157. await page.press('textarea >> nth=0', 'Enter')
  158. await page.waitForTimeout(100)
  159. await block.waitForBlocks(2)
  160. await page.waitForTimeout(100)
  161. await page.keyboard.press(modKey + '+v', { delay: 100 })
  162. await page.waitForTimeout(100)
  163. await page.keyboard.press('Enter', { delay: 100 })
  164. // Check if the newly created block-ref has the same referenced content
  165. await expect(page.locator('.block-ref >> text="Some random text"')).toHaveCount(1);
  166. // Move cursor into the block ref
  167. for (let i = 0; i < 4; i++) {
  168. await page.press('textarea >> nth=0', 'ArrowLeft', { delay: 10 })
  169. }
  170. await expect(page.locator('textarea >> nth=0')).not.toHaveValue('Some random text')
  171. for (let i = 0; i < 4; i++) {
  172. await page.press('textarea >> nth=0', 'ArrowLeft', { delay: 10 } )
  173. }
  174. // Trigger replace-block-reference-with-content-at-point
  175. await page.keyboard.press(modKey + '+Shift+r')
  176. await expect(page.locator('textarea >> nth=0')).toHaveValue('Some random text')
  177. await block.escapeEditing()
  178. await expect(page.locator('.block-ref >> text="Some random text"')).toHaveCount(0);
  179. await expect(page.locator('text="Some random text"')).toHaveCount(2);
  180. })
  181. test('copy and paste block after editing new block #5962', async ({ page, block }) => {
  182. await createRandomPage(page)
  183. // Create a block and copy it in block-select mode
  184. await block.mustType('Block being copied')
  185. await page.keyboard.press('Escape')
  186. await expect(page.locator('.ls-block.selected')).toHaveCount(1)
  187. await page.keyboard.press(modKey + '+c', { delay: 100 })
  188. await page.keyboard.press('Enter')
  189. await expect(page.locator('.ls-block.selected')).toHaveCount(0)
  190. await expect(page.locator('textarea >> nth=0')).toBeVisible()
  191. await page.keyboard.press('Enter')
  192. await block.waitForBlocks(2)
  193. await page.waitForTimeout(100)
  194. await block.mustType('Typed block')
  195. await page.keyboard.press(modKey + '+v')
  196. await expect(page.locator('text="Typed block"')).toHaveCount(1)
  197. await block.waitForBlocks(3)
  198. })
  199. test('undo and redo after starting an action should not destroy text #6267', async ({ page, block }) => {
  200. await createRandomPage(page)
  201. // Get one piece of undo state onto the stack
  202. await block.mustType('text1 ')
  203. await page.waitForTimeout(1000) // auto save
  204. // Then type more, start an action prompt, and undo
  205. await page.keyboard.type('text2 [[', { delay: 50 })
  206. await expect(page.locator(`[data-modal-name="page-search"]`)).toBeVisible()
  207. await page.waitForTimeout(1000) // auto save
  208. await page.keyboard.press(modKey + '+z', { delay: 100 })
  209. // Should close the action menu when we undo the action prompt
  210. // await expect(page.locator(`[data-modal-name="page-search"]`)).not.toBeVisible()
  211. // It should undo to the last saved state, and not erase the previous undo action too
  212. await expect(page.locator('text="text1"')).toHaveCount(1)
  213. // And it should keep what was undone as a redo action
  214. await page.keyboard.press(modKey + '+Shift+z')
  215. await expect(page.locator('text="text1 text2 [[]]"')).toHaveCount(1)
  216. })
  217. // test('undo after starting an action should close the action menu #6269', async ({ page, block }) => {
  218. // for (const [commandTrigger, modalName] of [['/', 'commands'], ['[[', 'page-search']]) {
  219. // await createRandomPage(page)
  220. // // Open the action modal
  221. // await block.mustType('text1 ')
  222. // await page.waitForTimeout(550)
  223. // await page.keyboard.type(commandTrigger, { delay: 20 })
  224. // await page.waitForTimeout(100) // Tolerable delay for the action menu to open
  225. // await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  226. // // Undo, removing "/today", and closing the action modal
  227. // await page.keyboard.press(modKey + '+z', { delay: 100 })
  228. // await expect(page.locator('text="/today"')).toHaveCount(0)
  229. // await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  230. // }
  231. // })
  232. // test('#6266 moving cursor outside of brackets should close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  233. // for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  234. // // First, left arrow
  235. // await createRandomPage(page)
  236. // await block.mustFill('t ')
  237. // await page.keyboard.type(commandTrigger, { delay: 20 })
  238. // await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  239. // await autocompleteMenu.expectVisible(modalName)
  240. // await page.keyboard.press('ArrowLeft')
  241. // await page.waitForTimeout(100)
  242. // await autocompleteMenu.expectHidden(modalName)
  243. // // Then, right arrow
  244. // await createRandomPage(page)
  245. // await block.mustFill('t ')
  246. // await page.keyboard.type(commandTrigger, { delay: 20 })
  247. // await autocompleteMenu.expectVisible(modalName)
  248. // await page.waitForTimeout(100)
  249. // // Move cursor outside of the space strictly between the double brackets
  250. // await page.keyboard.press('ArrowRight')
  251. // await page.waitForTimeout(100)
  252. // await autocompleteMenu.expectHidden(modalName)
  253. // }
  254. // })
  255. // // Old logic would fail this because it didn't do the check if @search-timeout was set
  256. // test('#6266 moving cursor outside of parens immediately after searching should still close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  257. // for (const [commandTrigger, modalName] of [['((', 'block-search']]) {
  258. // await createRandomPage(page)
  259. // // Open the autocomplete menu
  260. // await block.mustFill('t ')
  261. // await page.keyboard.type(commandTrigger, { delay: 20 })
  262. // await page.waitForTimeout(100)
  263. // await page.keyboard.type("some block search text")
  264. // await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  265. // await autocompleteMenu.expectVisible(modalName)
  266. // // Move cursor outside of the space strictly between the double parens
  267. // await page.keyboard.press('ArrowRight')
  268. // await page.waitForTimeout(100)
  269. // await autocompleteMenu.expectHidden(modalName)
  270. // }
  271. // })
  272. // test('pressing up and down should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  273. // for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  274. // await createRandomPage(page)
  275. // // Open the autocomplete menu
  276. // await block.mustFill('t ')
  277. // await page.keyboard.type(commandTrigger, { delay: 20 })
  278. // await autocompleteMenu.expectVisible(modalName)
  279. // const cursorPos = await block.selectionStart()
  280. // await page.keyboard.press('ArrowUp')
  281. // await page.waitForTimeout(100)
  282. // await autocompleteMenu.expectVisible(modalName)
  283. // await expect(await block.selectionStart()).toEqual(cursorPos)
  284. // await page.keyboard.press('ArrowDown')
  285. // await page.waitForTimeout(100)
  286. // await autocompleteMenu.expectVisible(modalName)
  287. // await expect(await block.selectionStart()).toEqual(cursorPos)
  288. // }
  289. // })
  290. // test('moving cursor inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  291. // for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  292. // await createRandomPage(page)
  293. // // Open the autocomplete menu
  294. // await block.mustType('test ')
  295. // await page.keyboard.type(commandTrigger, { delay: 20 })
  296. // await page.waitForTimeout(100)
  297. // if (commandTrigger === '[[') {
  298. // await autocompleteMenu.expectVisible(modalName)
  299. // }
  300. // await page.keyboard.type("search", { delay: 20 })
  301. // await autocompleteMenu.expectVisible(modalName)
  302. // // Move cursor, still inside the brackets
  303. // await page.keyboard.press('ArrowLeft')
  304. // await page.waitForTimeout(100)
  305. // await autocompleteMenu.expectVisible(modalName)
  306. // }
  307. // })
  308. // test('moving cursor inside of brackets when autocomplete menu is closed should NOT open autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  309. // // Note: (( behaves differently and doesn't auto-trigger when typing in it after exiting the search prompt once
  310. // for (const [commandTrigger, modalName] of [['[[', 'page-search']]) {
  311. // await createRandomPage(page)
  312. // // Open the autocomplete menu
  313. // await block.mustFill('')
  314. // await page.keyboard.type(commandTrigger, { delay: 20 })
  315. // await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  316. // await autocompleteMenu.expectVisible(modalName)
  317. // await block.escapeEditing()
  318. // await autocompleteMenu.expectHidden(modalName)
  319. // // Move cursor left until it's inside the brackets; shouldn't open autocomplete menu
  320. // await page.locator('.block-content').click()
  321. // await page.waitForTimeout(100)
  322. // await autocompleteMenu.expectHidden(modalName)
  323. // await page.keyboard.press('ArrowLeft', { delay: 50 })
  324. // await autocompleteMenu.expectHidden(modalName)
  325. // await page.keyboard.press('ArrowLeft', { delay: 50 })
  326. // await autocompleteMenu.expectHidden(modalName)
  327. // // Type a letter, this should open the autocomplete menu
  328. // await page.keyboard.type('z', { delay: 20 })
  329. // await page.waitForTimeout(100)
  330. // await autocompleteMenu.expectVisible(modalName)
  331. // }
  332. // })
  333. // test('selecting text inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  334. // for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  335. // await createRandomPage(page)
  336. // // Open the autocomplete menu
  337. // await block.mustFill('')
  338. // await page.keyboard.type(commandTrigger, { delay: 20 })
  339. // await page.waitForTimeout(100)
  340. // await autocompleteMenu.expectVisible(modalName)
  341. // await page.keyboard.type("some page search text", { delay: 10 })
  342. // await page.waitForTimeout(100)
  343. // await autocompleteMenu.expectVisible(modalName)
  344. // // Select some text within the brackets
  345. // await page.keyboard.press('Shift+ArrowLeft')
  346. // await page.waitForTimeout(100)
  347. // await autocompleteMenu.expectVisible(modalName)
  348. // }
  349. // })
  350. // test('pressing backspace and remaining inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  351. // for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  352. // await createRandomPage(page)
  353. // // Open the autocomplete menu
  354. // await block.mustFill('test ')
  355. // await page.keyboard.type(commandTrigger, { delay: 20 })
  356. // await page.waitForTimeout(100)
  357. // await autocompleteMenu.expectVisible(modalName)
  358. // await page.keyboard.type("some page search text", { delay: 10 })
  359. // await page.waitForTimeout(100)
  360. // await autocompleteMenu.expectVisible(modalName)
  361. // // Delete one character inside the brackets
  362. // await page.keyboard.press('Backspace')
  363. // await page.waitForTimeout(100)
  364. // await autocompleteMenu.expectVisible(modalName)
  365. // }
  366. // })
  367. // test('press escape when autocomplete menu is open, should close autocomplete menu only #6270', async ({ page, block }) => {
  368. // for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['/', 'commands']]) {
  369. // await createRandomPage(page)
  370. // // Open the action modal
  371. // await block.mustFill('text ')
  372. // await page.waitForTimeout(550)
  373. // await page.keyboard.type(commandTrigger, { delay: 20 })
  374. // await page.waitForTimeout(100)
  375. // await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  376. // await page.waitForTimeout(100)
  377. // // Press escape; should close action modal instead of exiting edit mode
  378. // await page.keyboard.press('Escape')
  379. // await page.waitForTimeout(100)
  380. // await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  381. // await page.waitForTimeout(1000)
  382. // expect(await block.isEditing()).toBe(true)
  383. // }
  384. // })
  385. // test('press escape when link/image dialog is open, should restore focus to input', async ({ page, block }) => {
  386. // for (const [commandTrigger, modalName] of [['/link', 'commands']]) {
  387. // await createRandomPage(page)
  388. // // Open the action modal
  389. // await block.mustFill('')
  390. // await page.waitForTimeout(550)
  391. // await page.keyboard.type(commandTrigger, { delay: 20 })
  392. // await page.waitForTimeout(100)
  393. // await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  394. // await page.waitForTimeout(100)
  395. // // Press enter to open the link dialog
  396. // await page.keyboard.press('Enter')
  397. // await expect(page.locator(`[data-modal-name="input"]`)).toBeVisible()
  398. // // Press escape; should close link dialog and restore focus to the block textarea
  399. // await page.keyboard.press('Escape')
  400. // await page.waitForTimeout(100)
  401. // await expect(page.locator(`[data-modal-name="input"]`)).not.toBeVisible()
  402. // await page.waitForTimeout(1000)
  403. // expect(await block.isEditing()).toBe(true)
  404. // }
  405. // })
  406. // test('should show text after soft return when node is collapsed #5074', async ({ page, block }) => {
  407. // const delay = 300
  408. // await createRandomPage(page)
  409. // await page.type('textarea >> nth=0', 'Before soft return', { delay: 10 })
  410. // await page.keyboard.press('Shift+Enter', { delay: 10 })
  411. // await page.type('textarea >> nth=0', 'After soft return', { delay: 10 })
  412. // await block.enterNext()
  413. // expect(await block.indent()).toBe(true)
  414. // await block.mustType('Child text')
  415. // // collapse
  416. // await page.click('.block-control >> nth=0')
  417. // await block.waitForBlocks(1)
  418. // // select the block that has the soft return
  419. // await page.keyboard.press('ArrowDown')
  420. // await page.waitForTimeout(delay)
  421. // await page.keyboard.press('Enter')
  422. // await page.waitForTimeout(delay)
  423. // await expect(page.locator('textarea >> nth=0')).toHaveText('Before soft return\nAfter soft return')
  424. // // zoom into the block
  425. // page.click('a.block-control + a')
  426. // await page.waitForNavigation()
  427. // await page.waitForTimeout(delay * 3)
  428. // // select the block that has the soft return
  429. // await page.keyboard.press('ArrowDown')
  430. // await page.waitForTimeout(delay)
  431. // await page.keyboard.press('Enter')
  432. // await page.waitForTimeout(delay)
  433. // await expect(page.locator('textarea >> nth=0')).toHaveText('Before soft return\nAfter soft return')
  434. // })
  435. test('should not erase typed text when expanding block quickly after typing #3891', async ({ page, block }) => {
  436. await createRandomPage(page)
  437. await block.mustFill('initial text,')
  438. await page.waitForTimeout(1000)
  439. await page.type('textarea >> nth=0', ' then expand', { delay: 10 })
  440. // A quick cmd-down must not destroy the typed text
  441. await page.keyboard.press(modKey + '+ArrowDown', { delay: 100 })
  442. expect(await page.inputValue('textarea >> nth=0')).toBe(
  443. 'initial text, then expand'
  444. )
  445. // First undo should delete the last typed information, not undo a no-op expand action
  446. await page.keyboard.press(modKey + '+z', { delay: 100 })
  447. expect(await page.inputValue('textarea >> nth=0')).toBe(
  448. 'initial text,'
  449. )
  450. await page.keyboard.press(modKey + '+z')
  451. await page.waitForTimeout(100)
  452. expect(await page.inputValue('textarea >> nth=0')).toBe(
  453. ''
  454. )
  455. })
  456. test('should keep correct undo and redo seq after indenting or outdenting the block #7615',async({page,block}) => {
  457. await createRandomPage(page)
  458. await block.mustFill("foo")
  459. await page.keyboard.press("Enter")
  460. await expect(page.locator('textarea >> nth=0')).toHaveText("")
  461. await block.indent()
  462. await page.waitForTimeout(100)
  463. await block.mustFill("bar")
  464. await expect(page.locator('textarea >> nth=0')).toHaveText("bar")
  465. await page.keyboard.press(modKey + '+z', { delay: 100 })
  466. // should undo "bar" input
  467. await expect(page.locator('textarea >> nth=0')).toHaveText("")
  468. await page.keyboard.press(modKey + '+Shift+z', { delay: 100 })
  469. // should redo "bar" input
  470. await expect(page.locator('textarea >> nth=0')).toHaveText("bar")
  471. await page.keyboard.press("Shift+Tab", { delay: 100 })
  472. await page.keyboard.press("Enter", { delay: 100 })
  473. await expect(page.locator('textarea >> nth=0')).toHaveText("")
  474. // #7615
  475. await page.keyboard.type("aaa")
  476. await block.indent()
  477. await page.waitForTimeout(550)
  478. await page.keyboard.type(" bbb")
  479. await page.waitForTimeout(550)
  480. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa bbb")
  481. await page.keyboard.press(modKey + '+z')
  482. await page.waitForTimeout(100)
  483. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
  484. await page.keyboard.press(modKey + '+Shift+z')
  485. await page.waitForTimeout(100)
  486. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa bbb")
  487. })
  488. test.describe('Text Formatting', () => {
  489. const formats = [
  490. { name: 'bold', prefix: '**', postfix: '**', shortcut: modKey + '+b' },
  491. { name: 'italic', prefix: '*', postfix: '*', shortcut: modKey + '+i' },
  492. {
  493. name: 'strikethrough',
  494. prefix: '~~',
  495. postfix: '~~',
  496. shortcut: modKey + '+Shift+s',
  497. },
  498. // {
  499. // name: 'underline',
  500. // prefix: '<u>',
  501. // postfix: '</u>',
  502. // shortcut: modKey + '+u',
  503. // },
  504. ]
  505. for (const format of formats) {
  506. test.describe(`${format.name} formatting`, () => {
  507. test('Applying to an empty selection inserts placeholder formatting and places cursor correctly', async ({
  508. page,
  509. block,
  510. }) => {
  511. await createRandomPage(page)
  512. const text = 'Lorem ipsum'
  513. await block.mustFill(text)
  514. // move the cursor to the end of Lorem
  515. await repeatKeyPress(page, 'ArrowLeft', text.length - 'ipsum'.length)
  516. await page.keyboard.press('Space')
  517. // Apply formatting
  518. await page.keyboard.press(format.shortcut)
  519. await expect(page.locator('textarea >> nth=0')).toHaveText(
  520. `Lorem ${format.prefix}${format.postfix} ipsum`
  521. )
  522. // Verify cursor position
  523. const cursorPos = await getCursorPos(page)
  524. expect(cursorPos).toBe(' ipsum'.length + format.prefix.length)
  525. })
  526. test('Applying to an entire block encloses the block in formatting and places cursor correctly', async ({
  527. page,
  528. block,
  529. }) => {
  530. await createRandomPage(page)
  531. const text = 'Lorem ipsum-dolor sit.'
  532. await block.mustFill(text)
  533. // Select the entire block
  534. await page.keyboard.press(modKey + '+a')
  535. // Apply formatting
  536. await page.keyboard.press(format.shortcut)
  537. await expect(page.locator('textarea >> nth=0')).toHaveText(
  538. `${format.prefix}${text}${format.postfix}`
  539. )
  540. // Verify cursor position
  541. const cursorPosition = await getCursorPos(page)
  542. expect(cursorPosition).toBe(format.prefix.length + text.length)
  543. })
  544. test('Applying and then removing from a word connected with a special character correctly formats and then reverts', async ({
  545. page,
  546. block,
  547. }) => {
  548. await createRandomPage(page)
  549. await block.mustFill('Lorem ipsum-dolor sit.')
  550. // Select 'ipsum'
  551. // Move the cursor to the desired position
  552. await moveCursor(page, -16)
  553. // Select the desired length of text
  554. await selectCharacters(page, 5)
  555. // Apply formatting
  556. await page.keyboard.press(format.shortcut)
  557. // Verify that 'ipsum' is formatted
  558. await expect(page.locator('textarea >> nth=0')).toHaveText(
  559. `Lorem ${format.prefix}ipsum${format.postfix}-dolor sit.`
  560. )
  561. // Re-select 'ipsum'
  562. // Move the cursor to the desired position
  563. await moveCursor(page, -5)
  564. // Select the desired length of text
  565. await selectCharacters(page, 5)
  566. // Remove formatting
  567. await page.keyboard.press(format.shortcut)
  568. await expect(page.locator('textarea >> nth=0')).toHaveText(
  569. 'Lorem ipsum-dolor sit.'
  570. )
  571. // Verify the word 'ipsum' is still selected
  572. const selection = await getSelection(page)
  573. expect(selection).toBe('ipsum')
  574. })
  575. })
  576. }
  577. })
  578. test.describe('Always auto-pair symbols', () => {
  579. // Define the symbols that should be auto-paired
  580. const autoPairSymbols = [
  581. { name: 'square brackets', prefix: '[', postfix: ']' },
  582. { name: 'curly brackets', prefix: '{', postfix: '}' },
  583. { name: 'parentheses', prefix: '(', postfix: ')' },
  584. // { name: 'angle brackets', prefix: '<', postfix: '>' },
  585. { name: 'backtick', prefix: '`', postfix: '`' },
  586. // { name: 'single quote', prefix: "'", postfix: "'" },
  587. // { name: 'double quote', prefix: '"', postfix: '"' },
  588. ]
  589. for (const symbol of autoPairSymbols) {
  590. test(`${symbol.name} auto-pairing`, async ({ page }) => {
  591. await createRandomPage(page)
  592. // Type prefix and check that the postfix is automatically added
  593. page.type('textarea >> nth=0', symbol.prefix, { delay: 100 })
  594. await expect(page.locator('textarea >> nth=0')).toHaveText(
  595. `${symbol.prefix}${symbol.postfix}`
  596. )
  597. // Check that the cursor is positioned correctly between the prefix and postfix
  598. const CursorPos = await getCursorPos(page)
  599. expect(CursorPos).toBe(symbol.prefix.length)
  600. })
  601. }
  602. })
  603. test.describe('Auto-pair symbols only with text selection', () => {
  604. const autoPairSymbols = [
  605. // { name: 'tilde', prefix: '~', postfix: '~' },
  606. { name: 'asterisk', prefix: '*', postfix: '*' },
  607. { name: 'underscore', prefix: '_', postfix: '_' },
  608. { name: 'caret', prefix: '^', postfix: '^' },
  609. { name: 'equal', prefix: '=', postfix: '=' },
  610. { name: 'slash', prefix: '/', postfix: '/' },
  611. { name: 'plus', prefix: '+', postfix: '+' },
  612. ]
  613. for (const symbol of autoPairSymbols) {
  614. test(`Only auto-pair ${symbol.name} with text selection`, async ({
  615. page,
  616. block,
  617. }) => {
  618. await createRandomPage(page)
  619. // type the symbol
  620. page.type('textarea >> nth=0', symbol.prefix, { delay: 100 })
  621. // Verify that there is no auto-pairing
  622. await expect(page.locator('textarea >> nth=0')).toHaveText(symbol.prefix)
  623. // remove prefix
  624. await page.keyboard.press('Backspace')
  625. // add text
  626. await block.mustType('Lorem')
  627. // select text
  628. await page.keyboard.press(modKey + '+a')
  629. // Type the prefix
  630. await page.type('textarea >> nth=0', symbol.prefix, { delay: 100 })
  631. // Verify that an additional postfix was automatically added around 'Lorem'
  632. await expect(page.locator('textarea >> nth=0')).toHaveText(
  633. `${symbol.prefix}Lorem${symbol.postfix}`
  634. )
  635. // Verify 'Lorem' is selected
  636. const selection = await getSelection(page)
  637. expect(selection).toBe('Lorem')
  638. })
  639. }
  640. })
  641. test('copy blocks should remove all ref-related values', async ({ page, block }) => {
  642. await createRandomPage(page)
  643. await block.mustFill('test')
  644. await page.keyboard.press(modKey + '+c', { delay: 100 })
  645. await page.waitForTimeout(100)
  646. await block.clickNext()
  647. await page.keyboard.press(modKey + '+v', { delay: 100 })
  648. await expect(page.locator('.open-block-ref-link')).toHaveCount(1)
  649. await page.keyboard.press('ArrowUp', { delay: 10 })
  650. await page.waitForTimeout(100)
  651. await page.keyboard.press('Escape')
  652. await expect(page.locator('.ls-block.selected')).toHaveCount(1)
  653. await page.keyboard.press(modKey + '+c', { delay: 100 })
  654. await block.clickNext()
  655. await page.keyboard.press(modKey + '+v', { delay: 100 })
  656. await block.clickNext() // let 3rd block leave editing state
  657. await expect(page.locator('.open-block-ref-link')).toHaveCount(1)
  658. })
  659. test('undo cut block should recover refs', async ({ page, block }) => {
  660. await createRandomPage(page)
  661. await block.mustFill('test')
  662. await page.keyboard.press(modKey + '+c', { delay: 100 })
  663. await block.clickNext()
  664. await page.keyboard.press(modKey + '+v', { delay: 100 })
  665. await expect(page.locator('.open-block-ref-link')).toHaveCount(1)
  666. await page.keyboard.press('ArrowUp', { delay: 10 })
  667. await page.waitForTimeout(100)
  668. await page.keyboard.press('Escape')
  669. await expect(page.locator('.ls-block.selected')).toHaveCount(1)
  670. await page.keyboard.press(modKey + '+x', { delay: 100 })
  671. await expect(page.locator('.ls-block')).toHaveCount(1)
  672. await page.keyboard.press(modKey + '+z')
  673. await page.waitForTimeout(100)
  674. await expect(page.locator('.ls-block')).toHaveCount(2)
  675. await expect(page.locator('.open-block-ref-link')).toHaveCount(1)
  676. })