editor.spec.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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.press('textarea >> nth=0', 'Enter')
  157. await block.waitForBlocks(2)
  158. await page.waitForTimeout(100)
  159. await page.keyboard.press(modKey + '+v')
  160. await page.waitForTimeout(100)
  161. await page.keyboard.press('Enter')
  162. // Check if the newly created block-ref has the same referenced content
  163. await expect(page.locator('.block-ref >> text="Some random text"')).toHaveCount(1);
  164. // Move cursor into the block ref
  165. for (let i = 0; i < 4; i++) {
  166. await page.press('textarea >> nth=0', 'ArrowLeft')
  167. }
  168. await expect(page.locator('textarea >> nth=0')).not.toHaveValue('Some random text')
  169. // FIXME: Sometimes the cursor is in the end of the editor
  170. for (let i = 0; i < 4; i++) {
  171. await page.press('textarea >> nth=0', 'ArrowLeft')
  172. }
  173. // Trigger replace-block-reference-with-content-at-point
  174. await page.keyboard.press(modKey + '+Shift+r')
  175. await expect(page.locator('textarea >> nth=0')).toHaveValue('Some random text')
  176. await block.escapeEditing()
  177. await expect(page.locator('.block-ref >> text="Some random text"')).toHaveCount(0);
  178. await expect(page.locator('text="Some random text"')).toHaveCount(2);
  179. })
  180. test('copy and paste block after editing new block #5962', async ({ page, block }) => {
  181. await createRandomPage(page)
  182. // Create a block and copy it in block-select mode
  183. await block.mustType('Block being copied')
  184. await page.keyboard.press('Escape')
  185. await expect(page.locator('.ls-block.selected')).toHaveCount(1)
  186. await page.keyboard.press(modKey + '+c', { delay: 10 })
  187. await page.keyboard.press('Enter')
  188. await expect(page.locator('.ls-block.selected')).toHaveCount(0)
  189. await expect(page.locator('textarea >> nth=0')).toBeVisible()
  190. await page.keyboard.press('Enter')
  191. await block.waitForBlocks(2)
  192. await page.waitForTimeout(100)
  193. await block.mustType('Typed block')
  194. await page.keyboard.press(modKey + '+v')
  195. await expect(page.locator('text="Typed block"')).toHaveCount(1)
  196. await block.waitForBlocks(3)
  197. })
  198. test('undo and redo after starting an action should not destroy text #6267', async ({ page, block }) => {
  199. await createRandomPage(page)
  200. // Get one piece of undo state onto the stack
  201. await block.mustType('text1 ')
  202. await page.waitForTimeout(500) // Wait for 500ms autosave period to expire
  203. // Then type more, start an action prompt, and undo
  204. await page.keyboard.type('text2 ', { delay: 50 })
  205. await page.keyboard.type('[[', { delay: 50 })
  206. await expect(page.locator(`[data-modal-name="page-search"]`)).toBeVisible()
  207. await page.keyboard.press(modKey + '+z')
  208. await page.waitForTimeout(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')
  228. await page.waitForTimeout(100)
  229. await expect(page.locator('text="/today"')).toHaveCount(0)
  230. await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  231. }
  232. })
  233. test('#6266 moving cursor outside of brackets should close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  234. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  235. // First, left arrow
  236. await createRandomPage(page)
  237. await block.mustFill('t ')
  238. await page.keyboard.type(commandTrigger, { delay: 20 })
  239. await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  240. await autocompleteMenu.expectVisible(modalName)
  241. await page.keyboard.press('ArrowLeft')
  242. await page.waitForTimeout(100)
  243. await autocompleteMenu.expectHidden(modalName)
  244. // Then, right arrow
  245. await createRandomPage(page)
  246. await block.mustFill('t ')
  247. await page.keyboard.type(commandTrigger, { delay: 20 })
  248. await autocompleteMenu.expectVisible(modalName)
  249. await page.waitForTimeout(100)
  250. // Move cursor outside of the space strictly between the double brackets
  251. await page.keyboard.press('ArrowRight')
  252. await page.waitForTimeout(100)
  253. await autocompleteMenu.expectHidden(modalName)
  254. }
  255. })
  256. // Old logic would fail this because it didn't do the check if @search-timeout was set
  257. test('#6266 moving cursor outside of parens immediately after searching should still close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  258. for (const [commandTrigger, modalName] of [['((', 'block-search']]) {
  259. await createRandomPage(page)
  260. // Open the autocomplete menu
  261. await block.mustFill('t ')
  262. await page.keyboard.type(commandTrigger, { delay: 20 })
  263. await page.waitForTimeout(100)
  264. await page.keyboard.type("some block search text")
  265. await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  266. await autocompleteMenu.expectVisible(modalName)
  267. // Move cursor outside of the space strictly between the double parens
  268. await page.keyboard.press('ArrowRight')
  269. await page.waitForTimeout(100)
  270. await autocompleteMenu.expectHidden(modalName)
  271. }
  272. })
  273. test('pressing up and down should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  274. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  275. await createRandomPage(page)
  276. // Open the autocomplete menu
  277. await block.mustFill('t ')
  278. await page.keyboard.type(commandTrigger, { delay: 20 })
  279. await autocompleteMenu.expectVisible(modalName)
  280. const cursorPos = await block.selectionStart()
  281. await page.keyboard.press('ArrowUp')
  282. await page.waitForTimeout(100)
  283. await autocompleteMenu.expectVisible(modalName)
  284. await expect(await block.selectionStart()).toEqual(cursorPos)
  285. await page.keyboard.press('ArrowDown')
  286. await page.waitForTimeout(100)
  287. await autocompleteMenu.expectVisible(modalName)
  288. await expect(await block.selectionStart()).toEqual(cursorPos)
  289. }
  290. })
  291. test('moving cursor inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  292. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  293. await createRandomPage(page)
  294. // Open the autocomplete menu
  295. await block.mustType('test ')
  296. await page.keyboard.type(commandTrigger, { delay: 20 })
  297. await page.waitForTimeout(100)
  298. if (commandTrigger === '[[') {
  299. await autocompleteMenu.expectVisible(modalName)
  300. }
  301. await page.keyboard.type("search", { delay: 20 })
  302. await autocompleteMenu.expectVisible(modalName)
  303. // Move cursor, still inside the brackets
  304. await page.keyboard.press('ArrowLeft')
  305. await page.waitForTimeout(100)
  306. await autocompleteMenu.expectVisible(modalName)
  307. }
  308. })
  309. test('moving cursor inside of brackets when autocomplete menu is closed should NOT open autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  310. // Note: (( behaves differently and doesn't auto-trigger when typing in it after exiting the search prompt once
  311. for (const [commandTrigger, modalName] of [['[[', 'page-search']]) {
  312. await createRandomPage(page)
  313. // Open the autocomplete menu
  314. await block.mustFill('')
  315. await page.keyboard.type(commandTrigger, { delay: 20 })
  316. await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  317. await autocompleteMenu.expectVisible(modalName)
  318. await block.escapeEditing()
  319. await autocompleteMenu.expectHidden(modalName)
  320. // Move cursor left until it's inside the brackets; shouldn't open autocomplete menu
  321. await page.locator('.block-content').click()
  322. await page.waitForTimeout(100)
  323. await autocompleteMenu.expectHidden(modalName)
  324. await page.keyboard.press('ArrowLeft', { delay: 50 })
  325. await autocompleteMenu.expectHidden(modalName)
  326. await page.keyboard.press('ArrowLeft', { delay: 50 })
  327. await autocompleteMenu.expectHidden(modalName)
  328. // Type a letter, this should open the autocomplete menu
  329. await page.keyboard.type('z', { delay: 20 })
  330. await page.waitForTimeout(100)
  331. await autocompleteMenu.expectVisible(modalName)
  332. }
  333. })
  334. test('selecting text inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  335. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  336. await createRandomPage(page)
  337. // Open the autocomplete menu
  338. await block.mustFill('')
  339. await page.keyboard.type(commandTrigger, { delay: 20 })
  340. await page.waitForTimeout(100)
  341. await autocompleteMenu.expectVisible(modalName)
  342. await page.keyboard.type("some page search text", { delay: 10 })
  343. await page.waitForTimeout(100)
  344. await autocompleteMenu.expectVisible(modalName)
  345. // Select some text within the brackets
  346. await page.keyboard.press('Shift+ArrowLeft')
  347. await page.waitForTimeout(100)
  348. await autocompleteMenu.expectVisible(modalName)
  349. }
  350. })
  351. test('pressing backspace and remaining inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  352. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  353. await createRandomPage(page)
  354. // Open the autocomplete menu
  355. await block.mustFill('test ')
  356. await page.keyboard.type(commandTrigger, { delay: 20 })
  357. await page.waitForTimeout(100)
  358. await autocompleteMenu.expectVisible(modalName)
  359. await page.keyboard.type("some page search text", { delay: 10 })
  360. await page.waitForTimeout(100)
  361. await autocompleteMenu.expectVisible(modalName)
  362. // Delete one character inside the brackets
  363. await page.keyboard.press('Backspace')
  364. await page.waitForTimeout(100)
  365. await autocompleteMenu.expectVisible(modalName)
  366. }
  367. })
  368. test('press escape when autocomplete menu is open, should close autocomplete menu only #6270', async ({ page, block }) => {
  369. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['/', 'commands']]) {
  370. await createRandomPage(page)
  371. // Open the action modal
  372. await block.mustFill('text ')
  373. await page.waitForTimeout(550)
  374. await page.keyboard.type(commandTrigger, { delay: 20 })
  375. await page.waitForTimeout(100)
  376. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  377. await page.waitForTimeout(100)
  378. // Press escape; should close action modal instead of exiting edit mode
  379. await page.keyboard.press('Escape')
  380. await page.waitForTimeout(100)
  381. await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  382. await page.waitForTimeout(1000)
  383. expect(await block.isEditing()).toBe(true)
  384. }
  385. })
  386. test('press escape when link/image dialog is open, should restore focus to input', async ({ page, block }) => {
  387. for (const [commandTrigger, modalName] of [['/link', 'commands']]) {
  388. await createRandomPage(page)
  389. // Open the action modal
  390. await block.mustFill('')
  391. await page.waitForTimeout(550)
  392. await page.keyboard.type(commandTrigger, { delay: 20 })
  393. await page.waitForTimeout(100)
  394. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  395. await page.waitForTimeout(100)
  396. // Press enter to open the link dialog
  397. await page.keyboard.press('Enter')
  398. await expect(page.locator(`[data-modal-name="input"]`)).toBeVisible()
  399. // Press escape; should close link dialog and restore focus to the block textarea
  400. await page.keyboard.press('Escape')
  401. await page.waitForTimeout(100)
  402. await expect(page.locator(`[data-modal-name="input"]`)).not.toBeVisible()
  403. await page.waitForTimeout(1000)
  404. expect(await block.isEditing()).toBe(true)
  405. }
  406. })
  407. test('should show text after soft return when node is collapsed #5074', async ({ page, block }) => {
  408. const delay = 300
  409. await createRandomPage(page)
  410. await page.type('textarea >> nth=0', 'Before soft return', { delay: 10 })
  411. await page.keyboard.press('Shift+Enter', { delay: 10 })
  412. await page.type('textarea >> nth=0', 'After soft return', { delay: 10 })
  413. await block.enterNext()
  414. expect(await block.indent()).toBe(true)
  415. await block.mustType('Child text')
  416. // collapse
  417. await page.click('.block-control >> nth=0')
  418. await block.waitForBlocks(1)
  419. // select the block that has the soft return
  420. await page.keyboard.press('ArrowDown')
  421. await page.waitForTimeout(delay)
  422. await page.keyboard.press('Enter')
  423. await page.waitForTimeout(delay)
  424. await expect(page.locator('textarea >> nth=0')).toHaveText('Before soft return\nAfter soft return')
  425. // zoom into the block
  426. page.click('a.block-control + a')
  427. await page.waitForNavigation()
  428. await page.waitForTimeout(delay * 3)
  429. // select the block that has the soft return
  430. await page.keyboard.press('ArrowDown')
  431. await page.waitForTimeout(delay)
  432. await page.keyboard.press('Enter')
  433. await page.waitForTimeout(delay)
  434. await expect(page.locator('textarea >> nth=0')).toHaveText('Before soft return\nAfter soft return')
  435. })
  436. test('should not erase typed text when expanding block quickly after typing #3891', async ({ page, block }) => {
  437. await createRandomPage(page)
  438. await block.mustFill('initial text,')
  439. await page.waitForTimeout(500)
  440. await page.type('textarea >> nth=0', ' then expand', { delay: 10 })
  441. // A quick cmd-down must not destroy the typed text
  442. await page.keyboard.press(modKey + '+ArrowDown')
  443. await page.waitForTimeout(500)
  444. expect(await page.inputValue('textarea >> nth=0')).toBe(
  445. 'initial text, then expand'
  446. )
  447. // First undo should delete the last typed information, not undo a no-op expand action
  448. await page.keyboard.press(modKey + '+z')
  449. expect(await page.inputValue('textarea >> nth=0')).toBe(
  450. 'initial text,'
  451. )
  452. await page.keyboard.press(modKey + '+z')
  453. expect(await page.inputValue('textarea >> nth=0')).toBe(
  454. ''
  455. )
  456. })
  457. test('should keep correct undo and redo seq after indenting or outdenting the block #7615',async({page,block}) => {
  458. await createRandomPage(page)
  459. await block.mustFill("foo")
  460. await page.keyboard.press("Enter")
  461. await expect(page.locator('textarea >> nth=0')).toHaveText("")
  462. await block.indent()
  463. await block.mustFill("bar")
  464. await expect(page.locator('textarea >> nth=0')).toHaveText("bar")
  465. await page.keyboard.press(modKey + '+z')
  466. // should undo "bar" input
  467. await expect(page.locator('textarea >> nth=0')).toHaveText("")
  468. await page.keyboard.press(modKey + '+Shift+z')
  469. // should redo "bar" input
  470. await expect(page.locator('textarea >> nth=0')).toHaveText("bar")
  471. await page.keyboard.press("Shift+Tab", { delay: 10 })
  472. await page.waitForTimeout(100)
  473. await page.keyboard.press("Enter")
  474. await page.waitForTimeout(100)
  475. await expect(page.locator('textarea >> nth=0')).toHaveText("")
  476. // swap input seq
  477. await block.mustFill("baz")
  478. await block.indent()
  479. await page.keyboard.press(modKey + '+z')
  480. // should undo indention
  481. await expect(page.locator('textarea >> nth=0')).toHaveText("baz")
  482. await page.keyboard.press("Shift+Tab")
  483. await page.waitForTimeout(100)
  484. await page.keyboard.press("Enter")
  485. await page.waitForTimeout(100)
  486. await expect(page.locator('textarea >> nth=0')).toHaveText("")
  487. // #7615
  488. await page.keyboard.type("aaa")
  489. await block.indent()
  490. await page.keyboard.type(" bbb")
  491. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa bbb")
  492. await page.keyboard.press(modKey + '+z')
  493. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
  494. await page.keyboard.press(modKey + '+z')
  495. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
  496. await page.keyboard.press(modKey + '+z')
  497. await expect(page.locator('textarea >> nth=0')).toHaveText("")
  498. await page.keyboard.press(modKey + '+Shift+z')
  499. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
  500. await page.keyboard.press(modKey + '+Shift+z')
  501. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
  502. await page.keyboard.press(modKey + '+Shift+z')
  503. await expect(page.locator('textarea >> nth=0')).toHaveText("aaa bbb")
  504. })
  505. test.describe('Text Formatting', () => {
  506. const formats = [
  507. { name: 'bold', prefix: '**', postfix: '**', shortcut: modKey + '+b' },
  508. { name: 'italic', prefix: '*', postfix: '*', shortcut: modKey + '+i' },
  509. {
  510. name: 'strikethrough',
  511. prefix: '~~',
  512. postfix: '~~',
  513. shortcut: modKey + '+Shift+s',
  514. },
  515. // {
  516. // name: 'underline',
  517. // prefix: '<u>',
  518. // postfix: '</u>',
  519. // shortcut: modKey + '+u',
  520. // },
  521. ]
  522. for (const format of formats) {
  523. test.describe(`${format.name} formatting`, () => {
  524. test('Applying to an empty selection inserts placeholder formatting and places cursor correctly', async ({
  525. page,
  526. block,
  527. }) => {
  528. await createRandomPage(page)
  529. const text = 'Lorem ipsum'
  530. await block.mustFill(text)
  531. // move the cursor to the end of Lorem
  532. await repeatKeyPress(page, 'ArrowLeft', text.length - 'ipsum'.length)
  533. await page.keyboard.press('Space')
  534. // Apply formatting
  535. await page.keyboard.press(format.shortcut)
  536. await expect(page.locator('textarea >> nth=0')).toHaveText(
  537. `Lorem ${format.prefix}${format.postfix} ipsum`
  538. )
  539. // Verify cursor position
  540. const cursorPos = await getCursorPos(page)
  541. expect(cursorPos).toBe(' ipsum'.length + format.prefix.length)
  542. })
  543. test('Applying to an entire block encloses the block in formatting and places cursor correctly', async ({
  544. page,
  545. block,
  546. }) => {
  547. await createRandomPage(page)
  548. const text = 'Lorem ipsum-dolor sit.'
  549. await block.mustFill(text)
  550. // Select the entire block
  551. await page.keyboard.press(modKey + '+a')
  552. // Apply formatting
  553. await page.keyboard.press(format.shortcut)
  554. await expect(page.locator('textarea >> nth=0')).toHaveText(
  555. `${format.prefix}${text}${format.postfix}`
  556. )
  557. // Verify cursor position
  558. const cursorPosition = await getCursorPos(page)
  559. expect(cursorPosition).toBe(format.prefix.length + text.length)
  560. })
  561. test('Applying and then removing from a word connected with a special character correctly formats and then reverts', async ({
  562. page,
  563. block,
  564. }) => {
  565. await createRandomPage(page)
  566. await block.mustFill('Lorem ipsum-dolor sit.')
  567. // Select 'ipsum'
  568. // Move the cursor to the desired position
  569. await moveCursor(page, -16)
  570. // Select the desired length of text
  571. await selectCharacters(page, 5)
  572. // Apply formatting
  573. await page.keyboard.press(format.shortcut)
  574. // Verify that 'ipsum' is formatted
  575. await expect(page.locator('textarea >> nth=0')).toHaveText(
  576. `Lorem ${format.prefix}ipsum${format.postfix}-dolor sit.`
  577. )
  578. // Re-select 'ipsum'
  579. // Move the cursor to the desired position
  580. await moveCursor(page, -5)
  581. // Select the desired length of text
  582. await selectCharacters(page, 5)
  583. // Remove formatting
  584. await page.keyboard.press(format.shortcut)
  585. await expect(page.locator('textarea >> nth=0')).toHaveText(
  586. 'Lorem ipsum-dolor sit.'
  587. )
  588. // Verify the word 'ipsum' is still selected
  589. const selection = await getSelection(page)
  590. expect(selection).toBe('ipsum')
  591. })
  592. })
  593. }
  594. })
  595. test.describe('Always auto-pair symbols', () => {
  596. // Define the symbols that should be auto-paired
  597. const autoPairSymbols = [
  598. { name: 'square brackets', prefix: '[', postfix: ']' },
  599. { name: 'curly brackets', prefix: '{', postfix: '}' },
  600. { name: 'parentheses', prefix: '(', postfix: ')' },
  601. // { name: 'angle brackets', prefix: '<', postfix: '>' },
  602. { name: 'backtick', prefix: '`', postfix: '`' },
  603. // { name: 'single quote', prefix: "'", postfix: "'" },
  604. // { name: 'double quote', prefix: '"', postfix: '"' },
  605. ]
  606. for (const symbol of autoPairSymbols) {
  607. test(`${symbol.name} auto-pairing`, async ({ page }) => {
  608. await createRandomPage(page)
  609. // Type prefix and check that the postfix is automatically added
  610. page.type('textarea >> nth=0', symbol.prefix, { delay: 100 })
  611. await expect(page.locator('textarea >> nth=0')).toHaveText(
  612. `${symbol.prefix}${symbol.postfix}`
  613. )
  614. // Check that the cursor is positioned correctly between the prefix and postfix
  615. const CursorPos = await getCursorPos(page)
  616. expect(CursorPos).toBe(symbol.prefix.length)
  617. })
  618. }
  619. })
  620. test.describe('Auto-pair symbols only with text selection', () => {
  621. const autoPairSymbols = [
  622. // { name: 'tilde', prefix: '~', postfix: '~' },
  623. { name: 'asterisk', prefix: '*', postfix: '*' },
  624. { name: 'underscore', prefix: '_', postfix: '_' },
  625. { name: 'caret', prefix: '^', postfix: '^' },
  626. { name: 'equal', prefix: '=', postfix: '=' },
  627. { name: 'slash', prefix: '/', postfix: '/' },
  628. { name: 'plus', prefix: '+', postfix: '+' },
  629. ]
  630. for (const symbol of autoPairSymbols) {
  631. test(`Only auto-pair ${symbol.name} with text selection`, async ({
  632. page,
  633. block,
  634. }) => {
  635. await createRandomPage(page)
  636. // type the symbol
  637. page.type('textarea >> nth=0', symbol.prefix, { delay: 100 })
  638. // Verify that there is no auto-pairing
  639. await expect(page.locator('textarea >> nth=0')).toHaveText(symbol.prefix)
  640. // remove prefix
  641. await page.keyboard.press('Backspace')
  642. // add text
  643. await block.mustType('Lorem')
  644. // select text
  645. await page.keyboard.press(modKey + '+a')
  646. // Type the prefix
  647. await page.type('textarea >> nth=0', symbol.prefix, { delay: 100 })
  648. // Verify that an additional postfix was automatically added around 'Lorem'
  649. await expect(page.locator('textarea >> nth=0')).toHaveText(
  650. `${symbol.prefix}Lorem${symbol.postfix}`
  651. )
  652. // Verify 'Lorem' is selected
  653. const selection = await getSelection(page)
  654. expect(selection).toBe('Lorem')
  655. })
  656. }
  657. })
  658. test('copy blocks should remove all ref-related values', async ({ page, block }) => {
  659. await createRandomPage(page)
  660. await block.mustFill('test')
  661. await page.keyboard.press(modKey + '+c', { delay: 10 })
  662. await block.clickNext()
  663. await page.keyboard.press(modKey + '+v', { delay: 10 })
  664. await expect(page.locator('.open-block-ref-link')).toHaveCount(1)
  665. await page.keyboard.press('ArrowUp', { delay: 10 })
  666. await page.waitForTimeout(100)
  667. await page.keyboard.press('Escape')
  668. await expect(page.locator('.ls-block.selected')).toHaveCount(1)
  669. await page.keyboard.press(modKey + '+c', { delay: 10 })
  670. await block.clickNext()
  671. await page.keyboard.press(modKey + '+v', { delay: 10 })
  672. await block.clickNext() // let 3rd block leave editing state
  673. await expect(page.locator('.open-block-ref-link')).toHaveCount(1)
  674. })
  675. test('undo cut block should recover refs', async ({ page, block }) => {
  676. await createRandomPage(page)
  677. await block.mustFill('test')
  678. await page.keyboard.press(modKey + '+c', { delay: 10 })
  679. await block.clickNext()
  680. await page.keyboard.press(modKey + '+v', { delay: 10 })
  681. await expect(page.locator('.open-block-ref-link')).toHaveCount(1)
  682. await page.keyboard.press('ArrowUp', { delay: 10 })
  683. await page.waitForTimeout(100)
  684. await page.keyboard.press('Escape')
  685. await expect(page.locator('.ls-block.selected')).toHaveCount(1)
  686. await page.keyboard.press(modKey + '+x', { delay: 10 })
  687. await expect(page.locator('.ls-block')).toHaveCount(1)
  688. await page.keyboard.press(modKey + '+z')
  689. await page.waitForTimeout(100)
  690. await expect(page.locator('.ls-block')).toHaveCount(2)
  691. await expect(page.locator('.open-block-ref-link')).toHaveCount(1)
  692. })