Joan Ortiz was waiting for a carpet cleaner to arrive and trying to coax her young children — including 2-year-old Nikolette Rivera — upstairs before he showed up.

But when Nikolette heard a knock at the door of their Kensington rowhouse, Ortiz recalled Tuesday, the girl insisted on seeing who it was. Ortiz lifted her into her arms, allowing her to twirl her mother’s hair, and let the man inside.

Within minutes, Ortiz said, she and Nikolette were knocked to the floor, and the carpet cleaner fell back into the television stand.

Bullets had ripped through the front of Ortiz’s house. One struck the carpet cleaner, Arik Enatayev, in the stomach. Another struck Nikolette in the head.

When police arrived, Ortiz said, they called an EMT in an attempt to save Nikolette. Ortiz said she asked them not to — she knew her daughter was dead, and just wanted another moment to hold her.

Ortiz’s harrowing account came during a preliminary hearing Tuesday for Tayvon Thomas, accused of firing an assault rifle into Ortiz’s house on the 3300 block of North Water Street on Oct. 20.

The shooting, which left Nikolette dead and Enatayev wounded, led to public outcry and was the second shooting of a child that weekend — both allegedly with the same assault rifle.

Authorities have described the shooting of Nikolette as a botched attempt by Thomas and a coconspirator, Freddie Perez, to shoot at the girl’s father in an effort to seize control of a Kensington drug corner.

According to testimony Tuesday, Thomas confessed his actions to detectives when questioned Oct. 23, saying that he thought his desired target, Nikolai Rivera, was at the house and that he didn’t know the toddler was inside.

Thomas was held for trial on all counts, including murder, nine counts of attempted murder, and related charges. He also is accused of shooting at a passing SUV on the 400 block of Clearfield Street three minutes earlier, allegedly as part of the same plot to target rival drug dealers in the neighborhood.

Perez waived his preliminary hearing, so he also will be held for trial on all counts.

In addition to Ortiz’s testimony, Thomas’ hearing featured a summary of his alleged confession to detectives; surveillance video that showed two men committing the shooting on Clearfield, then casing the blocks around Nikolette’s home before and after that shooting; and testimony from the carpet cleaner.

Enatayev told Assistant District Attorney Tracie Gaydos that he had been in the home only a few minutes when he was struck by bullets flying through the wall. He said he had actually hoped to tell Ortiz he couldn’t complete the job that day because he had been unable to find parking nearby.

Instead, he said, “I just got hit, and my body flew into the TV stand.” He was taken to Temple University Hospital by police and released in about a day, he said.

Enatayev’s testimony was part of an unusual day in court.

Municipal Court Judge Karen Y. Simmons ordered Enatayev held in custody before and after his time on the witness stand, saying she saw him taking photographs of the courtroom with his cell phone while he was seated in the gallery.

Simmons charged Enatayev with contempt and unlawful use of an electronic device, but ultimately found him not guilty and let him go. Enatayev said he had intended to text a friend because the courtroom reminded him of the movies. He said he couldn’t read the signs prohibiting cell phone use because he does not read English well.

“Sitting [in custody] was worse than getting shot,” Enatayev told Simmons before she released him.

Thomas and Perez remain jailed without bail while awaiting trial. They are scheduled to be formally arraigned next month.