editor.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. for (let [idx, events] of [
  87. kb_events.win10_pinyin_left_full_square_bracket,
  88. kb_events.macos_pinyin_left_full_square_bracket
  89. // TODO: support #3741
  90. // kb_events.win10_legacy_pinyin_left_full_square_bracket,
  91. ].entries()) {
  92. await createRandomPage(page)
  93. let check_text = "#3251 test " + idx
  94. await block.mustFill(check_text + "【")
  95. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  96. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text + '【')
  97. await block.mustFill(check_text + "【【")
  98. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  99. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(check_text + '[[]]')
  100. };
  101. // dont trigger RIME #3440
  102. for (let [idx, events] of [
  103. kb_events.macos_pinyin_selecting_candidate_double_left_square_bracket,
  104. kb_events.win10_RIME_selecting_candidate_double_left_square_bracket
  105. ].entries()) {
  106. await createRandomPage(page)
  107. let check_text = "#3440 test " + idx
  108. await block.mustFill(check_text)
  109. await dispatch_kb_events(page, ':nth-match(textarea, 1)', events)
  110. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe(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. }
  114. })
  115. test('copy & paste block ref and replace its content', async ({ page, block }) => {
  116. await createRandomPage(page)
  117. await block.mustFill('Some random text')
  118. // FIXME: copy instantly will make content disappear
  119. await page.waitForTimeout(1000)
  120. if (IsMac) {
  121. await page.keyboard.press('Meta+c')
  122. } else {
  123. await page.keyboard.press('Control+c')
  124. }
  125. await page.press('textarea >> nth=0', 'Enter')
  126. if (IsMac) {
  127. await page.keyboard.press('Meta+v')
  128. } else {
  129. await page.keyboard.press('Control+v')
  130. }
  131. await page.keyboard.press('Enter')
  132. const blockRef = page.locator('.block-ref >> text="Some random text"');
  133. // Check if the newly created block-ref has the same referenced content
  134. await expect(blockRef).toHaveCount(1);
  135. // Move cursor into the block ref
  136. for (let i = 0; i < 4; i++) {
  137. await page.press('textarea >> nth=0', 'ArrowLeft')
  138. }
  139. // Trigger replace-block-reference-with-content-at-point
  140. if (IsMac) {
  141. await page.keyboard.press('Meta+Shift+r')
  142. } else {
  143. await page.keyboard.press('Control+Shift+v')
  144. }
  145. })
  146. test('copy and paste block after editing new block #5962', async ({ page, block }) => {
  147. await createRandomPage(page)
  148. // Create a block and copy it in block-select mode
  149. await block.mustFill('Block being copied')
  150. await page.waitForTimeout(100)
  151. await page.keyboard.press('Escape')
  152. await page.waitForTimeout(100)
  153. if (IsMac) {
  154. await page.keyboard.press('Meta+c')
  155. } else {
  156. await page.keyboard.press('Control+c')
  157. }
  158. // await page.waitForTimeout(100)
  159. await page.keyboard.press('Enter')
  160. await page.waitForTimeout(100)
  161. await page.keyboard.press('Enter')
  162. await page.waitForTimeout(100)
  163. // Create a new block with some text
  164. await page.keyboard.insertText("Typed block")
  165. // Quickly paste the copied block
  166. if (IsMac) {
  167. await page.keyboard.press('Meta+v')
  168. } else {
  169. await page.keyboard.press('Control+v')
  170. }
  171. await expect(page.locator('text="Typed block"')).toHaveCount(1);
  172. })
  173. test('undo and redo after starting an action should not destroy text #6267', async ({ page, block }) => {
  174. await createRandomPage(page)
  175. // Get one piece of undo state onto the stack
  176. await block.mustFill('text1 ')
  177. await page.waitForTimeout(550) // Wait for 500ms autosave period to expire
  178. // Then type more, start an action prompt, and undo
  179. await page.keyboard.type('text2 ')
  180. for (const char of '[[') {
  181. await page.keyboard.type(char)
  182. }
  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. for (const char of commandTrigger) {
  209. await page.keyboard.type(char)
  210. }
  211. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  212. // Undo, removing "/today", and closing the action modal
  213. if (IsMac) {
  214. await page.keyboard.press('Meta+z')
  215. } else {
  216. await page.keyboard.press('Control+z')
  217. }
  218. await page.waitForTimeout(100)
  219. await expect(page.locator('text="/today"')).toHaveCount(0)
  220. await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  221. }
  222. })
  223. test('#6266 moving cursor outside of brackets should close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  224. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  225. // First, left arrow
  226. await createRandomPage(page)
  227. await block.mustFill('')
  228. for (const char of commandTrigger) {
  229. await page.keyboard.type(char)
  230. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  231. }
  232. await autocompleteMenu.expectVisible(modalName)
  233. await page.keyboard.press('ArrowLeft')
  234. await page.waitForTimeout(100)
  235. await autocompleteMenu.expectHidden(modalName)
  236. // Then, right arrow
  237. await createRandomPage(page)
  238. await block.mustFill('')
  239. for (const char of commandTrigger) {
  240. await page.keyboard.type(char)
  241. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  242. }
  243. await autocompleteMenu.expectVisible(modalName)
  244. await page.waitForTimeout(100)
  245. // Move cursor outside of the space strictly between the double brackets
  246. await page.keyboard.press('ArrowRight')
  247. await page.waitForTimeout(100)
  248. await autocompleteMenu.expectHidden(modalName)
  249. }
  250. })
  251. // Old logic would fail this because it didn't do the check if @search-timeout was set
  252. test('#6266 moving cursor outside of parens immediately after searching should still close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  253. for (const [commandTrigger, modalName] of [['((', 'block-search']]) {
  254. await createRandomPage(page)
  255. // Open the autocomplete menu
  256. // TODO: Maybe remove these "text " entries in tests that don't need them
  257. await block.mustFill('')
  258. await page.waitForTimeout(550)
  259. for (const char of commandTrigger) {
  260. await page.keyboard.type(char)
  261. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  262. }
  263. await page.waitForTimeout(100)
  264. await page.keyboard.type("some block search text")
  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('')
  277. for (const char of commandTrigger) {
  278. await page.keyboard.type(char)
  279. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  280. }
  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.mustFill('')
  298. for (const char of commandTrigger) {
  299. await page.keyboard.type(char)
  300. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  301. }
  302. await page.waitForTimeout(100)
  303. if (commandTrigger === '[[') {
  304. await autocompleteMenu.expectVisible(modalName)
  305. }
  306. await page.keyboard.type("search")
  307. await page.waitForTimeout(100)
  308. await autocompleteMenu.expectVisible(modalName)
  309. // Move cursor, still inside the brackets
  310. await page.keyboard.press('ArrowLeft')
  311. await page.waitForTimeout(100)
  312. await autocompleteMenu.expectVisible(modalName)
  313. }
  314. })
  315. test('moving cursor inside of brackets when autocomplete menu is closed should NOT open autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  316. // Note: (( behaves differently and doesn't auto-trigger when typing in it after exiting the search prompt once
  317. for (const [commandTrigger, modalName] of [['[[', 'page-search']]) {
  318. await createRandomPage(page)
  319. // Open the autocomplete menu
  320. await block.mustFill('')
  321. for (const char of commandTrigger) {
  322. await page.keyboard.type(char)
  323. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  324. }
  325. await autocompleteMenu.expectVisible(modalName)
  326. await block.escapeEditing()
  327. await autocompleteMenu.expectHidden(modalName)
  328. // Move cursor left until it's inside the brackets; shouldn't open autocomplete menu
  329. await page.locator('.block-content').click()
  330. await page.waitForTimeout(100)
  331. await autocompleteMenu.expectHidden(modalName)
  332. await page.keyboard.press('ArrowLeft')
  333. await page.waitForTimeout(100)
  334. await autocompleteMenu.expectHidden(modalName)
  335. await page.keyboard.press('ArrowLeft')
  336. await page.waitForTimeout(100)
  337. await autocompleteMenu.expectHidden(modalName)
  338. // Type a letter, this should open the autocomplete menu
  339. await page.keyboard.type('z')
  340. await page.waitForTimeout(100)
  341. await autocompleteMenu.expectVisible(modalName)
  342. }
  343. })
  344. test('selecting text inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  345. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  346. await createRandomPage(page)
  347. // Open the autocomplete menu
  348. await block.mustFill('')
  349. for (const char of commandTrigger) {
  350. await page.keyboard.type(char)
  351. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  352. }
  353. await page.waitForTimeout(100)
  354. await autocompleteMenu.expectVisible(modalName)
  355. await page.keyboard.type("some page search text")
  356. await page.waitForTimeout(100)
  357. await autocompleteMenu.expectVisible(modalName)
  358. // Select some text within the brackets
  359. await page.keyboard.press('Shift+ArrowLeft')
  360. await page.waitForTimeout(100)
  361. await autocompleteMenu.expectVisible(modalName)
  362. }
  363. })
  364. test('pressing backspace and remaining inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  365. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  366. await createRandomPage(page)
  367. // Open the autocomplete menu
  368. await block.mustFill('')
  369. for (const char of commandTrigger) {
  370. await page.keyboard.type(char)
  371. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  372. }
  373. await page.waitForTimeout(100)
  374. await autocompleteMenu.expectVisible(modalName)
  375. await page.keyboard.type("some page search text")
  376. await page.waitForTimeout(100)
  377. await autocompleteMenu.expectVisible(modalName)
  378. // Delete one character inside the brackets
  379. await page.keyboard.press('Backspace')
  380. await page.waitForTimeout(100)
  381. await autocompleteMenu.expectVisible(modalName)
  382. }})
  383. test('press escape when autocomplete menu is open, should close autocomplete menu only #6270', async ({ page, block }) => {
  384. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['/', 'commands']]) {
  385. await createRandomPage(page)
  386. // Open the action modal
  387. await block.mustFill('text ')
  388. await page.waitForTimeout(550)
  389. for (const char of commandTrigger) {
  390. await page.keyboard.type(char) // Type it one character at a time, because too quickly can fail to trigger it sometimes
  391. }
  392. await page.waitForTimeout(100)
  393. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  394. await page.waitForTimeout(100)
  395. // Press escape; should close action modal instead of exiting edit mode
  396. await page.keyboard.press('Escape')
  397. await page.waitForTimeout(100)
  398. await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  399. await page.waitForTimeout(1000)
  400. expect(await block.isEditing()).toBe(true)
  401. }
  402. })
  403. test('press escape when link/image dialog is open, should restore focus to input', async ({ page, block }) => {
  404. for (const [commandTrigger, modalName] of [['/link', 'commands']]) {
  405. await createRandomPage(page)
  406. // Open the action modal
  407. await block.mustFill('')
  408. await page.waitForTimeout(550)
  409. for (const char of commandTrigger) {
  410. await page.keyboard.type(char) // Type it one character at a time, because too quickly can fail to trigger it sometimes
  411. }
  412. await page.waitForTimeout(100)
  413. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  414. await page.waitForTimeout(100)
  415. // Press enter to open the link dialog
  416. await page.keyboard.press('Enter')
  417. await expect(page.locator(`[data-modal-name="input"]`)).toBeVisible()
  418. // Press escape; should close link dialog and restore focus to the block textarea
  419. await page.keyboard.press('Escape')
  420. await page.waitForTimeout(100)
  421. await expect(page.locator(`[data-modal-name="input"]`)).not.toBeVisible()
  422. await page.waitForTimeout(1000)
  423. expect(await block.isEditing()).toBe(true)
  424. }
  425. })