editor.spec.ts 30 KB

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