Skip to content

Commit

Permalink
feat: merge master and canary to get latest updates [HOMER-894] (#1300)
Browse files Browse the repository at this point in the history
* chore(deps-dev): bump eslint-plugin-react from 7.29.4 to 7.30.0 (#1273)

* chore(deps-dev): bump rollup from 2.73.0 to 2.74.1 (#1274)

* chore(deps-dev): bump typescript from 4.6.4 to 4.7.2 (#1277)

* chore(deps-dev): bump lint-staged from 12.4.1 to 12.4.2 (#1276)

* chore(deps-dev): bump rollup from 2.74.1 to 2.75.3 (#1279)

* chore(deps-dev): bump lint-staged from 12.4.2 to 12.4.3 (#1278)

* chore(deps-dev): bump rollup from 2.75.3 to 2.75.4 (#1280)

* feat: add option to include absolute elements in auto resizer (#901)

* feat: introduce some additional logic for window.updateHeight method in order to count absolute positioned elements.

* feat: check for absolute positioned elements with MutationObserver

* test: add mocked Array<MutationRecord> to test file

* refactor: apply review suggestions

Co-authored-by: Andi Pätzold <[email protected]>

* refactor: get height based on highest getBoundingClientRect.bottom

* refactor: always run observer to store absolute elements

* feat: update type definition for startAutoResizer

* fix: call updateHeight after checkAbsoluteElements is updated

* feat: do not check elements with negative bottom

* Update lib/window.ts

* fix: code readability

* fix: ignore tranformed elements

* Revert "fix: ignore tranformed elements"

This reverts commit 624e9f3.

* fix: do not check for negative bottom

* docs: add warning about negative bottom and transformed elements

* Update lib/types/window.types.ts

Co-authored-by: Andi Pätzold <[email protected]>

Co-authored-by: Renato Massao Yonamine <[email protected]>
Co-authored-by: Renato Massao Yonamine <[email protected]>
Co-authored-by: Andi Pätzold <[email protected]>

* chore: 4.6.0 [skip ci]

* add option to include absolute elements in auto resizer ([#901](#901)) ([0269b00](0269b00))

* chore(deps-dev): bump lint-staged from 12.4.3 to 13.0.0 (#1281)

* chore(deps-dev): bump rollup from 2.75.4 to 2.75.5 (#1282)

* chore(deps-dev): bump rollup-plugin-typescript2 from 0.31.2 to 0.32.0 (#1283)

* chore(deps): bump semver-regex from 3.1.3 to 3.1.4 (#1285)

* chore(deps): bump npm from 8.4.1 to 8.12.1 (#1286)

* chore(deps-dev): bump contentful-management from 10.6.0 to 10.6.2 (#1287)

* chore(deps-dev): bump typescript from 4.7.2 to 4.7.3 (#1288)

* chore(deps-dev): bump rollup-plugin-typescript2 from 0.32.0 to 0.32.1 (#1289)

* chore(deps-dev): bump rollup from 2.75.5 to 2.75.6 (#1290)

* chore(deps-dev): bump lint-staged from 13.0.0 to 13.0.1 (#1292)

* chore(deps-dev): bump semantic-release from 19.0.2 to 19.0.3 (#1291)

* feat: added worflowdefinition type to exported entities and to api.types (#1294)

* chore: 4.7.0 [skip ci]

* added worflowdefinition type to exported entities and to api.types ([#1294](#1294)) ([b2d917f](b2d917f))

* chore(deps-dev): bump prettier from 2.6.2 to 2.7.0 (#1293)

* chore(deps-dev): bump contentful-management from 10.6.2 to 10.6.3 (#1296)

* feat: exported workflow definition type (#1295)

* chore: 4.8.0 [skip ci]

* exported workflow definition type ([#1295](#1295)) ([490e5db](490e5db))

* chore(deps-dev): bump prettier from 2.7.0 to 2.7.1 (#1298)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Bohdan Hutsol <[email protected]>
Co-authored-by: Renato Massao Yonamine <[email protected]>
Co-authored-by: Renato Massao Yonamine <[email protected]>
Co-authored-by: Andi Pätzold <[email protected]>
Co-authored-by: semantic-release-bot <[email protected]>
Co-authored-by: Maria Jaramillo <[email protected]>
  • Loading branch information
8 people committed Jun 16, 2022
1 parent caf6383 commit b4ac65c
Show file tree
Hide file tree
Showing 10 changed files with 1,541 additions and 13,135 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
.env
.env.local
.envrc

# editors
.vscode/
.idea/
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# [4.8.0-alpha.1](https://github.com/contentful/ui-extensions-sdk/compare/v4.8.0...v4.8.0-alpha.1) (2022-06-16)

# [4.8.0](https://github.com/contentful/ui-extensions-sdk/compare/v4.7.0...v4.8.0) (2022-06-15)

### Features

- exported workflow definition type ([#1295](https://github.com/contentful/ui-extensions-sdk/issues/1295)) ([490e5db](https://github.com/contentful/ui-extensions-sdk/commit/490e5dba4e899134155cf21c3fff2ef772692c0b))

# [4.7.0](https://github.com/contentful/ui-extensions-sdk/compare/v4.6.0...v4.7.0) (2022-06-15)

### Features

- added worflowdefinition type to exported entities and to api.types ([#1294](https://github.com/contentful/ui-extensions-sdk/issues/1294)) ([b2d917f](https://github.com/contentful/ui-extensions-sdk/commit/b2d917fb7f261a27a00661486594d8d0e2f5ac25))

# [4.6.0](https://github.com/contentful/ui-extensions-sdk/compare/v4.5.0...v4.6.0) (2022-06-01)

### Features

- add option to include absolute elements in auto resizer ([#901](https://github.com/contentful/ui-extensions-sdk/issues/901)) ([0269b00](https://github.com/contentful/ui-extensions-sdk/commit/0269b008cce52af63adef26cb68e163c56c3f2e0))

# [4.6.0-alpha.1](https://github.com/contentful/ui-extensions-sdk/compare/v4.5.0...v4.6.0-alpha.1) (2022-05-18)

### Bug Fixes
Expand Down
3 changes: 2 additions & 1 deletion lib/types/api.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Entry,
Task,
Asset,
WorkflowDefinition,
} from './entities'
import { EntryAPI } from './entry.types'
import { SpaceAPI } from './space.types'
Expand Down Expand Up @@ -142,7 +143,7 @@ export type JSONPatchItem = {
path: string
value?: any
}
type PatchEntity = Entry | Task | Asset
type PatchEntity = Entry | Task | Asset | WorkflowDefinition

export interface AccessAPI {
can(action: 'read' | 'update', entity: 'EditorInterface' | EditorInterface): Promise<boolean>
Expand Down
1 change: 1 addition & 0 deletions lib/types/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type {
TeamProps as Team,
UploadProps as Upload,
UserProps as User,
WorkflowDefinitionProps as WorkflowDefinition,
} from 'contentful-management/types'

export interface CanonicalRequest {
Expand Down
1 change: 1 addition & 0 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type {
Task,
Team,
User,
WorkflowDefinition,
} from './entities'

export type { EntryAPI, TaskAPI, TaskInputData } from './entry.types'
Expand Down
11 changes: 9 additions & 2 deletions lib/types/window.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export interface WindowAPI {
/** Sets the iframe height to the given value in pixels or using scrollHeight if value is not passed */
updateHeight: (height?: number) => void
/** Listens for DOM changes and updates height when the size changes. */
startAutoResizer: () => void
/**
* Listens for DOM changes and updates height when the size changes.
*
* When passing absoluteElements true, an infinite loop can happen if elements are always rendered below the height of the window.
* e.g. transformed elements or absolute elements with negative bottom
* @param {Object} opts - Options to be passed to auto resize
* @param {Boolean} opts.absoluteElements - Defines if auto resize should consider absolut elements
*/
startAutoResizer: ({ absoluteElements }?: { absoluteElements?: boolean }) => void
/** Stops resizing the iframe automatically. */
stopAutoResizer: () => void
}
99 changes: 84 additions & 15 deletions lib/window.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,92 @@
import { Channel } from './channel'
import { WindowAPI } from './types'
import { WindowAPI } from './types/window.types'

export default function createWindow(currentWindow: Window, channel: Channel): WindowAPI {
// We assume MutationObserver and ResizeObserver were defined by the web-app
const { document, MutationObserver, ResizeObserver } = currentWindow as any

const autoUpdateHeight = () => {
self.updateHeight()
}
const mutationObserver = new MutationObserver(autoUpdateHeight)
const resizeObserver = new ResizeObserver(autoUpdateHeight)
let oldHeight: number
let isAutoResizing = false
let checkAbsoluteElements = false
const absolutePositionedElems: Set<Element> = new Set()

const mutationObserver = new MutationObserver((mutations: Array<MutationRecord>) => {
checkAbsolutePositionedElems(mutations)
if (isAutoResizing) {
self.updateHeight()
}
})

const resizeObserver = new ResizeObserver(() => {
self.updateHeight()
})

mutationObserver.observe(document.body, {
attributes: true,
childList: true,
subtree: true,
characterData: true,
})

const self = { startAutoResizer, stopAutoResizer, updateHeight }
return self

function startAutoResizer() {
function checkAbsoluteElementStyle(type: MutationRecordType, element: Element) {
const computedStyle = getComputedStyle(element)

if (computedStyle.position !== 'absolute') {
return false
}

switch (type) {
case 'attributes':
return computedStyle.display !== 'none'

default:
return true
}
}

function checkAbsolutePositionedElems(mutations: Array<MutationRecord>) {
mutations.forEach((mutation) => {
switch (mutation.type) {
case 'attributes':
if (mutation.target.nodeType === Node.ELEMENT_NODE) {
const element = mutation.target as Element
if (checkAbsoluteElementStyle(mutation.type, element)) {
absolutePositionedElems.add(element)
} else {
absolutePositionedElems.delete(element)
}
}
break

case 'childList':
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as Element
if (checkAbsoluteElementStyle(mutation.type, element)) {
absolutePositionedElems.add(element)
}
}
})

mutation.removedNodes.forEach((node) => {
const element = node as Element
absolutePositionedElems.delete(element)
})
break
}
})
}

function startAutoResizer({ absoluteElements = false } = {}) {
checkAbsoluteElements = Boolean(absoluteElements)
self.updateHeight()
if (isAutoResizing) {
return
}
isAutoResizing = true
mutationObserver.observe(document.body, {
attributes: true,
childList: true,
subtree: true,
characterData: true,
})
resizeObserver.observe(document.body)
}

Expand All @@ -36,13 +95,23 @@ export default function createWindow(currentWindow: Window, channel: Channel): W
return
}
isAutoResizing = false
mutationObserver.disconnect()
resizeObserver.disconnect()
}

function updateHeight(height: number | null = null) {
if (height === null) {
height = Math.ceil(document.documentElement.getBoundingClientRect().height)
const documentHeight = Math.ceil(document.documentElement.getBoundingClientRect().height)

// Only check for absolute elements if option is provided to startAutoResizer
if (checkAbsoluteElements && absolutePositionedElems.size) {
let maxHeight = documentHeight
absolutePositionedElems.forEach((element) => {
maxHeight = Math.max(element.getBoundingClientRect().bottom, maxHeight)
})
height = maxHeight
} else {
height = documentHeight
}
}

if (height !== oldHeight) {
Expand Down
Loading

0 comments on commit b4ac65c

Please sign in to comment.