editor.spec.ts 28 KB

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