editor.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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.mustType('text1 ')
  177. await page.waitForTimeout(500) // Wait for 500ms autosave period to expire
  178. // Then type more, start an action prompt, and undo
  179. await page.keyboard.type('text2 ', { delay: 50 })
  180. for (const char of '[[') {
  181. await page.keyboard.type(char, { delay: 50 })
  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 page.waitForTimeout(100)
  244. await autocompleteMenu.expectVisible(modalName)
  245. await page.waitForTimeout(100)
  246. // Move cursor outside of the space strictly between the double brackets
  247. await page.keyboard.press('ArrowRight')
  248. await page.waitForTimeout(100)
  249. await autocompleteMenu.expectHidden(modalName)
  250. }
  251. })
  252. // Old logic would fail this because it didn't do the check if @search-timeout was set
  253. test('#6266 moving cursor outside of parens immediately after searching should still close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  254. for (const [commandTrigger, modalName] of [['((', 'block-search']]) {
  255. await createRandomPage(page)
  256. // Open the autocomplete menu
  257. // TODO: Maybe remove these "text " entries in tests that don't need them
  258. await block.mustFill('')
  259. await page.waitForTimeout(550)
  260. for (const char of commandTrigger) {
  261. await page.keyboard.type(char)
  262. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  263. }
  264. await page.waitForTimeout(100)
  265. await page.keyboard.type("some block search text")
  266. await autocompleteMenu.expectVisible(modalName)
  267. // Move cursor outside of the space strictly between the double parens
  268. await page.keyboard.press('ArrowRight')
  269. await page.waitForTimeout(100)
  270. await autocompleteMenu.expectHidden(modalName)
  271. }
  272. })
  273. test('pressing up and down should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  274. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  275. await createRandomPage(page)
  276. // Open the autocomplete menu
  277. await block.mustFill('')
  278. for (const char of commandTrigger) {
  279. await page.keyboard.type(char)
  280. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  281. }
  282. await autocompleteMenu.expectVisible(modalName)
  283. const cursorPos = await block.selectionStart()
  284. await page.keyboard.press('ArrowUp')
  285. await page.waitForTimeout(100)
  286. await autocompleteMenu.expectVisible(modalName)
  287. await expect(await block.selectionStart()).toEqual(cursorPos)
  288. await page.keyboard.press('ArrowDown')
  289. await page.waitForTimeout(100)
  290. await autocompleteMenu.expectVisible(modalName)
  291. await expect(await block.selectionStart()).toEqual(cursorPos)
  292. }
  293. })
  294. test('moving cursor inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  295. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  296. await createRandomPage(page)
  297. // Open the autocomplete menu
  298. await block.mustFill('')
  299. for (const char of commandTrigger) {
  300. await page.keyboard.type(char)
  301. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  302. }
  303. await page.waitForTimeout(100)
  304. if (commandTrigger === '[[') {
  305. await autocompleteMenu.expectVisible(modalName)
  306. }
  307. await page.keyboard.type("search")
  308. await page.waitForTimeout(100)
  309. await autocompleteMenu.expectVisible(modalName)
  310. // Move cursor, still inside the brackets
  311. await page.keyboard.press('ArrowLeft')
  312. await page.waitForTimeout(100)
  313. await autocompleteMenu.expectVisible(modalName)
  314. }
  315. })
  316. test('moving cursor inside of brackets when autocomplete menu is closed should NOT open autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  317. // Note: (( behaves differently and doesn't auto-trigger when typing in it after exiting the search prompt once
  318. for (const [commandTrigger, modalName] of [['[[', 'page-search']]) {
  319. await createRandomPage(page)
  320. // Open the autocomplete menu
  321. await block.mustFill('')
  322. for (const char of commandTrigger) {
  323. await page.keyboard.type(char)
  324. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  325. }
  326. await autocompleteMenu.expectVisible(modalName)
  327. await block.escapeEditing()
  328. await autocompleteMenu.expectHidden(modalName)
  329. // Move cursor left until it's inside the brackets; shouldn't open autocomplete menu
  330. await page.locator('.block-content').click()
  331. await page.waitForTimeout(100)
  332. await autocompleteMenu.expectHidden(modalName)
  333. await page.keyboard.press('ArrowLeft')
  334. await page.waitForTimeout(100)
  335. await autocompleteMenu.expectHidden(modalName)
  336. await page.keyboard.press('ArrowLeft')
  337. await page.waitForTimeout(100)
  338. await autocompleteMenu.expectHidden(modalName)
  339. // Type a letter, this should open the autocomplete menu
  340. await page.keyboard.type('z')
  341. await page.waitForTimeout(100)
  342. await autocompleteMenu.expectVisible(modalName)
  343. }
  344. })
  345. test('selecting text inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  346. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  347. await createRandomPage(page)
  348. // Open the autocomplete menu
  349. await block.mustFill('')
  350. for (const char of commandTrigger) {
  351. await page.keyboard.type(char)
  352. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  353. }
  354. await page.waitForTimeout(100)
  355. await autocompleteMenu.expectVisible(modalName)
  356. await page.keyboard.type("some page search text")
  357. await page.waitForTimeout(100)
  358. await autocompleteMenu.expectVisible(modalName)
  359. // Select some text within the brackets
  360. await page.keyboard.press('Shift+ArrowLeft')
  361. await page.waitForTimeout(100)
  362. await autocompleteMenu.expectVisible(modalName)
  363. }
  364. })
  365. test('pressing backspace and remaining inside of brackets should NOT close autocomplete menu', async ({ page, block, autocompleteMenu }) => {
  366. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['((', 'block-search']]) {
  367. await createRandomPage(page)
  368. // Open the autocomplete menu
  369. await block.mustFill('')
  370. for (const char of commandTrigger) {
  371. await page.keyboard.type(char)
  372. await page.waitForTimeout(10) // Sometimes it doesn't trigger without this
  373. }
  374. await page.waitForTimeout(100)
  375. await autocompleteMenu.expectVisible(modalName)
  376. await page.keyboard.type("some page search text")
  377. await page.waitForTimeout(100)
  378. await autocompleteMenu.expectVisible(modalName)
  379. // Delete one character inside the brackets
  380. await page.keyboard.press('Backspace')
  381. await page.waitForTimeout(100)
  382. await autocompleteMenu.expectVisible(modalName)
  383. }
  384. })
  385. test('press escape when autocomplete menu is open, should close autocomplete menu only #6270', async ({ page, block }) => {
  386. for (const [commandTrigger, modalName] of [['[[', 'page-search'], ['/', 'commands']]) {
  387. await createRandomPage(page)
  388. // Open the action modal
  389. await block.mustFill('text ')
  390. await page.waitForTimeout(550)
  391. for (const char of commandTrigger) {
  392. await page.keyboard.type(char) // Type it one character at a time, because too quickly can fail to trigger it sometimes
  393. }
  394. await page.waitForTimeout(100)
  395. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  396. await page.waitForTimeout(100)
  397. // Press escape; should close action modal instead of exiting edit mode
  398. await page.keyboard.press('Escape')
  399. await page.waitForTimeout(100)
  400. await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
  401. await page.waitForTimeout(1000)
  402. expect(await block.isEditing()).toBe(true)
  403. }
  404. })
  405. test('press escape when link/image dialog is open, should restore focus to input', async ({ page, block }) => {
  406. for (const [commandTrigger, modalName] of [['/link', 'commands']]) {
  407. await createRandomPage(page)
  408. // Open the action modal
  409. await block.mustFill('')
  410. await page.waitForTimeout(550)
  411. for (const char of commandTrigger) {
  412. await page.keyboard.type(char) // Type it one character at a time, because too quickly can fail to trigger it sometimes
  413. }
  414. await page.waitForTimeout(100)
  415. await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
  416. await page.waitForTimeout(100)
  417. // Press enter to open the link dialog
  418. await page.keyboard.press('Enter')
  419. await expect(page.locator(`[data-modal-name="input"]`)).toBeVisible()
  420. // Press escape; should close link dialog and restore focus to the block textarea
  421. await page.keyboard.press('Escape')
  422. await page.waitForTimeout(100)
  423. await expect(page.locator(`[data-modal-name="input"]`)).not.toBeVisible()
  424. await page.waitForTimeout(1000)
  425. expect(await block.isEditing()).toBe(true)
  426. }
  427. })