editor.spec.ts 20 KB

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