editor.spec.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. import { expect } from '@playwright/test'
  2. import { test } from './fixtures'
  3. import { createRandomPage, enterNextBlock, systemModifier, IsMac } from './utils'
  4. import { dispatch_kb_events } from './util/keyboard-events'
  5. import * as kb_events from './util/keyboard-events'
  6. test('hashtag and quare brackets in same line #4178', async ({ page }) => {
  7. await createRandomPage(page)
  8. await page.type('textarea >> nth=0', '#foo bar')
  9. await enterNextBlock(page)
  10. await page.type('textarea >> nth=0', 'bar [[blah]]', { delay: 100 })
  11. for (let i = 0; i < 12; i++) {
  12. await page.press('textarea >> nth=0', 'ArrowLeft')
  13. }
  14. await page.type('textarea >> nth=0', ' ')
  15. await page.press('textarea >> nth=0', 'ArrowLeft')
  16. await page.type('textarea >> nth=0', '#')
  17. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  18. await page.type('textarea >> nth=0', 'fo')
  19. await page.click('.absolute >> text=' + 'foo')
  20. expect(await page.inputValue('textarea >> nth=0')).toBe(
  21. '#foo bar [[blah]]'
  22. )
  23. })
  24. test('disappeared children #4814', async ({ page, block }) => {
  25. await createRandomPage(page)
  26. await block.mustType('parent')
  27. await block.enterNext()
  28. expect(await block.indent()).toBe(true)
  29. for (let i = 0; i < 5; i++) {
  30. await block.mustType(i.toString())
  31. await block.enterNext()
  32. }
  33. // collapse
  34. await page.click('.block-control >> nth=0')
  35. // expand
  36. await page.click('.block-control >> nth=0')
  37. await block.waitForBlocks(7) // 1 + 5 + 1 empty
  38. // Ensures there's no active editor
  39. await expect(page.locator('.editor-inner')).toHaveCount(0, { timeout: 500 })
  40. })
  41. test('create new page from bracketing text #4971', async ({ page, block }) => {
  42. let title = 'Page not Exists yet'
  43. await createRandomPage(page)
  44. await block.mustType(`[[${title}]]`)
  45. await page.keyboard.press(systemModifier('Control+o'))
  46. // Check page title equals to `title`
  47. await page.waitForTimeout(100)
  48. expect(await page.locator('h1.title').innerText()).toContain(title)
  49. // Check there're linked references
  50. await page.waitForSelector(`.references .ls-block >> nth=1`, { state: 'detached', timeout: 100 })
  51. })
  52. test.skip('backspace and cursor position #4897', async ({ page, block }) => {
  53. await createRandomPage(page)
  54. // Delete to previous block, and check cursor postion, with markup
  55. await block.mustFill('`012345`')
  56. await block.enterNext()
  57. await block.mustType('`abcdef', { toBe: '`abcdef`' }) // "`" auto-completes
  58. expect(await block.selectionStart()).toBe(7)
  59. expect(await block.selectionEnd()).toBe(7)
  60. for (let i = 0; i < 7; i++) {
  61. await page.keyboard.press('ArrowLeft')
  62. }
  63. expect(await block.selectionStart()).toBe(0)
  64. await page.keyboard.press('Backspace')
  65. await block.waitForBlocks(1) // wait for delete and re-render
  66. expect(await block.selectionStart()).toBe(8)
  67. })
  68. test.skip('next block and cursor position', async ({ page, block }) => {
  69. await createRandomPage(page)
  70. // Press Enter and check cursor postion, with markup
  71. await block.mustType('abcde`12345', { toBe: 'abcde`12345`' }) // "`" auto-completes
  72. for (let i = 0; i < 7; i++) {
  73. await page.keyboard.press('ArrowLeft')
  74. }
  75. expect(await block.selectionStart()).toBe(5) // after letter 'e'
  76. await block.enterNext()
  77. expect(await block.selectionStart()).toBe(0) // should at the beginning of the next block
  78. const locator = page.locator('textarea >> nth=0')
  79. await expect(locator).toHaveText('`12345`', { timeout: 1000 })
  80. })
  81. test(
  82. "Press CJK Left Black Lenticular Bracket `【` by 2 times #3251 should trigger [[]], " +
  83. "but dont trigger RIME #3440 ",
  84. // cases should trigger [[]] #3251
  85. async ({ page, block }) => {
  86. // This test requires dev mode
  87. test.skip(process.env.RELEASE === 'true', 'not avaliable for release version')
  88. for (let [idx, events] of [
  89. kb_events.win10_pinyin_left_full_square_bracket,
  90. kb_events.macos_pinyin_left_full_square_bracket
  91. // TODO: support #3741
  92. // kb_events.win10_legacy_pinyin_left_full_square_bracket,
  93. ].entries()) {
  94. await createRandomPage(page)
  95. let check_text = "#3251 test " + idx
  96. await block.mustFill(check_text + "【")
  97. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  98. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text + '【')
  99. await block.mustFill(check_text + "【【")
  100. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  101. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text + '[[]]')
  102. };
  103. // dont trigger RIME #3440
  104. for (let [idx, events] of [
  105. kb_events.macos_pinyin_selecting_candidate_double_left_square_bracket,
  106. kb_events.win10_RIME_selecting_candidate_double_left_square_bracket
  107. ].entries()) {
  108. await createRandomPage(page)
  109. let check_text = "#3440 test " + idx
  110. await block.mustFill(check_text)
  111. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  112. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text)
  113. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  114. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text)
  115. }
  116. })
  117. test('copy & paste block ref and replace its content', async ({ page, block }) => {
  118. await createRandomPage(page)
  119. await block.mustFill('Some random text')
  120. // FIXME: copy instantly will make content disappear
  121. await page.waitForTimeout(1000)
  122. if (IsMac) {
  123. await page.keyboard.press('Meta+c')
  124. } else {
  125. await page.keyboard.press('Control+c')
  126. }
  127. await page.press('textarea >> nth=0', 'Enter')
  128. if (IsMac) {
  129. await page.keyboard.press('Meta+v')
  130. } else {
  131. await page.keyboard.press('Control+v')
  132. }
  133. await page.keyboard.press('Enter')
  134. const blockRef = page.locator('.block-ref >> text="Some random text"');
  135. // Check if the newly created block-ref has the same referenced content
  136. await expect(blockRef).toHaveCount(1);
  137. // Move cursor into the block ref
  138. for (let i = 0; i < 4; i++) {
  139. await page.press('textarea >> nth=0', 'ArrowLeft')
  140. }
  141. // Trigger replace-block-reference-with-content-at-point
  142. if (IsMac) {
  143. await page.keyboard.press('Meta+Shift+r')
  144. } else {
  145. await page.keyboard.press('Control+Shift+v')
  146. }
  147. })
  148. test('copy and paste block after editing new block #5962', async ({ page, block }) => {
  149. await createRandomPage(page)
  150. // Create a block and copy it in block-select mode
  151. await block.mustFill('Block being copied')
  152. await page.waitForTimeout(100)
  153. await page.keyboard.press('Escape')
  154. await page.waitForTimeout(100)
  155. if (IsMac) {
  156. await page.keyboard.press('Meta+c')
  157. } else {
  158. await page.keyboard.press('Control+c')
  159. }
  160. // await page.waitForTimeout(100)
  161. await page.keyboard.press('Enter')
  162. await page.waitForTimeout(100)
  163. await page.keyboard.press('Enter')
  164. await page.waitForTimeout(100)
  165. // Create a new block with some text
  166. await page.keyboard.insertText("Typed block")
  167. // Quickly paste the copied block
  168. if (IsMac) {
  169. await page.keyboard.press('Meta+v')
  170. } else {
  171. await page.keyboard.press('Control+v')
  172. }
  173. await expect(page.locator('text="Typed block"')).toHaveCount(1);
  174. })
  175. test('undo and redo after starting an action should not destroy text #6267', async ({ page, block }) => {
  176. await createRandomPage(page)
  177. // Get one piece of undo state onto the stack
  178. await block.mustType('text1 ')
  179. await page.waitForTimeout(500) // Wait for 500ms autosave period to expire
  180. // Then type more, start an action prompt, and undo
  181. await page.keyboard.type('text2 ', { delay: 50 })
  182. await page.keyboard.type('[[', { delay: 50 })
  183. await expect(page.locator(`[data-modal-name="page-search"]`)).toBeVisible()
  184. if (IsMac) {
  185. await page.keyboard.press('Meta+z')
  186. } else {
  187. await page.keyboard.press('Control+z')
  188. }
  189. await page.waitForTimeout(100)
  190. // Should close the action menu when we undo the action prompt
  191. await expect(page.locator(`[data-modal-name="page-search"]`)).not.toBeVisible()
  192. // It should undo to the last saved state, and not erase the previous undo action too
  193. await expect(page.locator('text="text1"')).toHaveCount(1)
  194. // And it should keep what was undone as a redo action
  195. if (IsMac) {
  196. await page.keyboard.press('Meta+Shift+z')
  197. } else {
  198. await page.keyboard.press('Control+Shift+z')
  199. }
  200. await expect(page.locator('text="text2"')).toHaveCount(1)
  201. })
  202. test('undo after starting an action should close the action menu #6269', async ({ page, block }) => {
  203. for (const [commandTrigger, modalName] of [['/', 'commands'], ['[[', 'page-search']]) {
  204. await createRandomPage(page)
  205. // Open the action modal
  206. await block.mustType('text1 ')
  207. await page.waitForTimeout(550)
  208. await page.keyboard.type(commandTrigger, { delay: 20 })
  209. await page.waitForTimeout(100) // Tolerable delay for the action menu to open
  210. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  211. // Undo, removing "/today", and closing the action modal
  212. if (IsMac) {
  213. await page.keyboard.press('Meta+z')
  214. } else {
  215. await page.keyboard.press('Control+z')
  216. }
  217. await page.waitForTimeout(100)
  218. await expect(page.locator('text="/today"')).toHaveCount(0)
  219. await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  220. }
  221. })
  222. test('#6266 moving cursor outside of brackets should close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  223. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  224. // First, left arrow
  225. await createRandomPage(page)
  226. await block.mustFill('t ')
  227. await page.keyboard.type(commandTrigger, { delay: 20 })
  228. await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  229. await autocompleteMenu.expectVisible(modalName)
  230. await page.keyboard.press('ArrowLeft')
  231. await page.waitForTimeout(100)
  232. await autocompleteMenu.expectHidden(modalName)
  233. // Then, right arrow
  234. await createRandomPage(page)
  235. await block.mustFill('t ')
  236. await page.keyboard.type(commandTrigger, { delay: 20 })
  237. await autocompleteMenu.expectVisible(modalName)
  238. await page.waitForTimeout(100)
  239. // Move cursor outside of the space strictly between the double brackets
  240. await page.keyboard.press('ArrowRight')
  241. await page.waitForTimeout(100)
  242. await autocompleteMenu.expectHidden(modalName)
  243. }
  244. })
  245. // Old logic would fail this because it didn't do the check if @search-timeout was set
  246. test('#6266 moving cursor outside of parens immediately after searching should still close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  247. for (const [commandTrigger, modalName] of [['((', 'block-search']]) {
  248. await createRandomPage(page)
  249. // Open the autocomplete menu
  250. await block.mustFill('t ')
  251. await page.keyboard.type(commandTrigger, { delay: 20 })
  252. await page.waitForTimeout(100)
  253. await page.keyboard.type("some block search text")
  254. await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  255. await autocompleteMenu.expectVisible(modalName)
  256. // Move cursor outside of the space strictly between the double parens
  257. await page.keyboard.press('ArrowRight')
  258. await page.waitForTimeout(100)
  259. await autocompleteMenu.expectHidden(modalName)
  260. }
  261. })
  262. test('pressing up and down should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  263. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  264. await createRandomPage(page)
  265. // Open the autocomplete menu
  266. await block.mustFill('t ')
  267. await page.keyboard.type(commandTrigger, { delay: 20 })
  268. await autocompleteMenu.expectVisible(modalName)
  269. const cursorPos = await block.selectionStart()
  270. await page.keyboard.press('ArrowUp')
  271. await page.waitForTimeout(100)
  272. await autocompleteMenu.expectVisible(modalName)
  273. await expect(await block.selectionStart()).toEqual(cursorPos)
  274. await page.keyboard.press('ArrowDown')
  275. await page.waitForTimeout(100)
  276. await autocompleteMenu.expectVisible(modalName)
  277. await expect(await block.selectionStart()).toEqual(cursorPos)
  278. }
  279. })
  280. test('moving cursor inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  281. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  282. await createRandomPage(page)
  283. // Open the autocomplete menu
  284. await block.mustType('test ')
  285. await page.keyboard.type(commandTrigger, { delay: 20 })
  286. await page.waitForTimeout(100)
  287. if (commandTrigger === '[[') {
  288. await autocompleteMenu.expectVisible(modalName)
  289. }
  290. await page.keyboard.type("search", { delay: 20 })
  291. await autocompleteMenu.expectVisible(modalName)
  292. // Move cursor, still inside the brackets
  293. await page.keyboard.press('ArrowLeft')
  294. await page.waitForTimeout(100)
  295. await autocompleteMenu.expectVisible(modalName)
  296. }
  297. })
  298. test('moving cursor inside of brackets when autocomplete menu is closed should NOT open autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  299. // Note: (( behaves differently and doesn't auto-trigger when typing in it after exiting the search prompt once
  300. for (const [commandTrigger, modalName] of [['[[', 'page-search']]) {
  301. await createRandomPage(page)
  302. // Open the autocomplete menu
  303. await block.mustFill('')
  304. await page.keyboard.type(commandTrigger, { delay: 20 })
  305. await page.waitForTimeout(100) // Sometimes it doesn't trigger without this
  306. await autocompleteMenu.expectVisible(modalName)
  307. await block.escapeEditing()
  308. await autocompleteMenu.expectHidden(modalName)
  309. // Move cursor left until it's inside the brackets; shouldn't open autocomplete menu
  310. await page.locator('.block-content').click()
  311. await page.waitForTimeout(100)
  312. await autocompleteMenu.expectHidden(modalName)
  313. await page.keyboard.press('ArrowLeft', { delay: 50 })
  314. await autocompleteMenu.expectHidden(modalName)
  315. await page.keyboard.press('ArrowLeft', { delay: 50 })
  316. await autocompleteMenu.expectHidden(modalName)
  317. // Type a letter, this should open the autocomplete menu
  318. await page.keyboard.type('z', { delay: 20 })
  319. await page.waitForTimeout(100)
  320. await autocompleteMenu.expectVisible(modalName)
  321. }
  322. })
  323. test('selecting text inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  324. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  325. await createRandomPage(page)
  326. // Open the autocomplete menu
  327. await block.mustFill('')
  328. await page.keyboard.type(commandTrigger, { delay: 20 })
  329. await page.waitForTimeout(100)
  330. await autocompleteMenu.expectVisible(modalName)
  331. await page.keyboard.type("some page search text", { delay: 10 })
  332. await page.waitForTimeout(100)
  333. await autocompleteMenu.expectVisible(modalName)
  334. // Select some text within the brackets
  335. await page.keyboard.press('Shift+ArrowLeft')
  336. await page.waitForTimeout(100)
  337. await autocompleteMenu.expectVisible(modalName)
  338. }
  339. })
  340. test('pressing backspace and remaining inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  341. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  342. await createRandomPage(page)
  343. // Open the autocomplete menu
  344. await block.mustFill('test ')
  345. await page.keyboard.type(commandTrigger, { delay: 20 })
  346. await page.waitForTimeout(100)
  347. await autocompleteMenu.expectVisible(modalName)
  348. await page.keyboard.type("some page search text", { delay: 10 })
  349. await page.waitForTimeout(100)
  350. await autocompleteMenu.expectVisible(modalName)
  351. // Delete one character inside the brackets
  352. await page.keyboard.press('Backspace')
  353. await page.waitForTimeout(100)
  354. await autocompleteMenu.expectVisible(modalName)
  355. }
  356. })
  357. test('press escape when autocomplete menu is open, should close autocomplete menu only #6270', async ({ page, block }) => {
  358. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['/', 'commands']]) {
  359. await createRandomPage(page)
  360. // Open the action modal
  361. await block.mustFill('text ')
  362. await page.waitForTimeout(550)
  363. await page.keyboard.type(commandTrigger, { delay: 20 })
  364. await page.waitForTimeout(100)
  365. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  366. await page.waitForTimeout(100)
  367. // Press escape; should close action modal instead of exiting edit mode
  368. await page.keyboard.press('Escape')
  369. await page.waitForTimeout(100)
  370. await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  371. await page.waitForTimeout(1000)
  372. expect(await block.isEditing()).toBe(true)
  373. }
  374. })
  375. test('press escape when link/image dialog is open, should restore focus to input', async ({ page, block }) => {
  376. for (const [commandTrigger, modalName] of [['/link', 'commands']]) {
  377. await createRandomPage(page)
  378. // Open the action modal
  379. await block.mustFill('')
  380. await page.waitForTimeout(550)
  381. await page.keyboard.type(commandTrigger, { delay: 20 })
  382. await page.waitForTimeout(100)
  383. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  384. await page.waitForTimeout(100)
  385. // Press enter to open the link dialog
  386. await page.keyboard.press('Enter')
  387. await expect(page.locator(`[data-modal-name="input"]`)).toBeVisible()
  388. // Press escape; should close link dialog and restore focus to the block textarea
  389. await page.keyboard.press('Escape')
  390. await page.waitForTimeout(100)
  391. await expect(page.locator(`[data-modal-name="input"]`)).not.toBeVisible()
  392. await page.waitForTimeout(1000)
  393. expect(await block.isEditing()).toBe(true)
  394. }
  395. })
  396. test('should show text after soft return when node is collapsed #5074', async ({ page, block }) => {
  397. const delay = 100
  398. await createRandomPage(page)
  399. await page.type('textarea >> nth=0', 'Before soft return', { delay: 10 })
  400. await page.keyboard.press('Shift+Enter', { delay: 10 })
  401. await page.type('textarea >> nth=0', 'After soft return', { delay: 10 })
  402. await block.enterNext()
  403. expect(await block.indent()).toBe(true)
  404. await block.mustType('Child text')
  405. await page.waitForTimeout(delay)
  406. // collapse
  407. await page.click('.block-control >> nth=0')
  408. await page.waitForTimeout(delay)
  409. // select the block that has the soft return
  410. await page.keyboard.press('ArrowDown')
  411. await page.waitForTimeout(delay)
  412. await page.keyboard.press('Enter')
  413. await page.waitForTimeout(delay)
  414. expect(await page.inputValue('textarea >> nth=0')).toBe(
  415. 'Before soft return\nAfter soft return'
  416. )
  417. // zoom into the block
  418. await page.click('a.block-control + a')
  419. await page.waitForTimeout(delay)
  420. // select the block that has the soft return
  421. await page.keyboard.press('ArrowDown')
  422. await page.waitForTimeout(delay)
  423. await page.keyboard.press('Enter')
  424. await page.waitForTimeout(delay)
  425. expect(await page.inputValue('textarea >> nth=0')).toBe(
  426. 'Before soft return\nAfter soft return'
  427. )
  428. })