editor.spec.ts 30 KB

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