editor.spec.ts 30 KB

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